Java作业 - 模拟银行账户业务
作业题目:
模拟银行账户业务:编写Account类,添加带参方法实现存款和取款业务,开户时帐户初始金额为0元,取款时如果余额不足给出提示。
我好像把简单的问题复杂化了... :@(喜极而泣)
我的运行截图:
:@(喷水) 不知道要不要加啥卡号和密码,就直接加上了。
(借鉴没啥问题,别直接复制,不然查重就不好了) ::(狂汗)
代码如下:
Account类:
public class Account {
private String cardnumber;
private String password;
private double balance;
public double getBalance() {
return balance;
}
public String getCardnumber() {
return cardnumber;
}
public String getPassword() {
return password;
}
// 构造Account方法
public Account(String cardnumber, String password, double balance) {
this.cardnumber = cardnumber;
this.password = password;
this.balance = balance;
}
// 取款加款
public void ChangeBalance(double money) {
if (money >= 0) {
this.balance = balance + money;
System.out.println("存入成功!您的余额为:" + this.balance + "元");
} else if (money < 0 && this.balance + money >= 0) {
this.balance = balance + money;
System.out.println("取出成功!您的余额为:" + this.balance + "元");
} else {
System.out.println("账户余额不足!取款失败");
}
}
}
test类:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建user1对象,使用构造方法,卡号:100000000,密码:abc123,余额0
Account user1 = new Account("100000000", "abc123", 0);
Scanner in = new Scanner(System.in);
System.out.print("请输入您的卡号:");
if (in.next().equals(user1.getCardnumber())) {
System.out.print("请输入您的密码:");
if (user1.getPassword().equals(in.next())) {
System.out.print("验证成功!\n您的账户余额为:" + user1.getBalance() + "元\n");
for (int i = 0;;i++) {
// for循环进行服务直到用户结束跳出
// switch进行判断
System.out.println("请输入您要办理的业务(存款/取款/结束)");
switch (in.next()) {
case "存款":
System.out.print("请输入您的存款金额:");
Double addmoney = in.nextDouble();
if (addmoney > 0) {
user1.ChangeBalance(addmoney);
} else {
System.out.println("您输入的金额不合法");
}
break;
case "取款":
System.out.print("请输入您的取款金额:");
Double deletemoney = in.nextDouble();
if (deletemoney > 0) {
user1.ChangeBalance(-deletemoney);
} else{
System.out.println("您输入的金额不合法");
}
break;
case "结束":
System.out.println("欢迎再次光临~");
return;
}
}
} else
System.out.println("密码错误!");
} else {
System.out.println("卡号不存在!");
}
in.close();
}
}