实验9:银行账户存取款业务
题目类别: 实验
关键字: 掌握继承、覆盖、多态、对象类型转换、ArrayList及范型
内容要求:
要求编写程序模拟银行账户的存、取款 *** 作。按要求完成以下步骤:
步骤 1:编写程序 Account.java,其中定义银行账户类 Account。该类中有账号、姓名、存款余额等数据域,余额默认是 0;有存款、取款、获取当前余额等方法。其中账号为长度为 12 位数字的字符串,姓名为字符串,存款余额为 double。
步骤 2:编写名为 CreditAccount 类的xyk账户类。该类继承自 Account 类,增加一个透支限额(double)数据域,透支限额默认为 1000。同时该类账户取款时允许透支,但不能超过透支限额。
步骤 3:编写名为 SavingAccount 的储蓄账户类 SavingAccount。该类继承自 Account 类。该类账户取款时不允许透支。
步骤 4:编写名为 Bank 的模拟银行类,
使用线性表定义一个数据域存储多个类型可能是xyk账户或储蓄账户的对象,建议使用ArrayList,注意使用范型;对数据域进行封装。该类包含以下方法:
开户:即增加一个新的账户,注意:不允许两个账户的账号相同
销户:即删除一个已有的账户
查询账户:根据一个账号,查询有无该账号的账户
统计目前银行的存款总余额的方法。
统计目前银行的xyk账户总透支金额的方法。
统计目前总账户数。
统计目前xyk账户数
统计目前储蓄卡账户数
步骤 5:编写客户端类 Client.java 完成以下功能:
编写一个静态方法,创建一个银行对象,并随机生成 10 个账号从 1000 0000 0000
0000 到 1000 0000 0000 0009、类型不同的账户。
main 方法中模拟几次开户 *** 作。
main 方法中模拟几次销户 *** 作。
模拟几个对指定账号的存款和取款 *** 作。
输出银行的总账户数、总余额、总透支数、各类具体账户数。
提交:打包为可以执行的JAR文档,其中要包含源程序文件。
Account 类package Main; public class Account { private String name; private String account; private double Money = 0; public Account(String account) { this.account = account; } public Account() { } public void setName(String name) { this.name = name; } public void setAccount(String account) { this.account = account; } public void setMoney(double money) { Money = money; } public String getName() { return name; } public String getAccount() { return account; } public double getMoney() { return Money; } public void saveMoney(double m) { this.Money += m; } public void takeMoney(double m) { this.Money -= m; } }bank类
package Main; import java.util.ArrayList; public class Bank { private ArrayList accountArrayList = new ArrayList<>(); public void setAccountArrayList(ArrayList accountArrayList) { this.accountArrayList = accountArrayList; } public ArrayList getAccountArrayList() { return accountArrayList; } public int getAccount(String theAccount){ //取得账号 for (int i = 0; i < accountArrayList.size(); i++) { if (accountArrayList.get(i).getAccount().equals(theAccount)) { return i; } } return -1; } public int getAccountType(String theAccount){ for (Account account : accountArrayList) { if (account.getAccount().equals(theAccount)) { if (account.getClass().getName().equals(CreditAccount.class.getName())) { return 1; } else return 0; } } return 0; } public void addNewAccount(Account newOne) { //开户:即增加一个新的账户,注意:不允许两个账户的账号相同 for (Account account : accountArrayList) { if (account.getAccount().equals(newOne.getAccount())) { //System.out.println("存在相同账号"); return; } } accountArrayList.add(newOne); } public void deleteTheAccount(String deleteOne) { //销户:即删除一个已有的账户 for (int i = 0; i < accountArrayList.size(); i++) { if (accountArrayList.get(i).getAccount().equals(deleteOne)) { accountArrayList.remove(i); return; } } } public boolean isExistTheAccount(Account checkOne) { //查询账户:根据一个账号,查询有无该账号的账户 for (Account account : accountArrayList) { if (account.getAccount().equals(checkOne.getAccount())) { //System.out.println("存在相同账号"); return true; } } return false; } public double getSumMoney() { //统计目前银行的存款总余额的方法。 double sum = 0; for (Account account : accountArrayList) { sum += account.getMoney(); } return sum; } public double getAllOverdraftLimit(){ double sum=0; //统计目前银行的信用卡账户总透支金额的方法 for (Account account : accountArrayList) { if (account.getClass().getName().equals(CreditAccount.class.getName())) { if(account.getMoney()<0) sum += account.getMoney(); } } return sum; } public int getAccountNumber() { //统计目前总账户数 return accountArrayList.size(); } public int getCreditAccountNumber() { //统计目前信用卡账户数 int sum = 0; for (Account account : accountArrayList) { if (account.getClass().getName().equals(CreditAccount.class.getName())) { sum++; } } return sum; } public int getSavingAccountNumber() { //统计目前储蓄卡账户数 int sum = 0; for (Account account : accountArrayList) { if (account.getClass().getName().equals(SavingAccount.class.getName())) { sum++; } } return sum; } }Client类
package Main; import java.util.Random; import java.util.Scanner; public class Client { public static void main(String[] args) { Random random = new Random(); Bank bank = new Bank(); String[] accountNum = {"100000000000", "100000000001", "100000000002", "100000000003", "100000000004", "100000000005", "100000000006", "100000000007", "100000000008", "100000000009" }; System.out.println("随机生成 10 个账号"); int f1 = 0, f2 = 0; for (int i = 0; i < 10; i++) { boolean t = random.nextBoolean(); if (t) { f1++; bank.addNewAccount(new CreditAccount(accountNum[i])); } else { f2++; bank.addNewAccount(new SavingAccount(accountNum[i])); } } System.out.println("信用卡数=" + f1 + " 储蓄卡数=" + f2); System.out.println("------------------------------------------"); System.out.print("输入模拟几次开户操作:"); Scanner reader = new Scanner(System.in); int creativeNum = reader.nextInt(); for (int i = 0; i < creativeNum; i++) { System.out.print("请选择开户类型(1)信用卡,(2)储蓄卡:"); int t = reader.nextInt(); String accountName = (long) ((random.nextDouble()) * Math.pow(10, 12)) + ""; while (bank.isExistTheAccount(new Account(accountName))){ accountName = (long) ((random.nextDouble()) * Math.pow(10, 12)) + ""; } if (t==1) { System.out.println("信用卡 账号: " + accountName); bank.addNewAccount(new CreditAccount(accountName)); } else { System.out.println("储蓄卡 账号: " + accountName); bank.addNewAccount(new SavingAccount(accountName)); } //System.out.println(bank.getAccountArrayList().get(i + 10).getClass().getName() + " " + bank.getAccountArrayList().get(i + 10).getAccount() + "n"); } System.out.println("------------------------------------------"); System.out.print("输入模拟几次销户操作:"); int destroyNum = reader.nextInt(); while (destroyNum < 0 || destroyNum > bank.getAccountArrayList().size()) { System.out.print("输入次数越界请重新输入:"); destroyNum = reader.nextInt(); } boolean deFlag; for (int i = 0; i < destroyNum; i++) { System.out.print("请输入删除的账号:"); String destroyAccount = reader.next(); deFlag = bank.isExistTheAccount(new Account(destroyAccount)); if (!deFlag) { System.out.println("不存在此账号,请重新输入"); i--; }else { bank.deleteTheAccount(destroyAccount); System.out.println("删除成功n"); } } System.out.println("------------------------------------------"); System.out.println("对指定账号的存款和取款"); int op; String name; int flag = 0; double money; int type; while (true) { flag=0; System.out.println("n请输入账号:(输入0退出)"); name = reader.next(); if (name.equals("0")) { break; } if (!bank.isExistTheAccount(new Account(name))) { System.out.println("不存在此账号,请重新输入!"); continue; } int theAccountNum = bank.getAccount(name); type = bank.getAccountType(name); System.out.print("账户类型:"); double limit=0; if(type==1){ System.out.println("信用卡"); limit = ((CreditAccount) bank.getAccountArrayList().get(theAccountNum)).getOverdraftLimit(); }else System.out.println("储蓄卡"); while (flag != 1) { System.out.println(); System.out.println("账户余额:" + bank.getAccountArrayList().get(theAccountNum).getMoney()); if(type==1){ System.out.println("当前可取款金额:"+(bank.getAccountArrayList().get(theAccountNum).getMoney()+ limit)); } System.out.println("请选择存款或取款"); System.out.println("输入”1“存款,输入“2”取款,输入“3”退出"); op = reader.nextInt(); switch (op) { case 1 -> { System.out.print("请输入存款金额:"); money = reader.nextDouble(); bank.getAccountArrayList().get(theAccountNum).saveMoney(money); } case 2 -> { System.out.println("请输入取款金额:"); money = reader.nextDouble(); if (type == 1) { if (money > bank.getAccountArrayList().get(theAccountNum).getMoney() + limit) { System.out.println("取款金额超出金额上限!取款失败"); break; } } else { if (money > bank.getAccountArrayList().get(theAccountNum).getMoney()) { System.out.println("取款金额超出金额上限!取款失败"); break; } } bank.getAccountArrayList().get(theAccountNum).takeMoney(money); } case 3 -> flag = 1; } } } System.out.println("银行的总账户数:" + bank.getAccountArrayList().size()); System.out.println("总余额:" + bank.getSumMoney()); System.out.println("总透支数:" +Math.abs( bank.getAllOverdraftLimit())); System.out.println("各类具体账户数:" + bank.getAccountNumber()); System.out.println("xyk账户:" + bank.getCreditAccountNumber()); System.out.println("储蓄卡账户:" + bank.getSavingAccountNumber()); //reader.close(); } }CreditAccount (继承Account)
package Main; public class CreditAccount extends Account { private double overdraftLimit = 1000; public CreditAccount(String account) { this.setAccount(account); } public double getOverdraftLimit() { return overdraftLimit; } }SavingAccount (继承Account)
package Main; public class SavingAccount extends Account{ public SavingAccount(String account) { this.setAccount(account); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)