JAVA第一次测试感受

本学期的第一节JAVA课,王老师让我们完成一个测试,模拟实现银行自动取款机(ATM)机的各项功能(取款,存款,查询余额,转账,修改密码),时间是三个小时。

要定义一个Account类,有七个私有变量,给每个人变量定义getset函数。并要求实现相关的功能,要求通过文本文件进行用户信息的录入,并可有通过程序在文本文件里修改用户的信息。

在测试之前我对JAVA进行了简单的学习,但是没有去学习文件的操作,关于文件什么都不会(之前学CC++的时候也没有学好文件操作)。因此在这次测试的过程中,我暂时没有实现文件的读入,而是实例化五个Account对象后,要求操作人员先输入五个用户信息后,再进行相关操作的选择。

为了方便实现操作过程中界面的跳转,我尽可能的写出了很多的函数,将各个操作独立的写出来,这样一来当程序判断输入的账号或者密码不正确的时候,可以很方便的跳转到相应的输入界面中再次进行输入,直到输入正确的字符。

在这次编程的过程中遇到了之前没有注意到的问题,JAVA的字符串内容判断是不能使用“==”的。使用“==”比较的是字符串的引用是否相同,例如String a=new String("abc"); String b=new String("abc");在这种情况下若使用“==”判断a,和b得到的结果是false。若想要判断字符串的内容是否相同,则需要用到equals函数进行判断,仍是例子里面的a和b,若使用a.equals(b)则可以返回true。

除此之外还遇到了一个问题,由于在很多的函数里面都需要用到输入,因此在每个函数里面我都新建了扫描器,程序完成后,编译器提示那些扫描器没有关闭,我便一个一个的对应加上了.close()。但是当我执行程序后,编译器却报错了。并提示错在输入的那些语句上。我便上网进行了查询,学得了以下知识。Scanner scan=new Scanner(System.in);这个语句便可以创建一个扫描器,获取从键盘输入的数据。如若再定义一个扫描器Scanner scan1=new Scanner(System.in);虽然scan和scan1是两个独立的对象,但是它们两个用的却是同一个输入流,若调用scan.close()则相当于执行System.in.close()。这样一来,关闭了其中任何一个扫描器后,别的扫描器也失去了获取从键盘输入的数据的能力了,因此当程序执行时,便会报错。解决办法便是,再各个函数里面不关闭扫描器,而是只在主函数的最后关闭主函数里面定义的扫描器。

测试结束之后,我对文件的简单读取和写入的操作进行了学习,以满足测试题目的要求,具体学会了一下简单操作。使用FileReader和BufferedReader和readline实现了从文本文件里面一行一行的读入信息。和我之前的程序相比,仍是先实例化五个对象,不过这次不需要操作者在程序开始后输入信息,而是程序从文本文件里面逐行读取信息并存储到对象相应的数据项里面。而当选择一些会修改到用户信息的操作时(如存取款,修改密码),我使用FileWriter和BufferedWriter和newLine将更新后的信息又按行写入了文件中(采用覆盖写入的方式),汇款的文本文件采用文件末尾追加的方式写入。

这段程序只可以简单的实现一些要求的基本操作,还有很多要求没有实现,我会在以后的学习过程中增强自己的能力,不断将这个程序完善,实现测试的所有要求。

源码:

package ruangongkaoshi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
//信1705-3 20173697 潘攀达
public class Account {
private String accountID;
private String accountname;
private String operatedate;
private int operatetype;
private String accountpassword;
private int accountbalance;
private int amount;
public void setID(String ID) {
this.accountID=ID;
}
public String getID() {
return this.accountID;
}
public void setname(String name) {
this.accountname=name;
}
public String getname() {
return this.accountname;
}
public void setdate(String date) {
this.operatedate=date;
}
public String getdate() {
return this.operatedate;
}
public void settype(int type) {
this.operatetype=type;
}
public int gettype() {
return this.operatetype;
}
public void setpassword(String word) {
this.accountpassword=word;
}
public String getpassword() {
return this.accountpassword;
}
public void setbalance(int balance) {
this.accountbalance=balance;
}
public int getbalance() {
return this.accountbalance;
}
public void setamount(int amount) {
this.amount=amount;
}
public int getamount() {
return this.amount;
}
Account(String accountID,String accountname,String accountpassword,int accountbalance){
this.accountID=accountID;
this.accountname=accountname;
this.accountpassword=accountpassword;
this.accountbalance=accountbalance;
}
Account(){
this.accountID="null";
this.accountname="null";
this.accountpassword="null";
this.accountbalance=0;
}
public static void zhanghaoshurujiemian(Account[] a) {
boolean bool1=false;
int hao=10;
Scanner scan=new Scanner(System.in);
System.out.println("**********************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统");
System.out.println("**********************************");
System.out.println(" 请输入您的账号:");
String zhanghao=scan.next();
for(int i=0;i<5;i++) {
if(a[i]!=null&&a[i].accountID.equals(zhanghao)) {
hao=i;
bool1=true;
break;
}
}
if(bool1) {
mimashuru(a,hao);
}
else {
System.out.println("未录入此账号,请重新输入。");
zhanghaoshurujiemian(a);
}
}
public static void gongnengjiemian()
{
System.out.println("**********************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统");
System.out.println("**********************************");
System.out.println(" 1、存款");
System.out.println(" 2、取款");
System.out.println(" 3、转账汇款");
System.out.println(" 4、修改密码");
System.out.println(" 5、查询余额");
System.out.println(" 6、退出");
System.out.println("**********************************");
}
public static void cun(Account [] a,int i) {
int nmoney;
int omoney=a[i].getbalance();
Scanner scan=new Scanner(System.in);
System.out.println("请输入存入的款数:");
int cmoney=scan.nextInt();
nmoney=a[i].getbalance()+cmoney;
a[i].setbalance(nmoney);
System.out.println("该账户的存款变化为:");
System.out.println(cmoney+"+"+omoney+"="+a[i].getbalance());
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void zhuan(Account [] a,int i) {
Scanner scan=new Scanner(System.in);
int zhuanru=6;
System.out.println("请输入转入用户的账号:");
String zhang=scan.nextLine();
for(int j=0;j<5;j++) {
if(a[j].accountID.equals(zhang)) {
zhuanru=j;
break;
}
}
System.out.println("请输入转入的金额:");
int zmoney=scan.nextInt();
int imoney=a[i].getbalance();
int jmoney=a[zhuanru].getbalance();
a[i].setbalance(imoney-zmoney);
a[zhuanru].setbalance(jmoney+zmoney);
System.out.println("汇款已完成");
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
FileWriter fw1 = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f=new File("accountinformation.txt");
fw1 = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter pw = new PrintWriter(fw1);
pw.println(a[i].getname()+"转"+zmoney+"元钱给"+a[zhuanru].getname());
pw.flush();
pw.println();
try {
fw1.flush();
pw.close();
fw1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void qu(Account [] a,int i) {
int nmoney;
int omoney=a[i].getbalance();
Scanner scan=new Scanner(System.in);
System.out.println("请输入取出的款数:");
int qmoney=scan.nextInt();
nmoney=omoney-qmoney;
a[i].setbalance(nmoney);
System.out.println("该账户的存款变化为:");
System.out.println(omoney+"-"+qmoney+"="+a[i].getbalance());
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void yue(Account [] a,int i) {
System.out.println("该账户的余额为:");
System.out.println(a[i].accountbalance);
}
public static void xiugai(Account[] a,int i) {
System.out.println("请输入新的密码:");
Scanner scan=new Scanner(System.in);
String newword=scan.nextLine();
a[i].setpassword(newword);
System.out.println("密码修改完成!");
File file=new File("accountlist.txt");
try {
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
for(int j=0;j<5;j++) {
if(a[j].getname().equals("null")) {
break;
}
bw.write(a[j].getname());
bw.newLine();
bw.write(a[j].getID());
bw.newLine();
bw.write(a[j].getpassword());
bw.newLine();
bw.write(String.valueOf(a[j].getbalance()));
bw.newLine();
}
bw.close();
fw.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void gongneng(Account [] a,int i) {
boolean bool3=true;
Scanner scan=new Scanner(System.in);
int choice;
do
{
gongnengjiemian();
System.out.println("请输入您的选择:");
choice=scan.nextInt();
switch(choice) {
case 1:
cun(a,i);
break;
case 2:
qu(a,i);
break;
case 3:
zhuan(a,i);
break;
case 4:
xiugai(a,i);
break;
case 5:
yue(a,i);
break;
case 6:
System.out.println("感谢您的使用!!!");
bool3=false;
break;
}
}
while(bool3);
}
public static void mimashuru(Account [] a,int i) {
Scanner scan=new Scanner(System.in);
System.out.println("**********************************");
System.out.println(" 欢迎使用中国工商银行自动柜员系统");
System.out.println("**********************************");
System.out.println(" 请输入您的密码:");
String mima=scan.nextLine();
if(a[i].accountpassword.equals(mima)) {
gongneng(a,i);
}
else {
System.out.println("密码输入有误,请重新输入。");
mimashuru(a,i);
}
}
public static void jinxing() {
Scanner scan=new Scanner(System.in);
Account []a=new Account[5];
a[0]=new Account();
a[1]=new Account();
a[2]=new Account();
a[3]=new Account();
a[4]=new Account();
try {
FileReader file = new FileReader("accountlist.txt");
BufferedReader br = new BufferedReader(file);
int i=0;
String str = null;
while(true) {
str = br.readLine();
if(str == null) {
break;
}
a[i].setname(str);
str = br.readLine();
a[i].setID(str);;
str = br.readLine();
a[i].setpassword(str);
str = br.readLine();
a[i].setbalance(Integer.parseInt(str));
++i;
}
br.close();
file.close();
} catch (FileNotFoundException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
zhanghaoshurujiemian(a);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
jinxing();
}
}

猜你喜欢

转载自www.cnblogs.com/ruangongyouxi/p/9696197.html
今日推荐