一道比较基础的练习题
1.Account类
/*账户类*/
public class Account {
private long id; // 账户id
private double balance; // 账户余额
private String password; // 账户密码
public Account() {
}
public Account(long id, String password) {
this.id = id;
this.password = password;
}
// 构造方法
public Account(long id, double balance, String password) {
this.id = id;
this.balance = balance;
if (password.length() == 6) {
this.password = password;
}else {
System.out.println("密码必须是6位数!请重试~");
}
}
// 封装
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword(String glymm) {
//设一个管理员密码,ATM机器上默认有密码
if (glymm.equals("fuckyoujava")){
return password;
}else {
System.out.println("密码错误!已报警!");
return null;
}
}
//设置密码
public void setPassword(String password) {
if (password.length() == 6) {
this.password = password;
}else {
System.out.println("密码必须是6位数!请重试~");
}
}
2.CreditAccount类
/*信用账户类*/
public class CreditAccount extends Account{
private double creditLine; //信用额度
public CreditAccount() {
super();
}
public CreditAccount(long id, String password) {
super(id, password);
}
//封装
public double getCreditLine() {
return creditLine;
}
public void setCreditLine(double creditLine) {
this.creditLine = creditLine;
}
}
3.SavingAccount类
/*储蓄账户类*/
public class SavingAccount extends Account{
private double interestRate; //存款利率
public SavingAccount(long id, String password) {
super(id, password);
}
//封装
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
if (interestRate > 0 && interestRate < 0.1) {
this.interestRate = interestRate;
}else {
System.out.println("利率要在10%以内!请重试~");
}
}
}
4.Bank类
public class Bank {
// 开户方法
public static Account openAccount(long id, String password, int type) {
switch (type) {
case 0:
System.out.println("开户成功!您的账户id:" + id + ",密码:****** , 账户类型:" + "普通账户");
return new Account(id, password);
case 1:
System.out.println("开户成功!您的账户id:" + id + ",密码:****** , 账户类型:" + "储蓄账户");
return new SavingAccount(id, password);
case 2:
System.out.println("开户成功!您的账户id:" + id + ",密码:****** , 账户类型:" + "信用账户");
return new CreditAccount(id, password);
default:
System.out.println("请输入0-2开户哦!");
return null;
}
}
// 存款
public static double deposit(Account a, double amount) {
int count = 5;
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入银行密码:");
String psw = sc.nextLine();
if (a.getPassword("fuckyoujava").equals(psw)) {
a.setBalance(a.getBalance() + amount);
System.out.println("存款" + amount+"成功!您当前的账户余额:" + a.getBalance());
return a.getBalance();
} else {
System.out.println("密码错误!请重试");
count--;
if (count == 0) {
System.out.println("密码错误次数过多,账户被锁定!请联系柜台人员!");
return -1;
}
}
}
}
// 取款
public static double withdraw(Account a, double amount) {
int count = 5;
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入银行密码:");
String psw = sc.nextLine();
if (a.getPassword("fuckyoujava").equals(psw)) {
if (a.getBalance() <= amount) {
a.setBalance(a.getBalance() - amount);
System.out.println("取款"+ amount +"成功!您当前的账户余额:" + a.getBalance());
return a.getBalance();
}else {
System.out.println("余额不足,取款失败!");
return -1;
}
} else {
System.out.println("密码错误!请重试");
count--;
if (count == 0) {
System.out.println("密码错误次数过多,账户被锁定!请联系柜台人员!");
return -1;
}
}
}
}
}
* --- Be Humble and Hungry ---*