第六天的Bank作业时间:2022-04-26 06:45:02目录结构:如图所示 Account.java package model; public class Account { private long id;// 账户号码 长整数 private String password;// 账户密码 private String name;// 真实姓名 private String personId;// 身份证号码 字符串类型 private String email;// 客户的电子邮箱 private double balance;// 账户余额 public Account() { } public Account(long id, String password, String name) { this.id = id; this.password = password; this.name = name; } public Account(long id, String password, String name, String personId, String email, double balance) { this.id = id; this.password = password; this.name = name; this.personId = personId; this.email = email; this.balance = balance; } public double deposit(double money) { // this.balance += money; this.balance = this.balance + money; return this.balance; } public double withdraw(double money) { if (this.balance >= money) { this.balance -= money; } else { System.out.println("余额不足!~~"); } return this.balance; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPersonId() { return personId; } public void setPersonId(String personId) { this.personId = personId; } } CreditAccount.java package model; public class CreditAccount extends Account{ private double ceiling; public double getCeiling() { return ceiling; } public void setCeiling(double ceiling) { this.ceiling = ceiling; } public CreditAccount(){} public CreditAccount(long id, String password, String name,double ceiling){ super(id,password,name); this.ceiling = ceiling; } public CreditAccount(long id, String password, String name, String personId, String email, double balance,double ceiling) { super(id,password,name,personId,email,balance); this.ceiling = ceiling; } public double withdraw(double money) { double temp = this.getBalance(); if(temp+ceiling>=money){ this.setBalance(temp-money); System.out.println("欢迎下次再来!~~"); }else{ System.out.println("透支额度不足!~~"); } return this.getBalance(); } } SavingAccount.java package model; public class SavingAccount extends Account { public SavingAccount(){} public SavingAccount(long id, String password, String name){ super(id,password,name); } public SavingAccount(long id, String password, String name, String personId, String email, double balance) { super(id,password,name,personId,email,balance); } } Bank.java package contrl; import model.Account; import model.CreditAccount; import model.SavingAccount; public class Bank { private Account[] accounts = new Account[10000]; private int count = 0; public Account register(long id, String password1, String password2, String name, String personId, String email, int type){ if(count+1==accounts.length){ Account[] temp = new Account[2*accounts.length]; System.arraycopy(accounts, 0, temp, 0, accounts.length); accounts = temp; } Account account = null; if(password1.equals(password2)){ if(type==0){ account = new SavingAccount(id,password1,name,personId,email,0); accounts[count++] = account; }else if(type==1){ account = new CreditAccount(id,password1,name,personId,email,0,5000); accounts[count++] = account; }else{ System.out.println("该类型帐户不存在!~~"); } }else{ System.out.println("确认密码错误!~~"); } return account; } public Account login(long id,String password){ for (int i = 0; i < count; i++) { if(id==accounts[i].getId() &&password.equals(accounts[i].getPassword())){ System.out.println("欢迎光临!"); return accounts[i]; } } System.out.println("密码错误或者该账户不存在!"); return null; } /** * 用户存款 * @param id * @param money * @return */ public Account deposit(long id,double money){ for (int i = 0; i < count; i++) { //遍历所有用户来查询指定用户 if(id==accounts[i].getId()){ accounts[i].deposit(money); System.out.println("存款成功!"); return accounts[i]; } } System.out.println("密码错误或者该账户不存在!"); return null; } public Account withdraw(long id,double money){ for (int i = 0; i < count; i++) { if(id==accounts[i].getId()){ accounts[i].withdraw(money); return accounts[i]; } } System.out.println("密码错误或者该账户不存在!"); return null; } public Account select(long id){ for (int i = 0; i < count; i++) { if(id==accounts[i].getId()){ System.out.println("您的余额为:"+accounts[i].getBalance()); return accounts[i]; } } System.out.println("密码错误或者该账户不存在!"); return null; } public Account setCeiling(long id,double newCeiling){ for (int i = 0; i < count; i++) { if(id==accounts[i].getId()){ if(accounts[i] instanceof CreditAccount){ CreditAccount ca = (CreditAccount)accounts[i]; ca.setCeiling(newCeiling); System.out.println("信用额度设置成功!~~"); return ca; }else{ System.out.println("该用户不是信用账户!~~"); } } } return null; } /** * 遍历所有用户统计余额总数 * @return all 总余额 */ public double comBalance(){ double all = 0 ; for (int i = 0; i < count; i++) { all += accounts[i].getBalance(); } return all; } /** * 通过遍历所有用户后,判断用户的类型, * 如果是信用用户则累加他们的透支额度 * @return ceiling 所用信用用户的透支额度的综合 */ public double comCeiling(){ double ceilings = 0; for (int i = 0; i < count; i++) { if(accounts[i] instanceof CreditAccount){ CreditAccount ca = (CreditAccount)accounts[i]; ceilings += ca.getCeiling(); } } return ceilings; } } AccountTest.java package test; import model.Account; import contrl.Bank; public class AccountTest { public static void main(String[] args) { // Account account = new Account(123,"haoyz","haoyz", // "123","haoyz@tarena.com.cn",100000); // System.out.println("Account's name:"+account.getName()); // System.out.println("Account's balance:"+account.getBalance()); // System.out.println("Account's id:"+account.getId()); // SavingAccount sa = new SavingAccount(123,"haoyz","haoyz", // "123","haoyz@tarena.com.cn",0); // System.out.println("Account's name:"+sa.getName()); // System.out.println("Account's balance:"+sa.getBalance()); // System.out.println("Account's id:"+sa.getId()); // sa.withdraw(2000); // CreditAccount ca = new CreditAccount(123,"haoyz","haoyz", // "123","haoyz@tarena.com.cn",0,5000); // ca.withdraw(5000); Bank bank = new Bank(); Account account = bank.register(123,"haoyz","haoyz","haoyz", "123","haoyz@tarena.com.cn",1); account.deposit(200); account.withdraw(5000); System.out.println("Account's name:"+account.getName()); System.out.println("Account's balance:"+account.getBalance()); System.out.println("Account's id:"+account.getId()); bank.login(123, "haoyz"); bank.deposit(123, 5000); bank.withdraw(123, 10000); bank.select(123); bank.setCeiling(123, 50000); System.out.println("所有用户的余额总数是:"+bank.comBalance()); System.out.println("所有信用用户的透支额度的总数是:"+bank.comCeiling()); } }