一、实验目的
- 理解面向对象程序的基本概念。
- 掌握类的继承的实现机制。
- 熟悉类中成员的访问控制方法。
- 熟悉ArrayList类的使用。
二、实验内容、过程及结果
**11.8 (New Account class) An Account class was specified in Programming
VideoNote New Account class Exercise 9.7. Design a new Account class as follows: I Add a new data field name of the String type to store the name of the
customer.
Add a new constructor that constructs an account with the specified name, id, and balance.
11.1
Add a new data field named transactions whose type is ArrayList that stores the transaction for the accounts. Each transaction is an instance of the Transaction class. The Transaction class is defined as shown in Figure 11.6.
运行代码如下 :
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;
}
}
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;
}
}
运行结果
三、实验结论
通过本次实验我学会了在原有代码的基础上添加功能,得到了编程与逻辑思维息息相关的感悟,要想写优秀的代码,基础必须筑牢,头脑必须清醒,思维必须井井有条。
结语
有想法就去做
什么时候都不算晚
!!!