【java入门】第十一章 继承与多态 练习题 新的Account类

11.8 新的Account类

先补充9.7的Account类:
在这里插入图片描述
在这里插入图片描述
代码:

public class Exercise09_07 {
    
    

	public static void main(String[] args) {
    
    
		//创建一个账户对象
		Account account = new Account(1122,20000);
		account.setAnnualInterestRate(4.5);
		//使用withdraw取款
		account.withDraw(2500);
		//存款
		account.deposit(3000);
		//打印
		System.out.println("Balance is "+account.getBalance());
		System.out.println("Monthly interest is "+account.getMonthlyInterest());
		System.out.println("This account was created at "
				+account.getDateCreated());
	}

}

class Account{
    
    
	private int id;
	private double balance;
	private double annualInterestRate=0;
	private java.util.Date dateCreated;//注意此处要使用Date的包
	
	//创建默认账户的无参构造方法,名字和类一样
	public Account(){
    
    
		dateCreated = new java.util.Date();
	}
	//带参构造方法
	public Account(int id,double balance){
    
    
		this.id = id;
		this.balance =balance;//余额
		dateCreated = new java.util.Date();
	}
	//访问器和修改器get和set
	public int getId() {
    
    
		return id;
	}
	public void setId(int id) {
    
    
		this.id = id;
	}
	public double getBalance() {
    
    
		return balance;
	}
	public void setBalance(double balance) {
    
    
		this.balance = balance;
	}
	public double getAnnualInterestRate() {
    
    
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
    
    
		this.annualInterestRate = annualInterestRate;
	}
	//dateCreated的访问器
	public java.util.Date getDateCreated() {
    
    
		return dateCreated;
	}
	//返回月利率
	public double getMonthlyInterest() {
    
    
		return balance*(annualInterestRate/1200);
	}
	
	//withDraw从账户提取特定数额
	public void withDraw(double amount) {
    
    
		balance-=amount;
	}
	//deposit向账户存储特定数额
	public void deposit(double amount) {
    
    
		balance +=amount;
	}
	
}

11.8题目要求:
在这里插入图片描述
代码:

public class Exercise11_08 {
    
    

	public static void main(String[] args) {
    
    
		//创建一个账户对象
		Account account = new Account("George",1122,20000);
		account.setAnnualInterestRate(5.5);
		//存款
		account.deposit(30);
		account.deposit(40);
		account.deposit(50);
		//使用withdraw取款
		account.withDraw(5);
		account.withDraw(4);
		account.withDraw(2);
		
		//打印
		System.out.println("Name "+account.getName());
		System.out.println("Annual interest rate: "+account.getAnnualInterestRate());
		System.out.println("Balance is "+account.getBalance());
		
		java.util.ArrayList list = account.getTransactions();
		
		//打印清单
		System.out.printf("%-35s%-15s%-15s%-15s\n","Date","Type","Account","Balance");
		for(int i=0;i<list.size();i++) {
    
    
			Transaction transaction = (Transaction)(list.get(i));
			System.out.printf("%-35s%-15s%-15s%-15s\n",transaction.getDate(),
					transaction.getType(),transaction.getAmount(),transaction.getBalance());
		}
	}

}

class Account{
    
    
	private int id;
	//新加的名字数据
	private String name;
	private double balance;
	private double annualInterestRate=0;
	private java.util.Date dateCreated;
	//新加的transactions用于为账户存储交易
	private java.util.ArrayList transactions = new java.util.ArrayList();
	
	public java.util.ArrayList getTransactions() {
    
    
		return transactions;
	}
	public void setTransactions(java.util.ArrayList transactions) {
    
    
		this.transactions = transactions;
	}
	//创建默认账户的无参构造方法,名字和类一样
	public Account(){
    
    
		dateCreated = new java.util.Date();
	}
	//带参构造方法
	public Account(String name,int id,double balance){
    
    
		this.id = id;
		this.name = name;
		this.balance =balance;//余额
		dateCreated = new java.util.Date();
	}
	//访问器和修改器get和set
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	public int getId() {
    
    
		return id;
	}
	
	public void setId(int id) {
    
    
		this.id = id;
	}
	public double getBalance() {
    
    
		return balance;
	}
	public void setBalance(double balance) {
    
    
		this.balance = balance;
	}
	public double getAnnualInterestRate() {
    
    
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
    
    
		this.annualInterestRate = annualInterestRate;
	}
	//dateCreated的访问器
	public java.util.Date getDateCreated() {
    
    
		return dateCreated;
	}
	//返回月利率
	public double getMonthlyInterest() {
    
    
		return balance*(annualInterestRate/1200);
	}
	
	//withDraw从账户提取特定数额
	public void withDraw(double amount) {
    
    
		balance-=amount;
		transactions.add(new Transaction('W',amount,balance," "));
	}
	//deposit向账户存储特定数额
	public void deposit(double amount) {
    
    
		balance +=amount;
		transactions.add(new Transaction('D',amount,balance," "));
	}
	
}

class Transaction{
    
    
	private java.util.Date date;
	private char type;
	private double amount;
	private double balance;
	private String description;
	
	public Transaction(char Type,double amount,double balance,String description) {
    
    
		date = new java.util.Date();
		this.type =type;
		this.balance = balance;
		this.description = description;
	}
	
	public java.util.Date getDate() {
    
    
		return date;
	}
	public char getType() {
    
    
		return type;
	}
	
	public double getAmount() {
    
    
		return amount;
	}
	
	public double getBalance() {
    
    
		return balance;
	}
	
	public String getDescription() {
    
    
		return description;
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_51669241/article/details/117106527