要求:使用字符用户界面。当输入给定的卡号和密码(初始卡号和密码为123456)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行:
1、查询余额:初始余额为10000元
2、ATM取款:每次取款金额为100的倍数,总额不超过5000元清滑,支取金额不允许透支。
3、ATM存款:不能出现负存款。
4、修改密码:新密码长度不缓正芦小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求,且两次输入相同的情况下才可以成扰带功修改密码。
(卡号密码余额放到文件中)
*/
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)
}
}
请采纳答案,支持我一下。
密码功能已实现。试试吧。用户:lorin ,密码: 123import java.util.Scanner
public class ATM1 {
Scanner sc = new Scanner(System.in)
private String name = "lorin"
private double password = 123
private double money = 500
public void aloha(){
System.out.println("***********************")
System.out.println("* *")
System.out.println("* 欢迎使用ATM *")
System.out.println("* *")
System.out.println("* 制作人:lorin *")
System.out.println("* *")
System.out.println("* *")
System.out.println("************************")
System.out.println("请输入用户名:")
String s = sc.next()
if(s.equals("lorin")) {
System.out.println("请输入密码:")
int ss = sc.nextInt()
if(ss == 123) {
land()
}else{
System.out.println("密码错罩蚂误,系统退出")
System.exit(0)
}
}else{
System.out.println("用户名错误,系统退出")
System.exit(0)
}
}
//登陆页面
public void land(){
System.out.println("请选择 *** 作界面:1取款,2存款,3查询,4转账,5退出")
int a = sc.nextInt()// 请加入此句
switch(a){
case 1 : // 去掉‘’ 因为a 为int型。胡闷历
get()
break
case 2 :
set()
break
case 3 :
query()
break
case 4 :
divert()
break
case 5 :
SetOut()
break
}
}
//取款
public void get(){
System.out.println("请输入取款金额:")
int a = sc.nextInt()
if(a>money){
System.out.println("余额不足!")
}else{
money-=a
System.out.println("当前余额为:"+money)
land()
}
}
//存款
public void set(){
System.out.println("请输入存款金额:")
int a = sc.nextInt()
money+=a
System.out.println("当前余额为:裤搜"+money)
land()
}
//查询
public void query(){
System.out.println("当前余额为:"+money)
land()
}
//转账
public void divert(){
System.out.println("该功能站未开放!")
land()
}
//退出
public void SetOut(){
System.exit(0)
}
public static void main(String[] args) {
ATM1 atm = new ATM1()
atm.aloha()
}
}
求采纳为满意回答。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)