永发信息网

关于用JAVA编程ATM模拟程序问题

答案:2  悬赏:30  手机版
解决时间 2021-12-19 20:25
  • 提问者网友:爱了却不能说
  • 2021-12-19 15:36
ATM柜员机模拟程序
要求:使用图形用户界面。当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
(1)查询余额功能:初始余额为3000元
(2)ATM取款功能:每次取款金额为100的倍数,总额不超过2000元,支取金额不允许透支。
(3)ATM存款功能:不能出现负存款。
(4)修改密码:新密码长度不小于4位,不允许出现4位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成功修改密码。
急~~~
最佳答案
  • 五星知识达人网友:低血压的长颈鹿
  • 2021-12-19 16:36


public class ATM {

private Account acc;

private File dataFile;
private FileWriter fw;
private BufferedWriter bw;

private String filePath = "./data.txt";

public ATM() {
this.acc = new Account();
try {
this.dataFile = new File(this.filePath);
if (!this.dataFile.exists()) {
this.dataFile.createNewFile();
}
this.fw = new FileWriter(this.filePath);
this.bw = new BufferedWriter(this.fw);
} catch (IOException io) {
System.err.println("Cannot open file");
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new ATM().interact();
}

public void interact() {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Account #: ");
String temp = br.readLine();
System.out.println("Password: ");
String temp2 = br.readLine();
if (!this.acc.isValid(Long.parseLong(temp.trim()), temp2.trim()) {
System.err.println("Wrong password");
return;
}
System.out.println("1. Account Inquery.");
System.out.println("2. Withdraw");
System.out.println("3. Deposit.");
System.out.println("4. Change Password.");
System.out.println("5. Export to File.");
System.out.println("0. Exit.");
int c = 100;
while (c != 0) {
String str = br.readLine();
try {
int c = Integer.parseInt(str.trim());
} catch (NumberFormatException nfe) {
System.err.println("Invalid choice");
continue;
}
switch (c) {
case 0:
System.out.println("Thank you");
break;
case 1:
System.out.println("Balance: " + this.acc.balanceInquery());
break;
case 2:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.withdraw(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 3:
System.out.println("How much? ");
String temp = br.readLine();
try {
long ammount = Long.parseLong(temp.trim());
this.acc.deposit(ammount);
break;
} catch (NumberFormatException nfe) {
System.err.println("Invalid amount");
continue;
}
case 4:
System.out.println("Old password: ");
String temp = br.readLine();
System.out.println("New password: ");
String temp2 = br.readLine();
this.acc.changePassword(temp, temp2);
break;
case 5:
this.bw.write(this.acc.toString());
break;
default:
break;
}
}
}

}

class Account {

private long accNo = 123456;
private String pass = "123456";
private long balance = 10000;

public Account() {

}

public boolean isValid(long accNo, String pass) {
return (this.accNo == accNo) && (pass.equals(this.pass));
}

public void changePassword(String oldPass, String password) {
if (!oldPass.equals(this.pass)) {
System.err.println("Wrong password.");
return;
}
if (password.length < 6) {
System.err.println("Password too short");
return;
}
if (password.equals(this.pass)) {
System.err.println("Password cannot be the same.");
return;
}
this.pass = password;
}

public long balanceInquery() {
return this.balance;
}

public void withdraw(long amount) {
if (amount > 5000 || amount < 0) {
System.err.println("Withdraw limit: $0-$5000");
return;
}
if ((amount % 100) != 0) {
System.err.println("The amount has to be a product of 100");
return;
}
long newBalance = this.balance - amount;
if (newBalance < 0) {
System.err.println("Not enough money in the account");
return;
}
this.balance = newBalance;
}

public void deposit(long amount) {
if (amount < 0) {
System.err.println("Cannot deposit negative amount");
return;
}
this.balance += amount;
}

public String toString() {
return ("Account #: " + this.accNo + "\n" + "Password: " + this.pass + "\n" + "Balance: " + this.balance);
}
}
全部回答
  • 1楼网友:掌灯师
  • 2021-12-19 18:13

#include<iostream.h> class consumer; class atm // atm取款机 { public: atm(consumer& cn):cnsm(cn) { } void welcome(); // 登陆界面 bool check_passwd(char n[],char pwd[]); // 核对密码 void change_passwd(); // 修改密码 void fetchmoney(); // 取款 void information(); // 查询信息 void exitatm(); // 退出系统 void functionshow(); // 功能界面 void lock(); // 锁机 private: int times; // 记录密码输入次数 consumer& cnsm; }; class consumer // 用户 { public: friend class atm; consumer(char name[],char num[], float money,char password[]); protected: char* get_name(); // 取得姓名 char* get_num(); // 取得卡号 char* get_passwd(); // 取得密码 float get_money(); // 取得余额 void set_passwd(char pwd[]); // 设置密码 void set_money(float m); // 取钱 private: char passwd[8]; // 用户密码 char name[20]; // 用户姓名 char num[20]; float money; };

#include"function.h" #include<string.h> consumer::consumer(char name[], char num[],float money,char password[]) { strcpy(name,name); strcpy(num,num); money=money; strcpy(passwd,password); } float consumer::get_money() { return money; } char* consumer::get_name() { return name; } char* consumer::get_num() { return num; } char* consumer::get_passwd() { return passwd; } void consumer::set_money(float m) { money-=m; } void consumer::set_passwd(char pwd[]) { strcpy(passwd,pwd); }

#include "function.h" #include <string.h> #include<stdlib.h> void atm::welcome() { times=0; cout<<"$ 欢迎使用若雪银行atm自动取款机!~! "<<endl; char pwd[8],num[20],ch; int i=0; do { i=0; cout<<endl<<"请输入卡号:"; do { cin.get(ch); num[i++]=ch; }while(ch!='\n'); num[i-1]='\0'; i=0; cout<<"请输入密码:"; do { cin.get(ch); pwd[i++]=ch; }while(ch!='\n'); pwd[i-1]='\0'; if(!check_passwd(num,pwd)) { cout<<"你输入的卡号或密码有误, 请重新输入"<<endl; times++; } else { functionshow(); } }while(times<3); lock(); } bool atm::check_passwd(char num[], char pwd[]) { if(strcmp(num,cnsm.get_num())==0&&strcmp (pwd,cnsm.get_passwd())==0) return true; else return false; } void atm::functionshow() { int n; do { cout<<endl<<"请你输入相应的操作序号进行操作: "<<endl; cout<<"1) 修改密码 "<<endl <<"2) 取款 "<<endl <<"3) 查询余额 "<<endl <<"4) 退出系统 "<<endl; cout<<"$ >\\"; cin>>n; while(n<1||n>4) { cout<<"请输入正确的操作序号!"<<endl; cout<<"$ >\\"; cin>>n; } switch(n) { case 1: change_passwd(); break; case 2: fetchmoney(); break; case 3: information(); break; case 4: exitatm(); break; } }while(true); } void atm::change_passwd() { char pwd[8],repwd[8]; times=0; do { cout<<endl<<"请输入旧密码:"; cin>>pwd; if(!check_passwd(cnsm.get_num(),pwd)) times++; else break; }while(times<3); if(times==3) lock(); int t=0; do { cout<<"请输入新密码:"; cin>>pwd; cout<<"请再输入一次新密码:"; cin>>repwd; if((t=strcmp(pwd,repwd))!=0) cout<<"你输入的两次密码不一样, 请重新输入!"<<endl; }while(t!=0); cnsm.set_passwd(pwd); cout<<"密码修改成功,请牢记!"<<endl; } void atm::fetchmoney() { float m; char ch; do { cout<<endl<<"你要取多少钱:" <<"\n$>\\"<<endl ; cin>>m; while(m<=0) { cout<<"请输入正确的数字!"<<endl; cout<<"$ >\\ "; cin>>m; } if(cnsm.get_money()-m<0) { cout<<"对不起,你的余额不足!" <<endl; } else { cout<<endl<<"操作成功,请收好钱!" <<endl; cnsm.set_money(m); } cout<<"是否要继续该项操作:(y/n) " <<endl; cout<<"$ >\\ "; cin>>ch; while(ch!='n'&&ch!='n'&&ch!='y'&&ch!='y') { cout<<"$ >\\"; cin>>ch; } }while(ch=='y'||ch=='y'); } void atm::information() { cout<<"**********************************"<<endl; cout<<"*"<<endl; cout<<"* 用户姓名:"<<cnsm.get_name()<<endl; cout<<"* 卡号: "<<cnsm.get_num()<<endl; cout<<"* 余额: "<<cnsm.get_money()<<endl; cout<<"**********************************"<<endl; } void atm::lock() { cout<<endl<<"对不起,由于你的操作有误, 你的卡已经被没收! "<<endl; exit(1); } void atm::exitatm() { cout<<endl<<"感谢你对本银行的支持, 欢迎下次光临!"<<endl; cout<<"请取卡……"<<endl; exit(0); }

#include<iostream.h> #include"function.h" void main() { consumer c1("jim","12345",5200.3f,"123"); // 先构造一个用户 atm atm(c1); atm.welcome(); }

用c++语言也行的!

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯