JavaSE实现ATM流程(练习)

/*
* 信1905-2班
* 杨传伟
* 2020.9.22
* 20194074
* 账号密码默认 666666
*/
package atm;

import java.util.*;
public class AccountManager {

public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("**********************************");
System.out.println(" 欢迎使用中国工商银行自动柜员机系统 ");
System.out.println("**********************************");
System.out.println(" 请输入你的账号 ");
System.out.println("**********************************");
String ID="88888888"; //ID 8位账号
boolean flag=false ; //判定账号是否正确
int count=0; //计算密码 输入次数

ArrayList<Account> array=new ArrayList<Account>();
Account aDemo=new Account();
Scanner sc=new Scanner(System.in);


/*
* 账号输入功能!
*/


while(true)
{
ID=sc.next();
flag=aDemo.idInput(ID);
if(flag==true)
break;
}

aDemo.setAmount(0);
aDemo.setAccountbalance(10000);
aDemo.setAccountID("88888888");
aDemo.setAccountpassword("666666");
array.add(aDemo);


/*
* 完成密码输入功能
* 错误提示: 密码输入错误
* 3次错误提示 : 该卡面已经三次输入错误,该卡已被没收,请与工行及时联系处理! 返回账号输入界面!
*
*/


while(true)
{
flag=aDemo.passInput();
if(flag==false) {
count++;
System.out.println("密码输入错误!请重新输入!");
}
else {
aDemo.mainATM();
break;
}
if(count==3)
{
System.out.println("该卡已经三次输入错误,该卡已被没收,请与工行及时联系处理! 返回账号输入界面!");
while(true)
{
ID=sc.next();
flag=aDemo.idInput(ID);
if(flag==true)
break;
}

}
}
Scanner SC=new Scanner(System.in);
int choice=99;
while(true)
{
choice=SC.nextInt();
switch(choice)
{
/*
* 调用存款功能
*
*/

case 1: aDemo.moneyInput();
break;
/*
* 调用取款功能
*
*/
case 2: aDemo.outaccount();
/*
* 调用修改密码功能
*
*/
break;

case 4: aDemo.changePass();
break;
}
aDemo.mainATM();
}
}
}

 

 

——————————————————————————————————————————————————————————————————————————

package atm;

import java.util.*;

 

public class Account {

//成员变量

 

private String accountID;      //用户8位账号

private String accountname;    //用户账户名称

private String operatedate;     //用户操作时间  10位  2020-09-22

private int operatetype;     

 

/*

* 1 存款

* 2 取款

* 3 转账汇款

* 4 表示修改账户密码

* 5 查询余额

*/

 

private String accountpassword ;    //六位用户密码

private int accountbalance ;        //账户余额

private int amount;                 //操作流水金额

 

Account(){};      //无参构造

 

public String getAccountID() {

return accountID;

}

public void setAccountID(String accountID) {

this.accountID = accountID;

}

public String getAccountname() {

return accountname;

}

public void setAccountname(String accountname) {

this.accountname = accountname;

}

public String getOperatedate() {

return operatedate;

}

public void setOperatedate(String operatedate) {

this.operatedate = operatedate;

}

public int getOperatetype() {

return operatetype;

}

public void setOperatetype(int operatetype) {

this.operatetype = operatetype;

}

public String getAccountpassword() {

return accountpassword;

}

public void setAccountpassword(String accountpassword) {

this.accountpassword = accountpassword;

}

public int getAccountbalance() {

return accountbalance;

}

public void setAccountbalance(int accountbalance) {

this.accountbalance = accountbalance;

}

public int getAmount() {

return amount;

}

public void setAmount(int amount) {

this.amount = amount;

}

 

 

/*

* 定义账号输入函数!

*/

 

 

public boolean idInput(String ID) {

 

boolean flag=true;

accountID=ID;

if(ID.length()!=8)

{

System.out.println("您输入的不是8位工商银行账号!请检查后重新输入!");

flag=false;

}

 

return flag;

}

 

 

/*

* 定义密码输入函数

* 错误提示: 密码输入错误

* 3次错误提示 :   该卡面已经三次输入错误,该卡已被没收,请与工行及时联系处理! 返回账号输入界面!

*/

 

 

public boolean passInput() {

final int count=0;

 

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统,请输入您的密码:\n"

+"**********************************");

String pass;

Scanner sc=new Scanner(System.in);

accountpassword=sc.next();

if(accountpassword.equals("666666"))

{

System.out.println("密码输入正确!");

return true;

 

}

else

{

return false;

}

}

 

 

/*

* 用户主界面输出

*/

 

public void mainATM() {

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"

+" 1、存款\n 2、取款\n 3、转账汇款\n 4、修改密码\n 5、查询余额\n"

+"**********************************");

}

 

 

/*

* 定义存款功能方法

*/

 

 

@SuppressWarnings("unlikely-arg-type")

public void moneyInput() {

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统,请输入存款金额:\n"

+"**********************************");

int money;

int flag=0;

boolean Flag=true;

Scanner sc=new Scanner(System.in);

money=sc.nextInt();

if(money<=0)

{

System.out.println("金额应该为不为0的正整数,请检查后重新输入!");

}

 

else {

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************"

+"当前账户存款操作成功!"+"当前账户余额为:"+(accountbalance+money)+"元");

System.out.print("请输入1进入账号界面!");

String ID="88888888";   //ID    8位账号

while(true) {

flag=sc.nextInt();

if(flag==1)

{

idInput(accountID);

System.out.println("**********************************");

System.out.println("     欢迎使用中国工商银行自动柜员机系统        ");

System.out.println("**********************************");

System.out.println("          请输入你的账号               ");

System.out.println("**********************************");

while(true)

{

ID=sc.next();

Flag=idInput(ID);

if(Flag==true)

break;

}

break;

}

else

{

System.out.println("您输入的不是 1 ,请重新输入!!!");

}

  }

   }

}

 

 

 

/*

* 定义取款方法

*/

 

 

 

public void outaccount() {

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************"

+"当前账户每日可以支取2万!\n"

+"1、100元\n2、500元\n3、1000元\n4、1500元\n5/2000元\n6/5000元\n7、其他金额\n8、退卡\n9、返回\n"

+"**********************************");

int choice=999;

Scanner sc=new Scanner(System.in);

choice=sc.nextInt();

int tempMoney;       //其他金额

switch(choice)

{

case 1:

if(accountbalance<100)

System.out.println("账户余额不足!");

else

{

accountbalance-=100;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作100元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

break;

case 2:

if(accountbalance<500)

System.out.println("账户余额不足!");

else

{

accountbalance-=500;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作500元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

break;

case 3:

if(accountbalance<1000)

System.out.println("账户余额不足!");

else

{

accountbalance-=1000;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作1000元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

break;

case 4:

if(accountbalance<1500)

System.out.println("账户余额不足!");

else

{

accountbalance-=1500;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作1500元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

    break;

case 5:

if(accountbalance<2000)

System.out.println("账户余额不足!");

else

{

accountbalance-=2000;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作2000元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

break;

case 6:

if(accountbalance<5000)

System.out.println("账户余额不足!");

else

{

accountbalance-=5000;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作5000元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

break;

case 7:

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"请输入取款功能:"+"**********************************\n");

 

tempMoney=sc.nextInt();

if(accountbalance<tempMoney)

System.out.println("账户余额不足!");

else

{

accountbalance-=tempMoney;

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"

+"**********************************\n"+"当前账户取款操作tempMoney元完成!\n当前账户余额为:"+accountbalance+"元\n"

+"**********************************");

}

break;

case 8:

System.exit(1);

break;

case 9:

mainATM();

break;

}

}

 

 

 

/*

* 定义修改密码方法

*/

 

 

public void changePass() {

String afterPass1;     //修改后的密码!

String afterPass2; 

String beforePass;    //修改前的密码!

Scanner sc=new Scanner(System.in);

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"

+"请输入当前密码:");

while(true)

{

beforePass=sc.next();

if(beforePass.equals(getAccountpassword())) {

while(true){

System.out.println("请输入修改密码:");

afterPass1=sc.next();

System.out.println("请输入确认密码:");

afterPass2=sc.next();

if(afterPass1.equals(afterPass2)) {

System.out.println("**********************************\n"+

"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"当前密码修改成功!"+"**********************************");

break;

}

else {

System.out.println("您两次输入的密码不一致,请重新输入!");

}

}

break;

}

else {

System.out.println("当前密码输入错误!请重新输入!");

}

}

}

}

 

 

 

 

 

 

 

 

2020 9.22 Java学习日记

今天下午进行了本学期第一次Java测试,这个特殊的下午我收获很多,感触很多。

下午的测试题目是用Java语言编写一个doc版本的ATM机,实现账户登录,密码修改,存款操作,取款操作,余额查询,流水查询和转账汇款等等功能。拿到这个题目,我首先考虑到了一个类来写方法,用另一个类来调用方法,但是结果并不尽如人意。

基本数据类写起来还是很简单的,但是另一个类我并没有处理好,首先我用一个数据类,定义了全部数据成员,并且定义了全部数据成员对应的getset方法,我觉得我思路还是很有问题的,因为我的主类很混乱。一般来说,主类只负责逻辑层面代码的调用,我的主类就有些问题。我首先在主类里写账号录入的代码,这里我出现了逻辑混乱的问题,我定义了太多的变量,导致这一块的内容很复杂。我本来想的是吧账号判断写成一个函数,然后只用主类调用一个函数来实现这个功能,但是在我写函数的时候就出现了问题,我并没有给函数传递参数,但是我当时并没有觉察到这个问题,所以我在主类重新定义了变量,然后那个账号判断函数其实只负责了很少了一部分内容,我感觉写的就是不伦不类,因为程序有要求说在之后这个账号判断界面要重复跳转调用,但是我这里写的很乱,这就导致我在之后只要用得到这个模块就很麻烦,不但要调用函数,而且要复制主类里的代码,而且还要定义一堆没什么用的变量。之后我写了输入密码的代码块,这里有一个重复输入错误三次及以上要重新输入的要求,我在处理这个问题的时候,一直在报一个错误,应该是我的while循环出现了问题,这里需要在密码输入错误之后跳转到账号判断界面,那么这就是我之前提到的问题,我需要调用函数,还需要把代码复制过来,这样一处理,这就很无奈,整个主类就显得十分臃肿繁琐。后面我去数据类定义存款功能,这个功能的实现我只是把存款之后的数值输出了出来,但是类里的数据值并没有改变,这是在我之后定义取款类的时候发现的,我发现我每次取款的余额都是基础值,不管我有没有存钱。定义取款方法,我使用了switch-case语句,但是case语句中不能写循环语句,那么我的账户功能的跳转就是无法完成,所以这个方法是失败的,有两个跳转功能都没有定义。

在我写了两个方法的时候,我发现只剩下40分钟的时间了,这时候我只写了两个方法,还有三个方法要写,我选择了去写账号密码修改的功能,这个方法的实现我还是很顺利的,也许是因为快要没时间了,写的虽然仓促,也还算成功。这时候我就不得不去主方法里用合理的逻辑性去调用方法,我之前都是写一个方法,调用一个方法看看结果对不对。因为第一我的方法定义并没有写完,第二我那个账号判断程序块写的很混乱,这就导致我之后的跳转功能没有办法,或者说很难实现。所以我只是写了一个switch-case语句来调用了3个方法,后来学长调试检查的时候一眼就看到了我忘记了写break语句,这真的是很不应该的错误了,我要好好反省。题目要求要使用文件来存储相关数据内容,但是我文件部分的学习可以说十分草率,一些内容还没有掌握的很好,一定要抓紧掌握。

 

总的来说,对我个人而言,我还是很喜欢这种形式的课程的,完完全全自己去摸索,这个过程我既讨厌又享受。收获还是很大的,有的错误真的是不自己去摸索不会发现的,比如说case语句没有写break,初学C语言的时候我犯过这样的错误,我以为我是不可能再犯这样的错误了,但是很不巧,今天下午就出现了,我也是很感谢学长的指导,对我说下去一定要好好学文件部分的内容,还要注意主函数的逻辑性调用!

 

 

package atm;import java.util.*;
public class Account {//成员变量private String accountID;      //用户8位账号private String accountname;    //用户账户名称private String operatedate;     //用户操作时间  10位  2020-09-22private int operatetype;     /* * 1 存款 * 2 取款 * 3 转账汇款 * 4 表示修改账户密码 * 5 查询余额 */private String accountpassword ;    //六位用户密码private int accountbalance ;        //账户余额private int amount;                 //操作流水金额Account(){};      //无参构造public String getAccountID() {return accountID;}public void setAccountID(String accountID) {this.accountID = accountID;}public String getAccountname() {return accountname;}public void setAccountname(String accountname) {this.accountname = accountname;}public String getOperatedate() {return operatedate;}public void setOperatedate(String operatedate) {this.operatedate = operatedate;}public int getOperatetype() {return operatetype;}public void setOperatetype(int operatetype) {this.operatetype = operatetype;}public String getAccountpassword() {return accountpassword;}public void setAccountpassword(String accountpassword) {this.accountpassword = accountpassword;}public int getAccountbalance() {return accountbalance;}public void setAccountbalance(int accountbalance) {this.accountbalance = accountbalance;}public int getAmount() {return amount;}public void setAmount(int amount) {this.amount = amount;}/* * 定义账号输入函数! */public boolean idInput(String ID) {boolean flag=true;accountID=ID;if(ID.length()!=8){System.out.println("您输入的不是8位工商银行账号!请检查后重新输入!");flag=false;}return flag;}/* * 定义密码输入函数 * 错误提示: 密码输入错误 * 3次错误提示 :   该卡面已经三次输入错误,该卡已被没收,请与工行及时联系处理! 返回账号输入界面! */public boolean passInput() {final int count=0;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统,请输入您的密码:\n"+"**********************************");String pass;Scanner sc=new Scanner(System.in);accountpassword=sc.next();if(accountpassword.equals("666666")){System.out.println("密码输入正确!");return true;}else{return false;}}/* * 用户主界面输出 *  */public void mainATM() {System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+" 1、存款\n 2、取款\n 3、转账汇款\n 4、修改密码\n 5、查询余额\n"+"**********************************");}/* * 定义存款功能方法 */@SuppressWarnings("unlikely-arg-type")public void moneyInput() {System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统,请输入存款金额:\n"+"**********************************");int money;int flag=0;boolean Flag=true;Scanner sc=new Scanner(System.in);money=sc.nextInt();if(money<=0){System.out.println("金额应该为不为0的正整数,请检查后重新输入!");}else {System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************"+"当前账户存款操作成功!"+"当前账户余额为:"+(accountbalance+money)+"元");System.out.print("请输入1进入账号界面!");String ID="88888888";   //ID    8位账号while(true) {flag=sc.nextInt();if(flag==1){idInput(accountID);System.out.println("**********************************");System.out.println("     欢迎使用中国工商银行自动柜员机系统        ");System.out.println("**********************************");System.out.println("          请输入你的账号               ");System.out.println("**********************************");while(true){ID=sc.next();Flag=idInput(ID);if(Flag==true)break;}break;}else{System.out.println("您输入的不是 1 ,请重新输入!!!");}  }   }}/* * 定义取款方法 *  *  */public void outaccount() {System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************"+"当前账户每日可以支取2万!\n"+"1、100元\n2、500元\n3、1000元\n4、1500元\n5/2000元\n6/5000元\n7、其他金额\n8、退卡\n9、返回\n"+"**********************************");int choice=999;Scanner sc=new Scanner(System.in);choice=sc.nextInt();int tempMoney;       //其他金额switch(choice){case 1:if(accountbalance<100)System.out.println("账户余额不足!");else{accountbalance-=100;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作100元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}break;case 2:if(accountbalance<500)System.out.println("账户余额不足!");else{accountbalance-=500;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作500元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}break;case 3:if(accountbalance<1000)System.out.println("账户余额不足!");else{accountbalance-=1000;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作1000元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}break;case 4:if(accountbalance<1500)System.out.println("账户余额不足!");else{accountbalance-=1500;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作1500元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}    break;case 5:if(accountbalance<2000)System.out.println("账户余额不足!");else{accountbalance-=2000;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作2000元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}break;case 6:if(accountbalance<5000)System.out.println("账户余额不足!");else{accountbalance-=5000;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作5000元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}break;case 7:System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"请输入取款功能:"+"**********************************\n");tempMoney=sc.nextInt();if(accountbalance<tempMoney)System.out.println("账户余额不足!");else{accountbalance-=tempMoney;System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"当前账户取款操作tempMoney元完成!\n当前账户余额为:"+accountbalance+"元\n"+"**********************************");}break;case 8:System.exit(1);break;case 9:mainATM();break;} }/* *  * 定义修改密码方法 *  */public void changePass() {String afterPass1;     //修改后的密码!String afterPass2; String beforePass;    //修改前的密码!Scanner sc=new Scanner(System.in);System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"**********************************\n"+"请输入当前密码:");while(true){beforePass=sc.next();if(beforePass.equals(getAccountpassword())) {while(true){System.out.println("请输入修改密码:");afterPass1=sc.next();System.out.println("请输入确认密码:");afterPass2=sc.next();if(afterPass1.equals(afterPass2)) {System.out.println("**********************************\n"+"欢迎"+accountID+"使用中国工商银行自动柜员机系统\n"+"当前密码修改成功!"+"**********************************");break;}else {System.out.println("您两次输入的密码不一致,请重新输入!");}}break;}else {System.out.println("当前密码输入错误!请重新输入!");}}}}

posted @ 2020-09-22 23:34  靠谱杨  阅读(205)  评论(0编辑  收藏  举报