程序1.
//时间:2013.11.07
//功能:创建账户Account 类 并且把它的信息输出出来
public class AccountShow {
public static void main (String[] args) {
Account account = new Account(1122, 20000); //创建一个对象,ID为1122,余额20000
Account.setAnnualInterestRate(4.5);//设置年利率 4.5%
account.withdraw(2500);//取款 2500
account.deposit(3000);//存款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 {//创建Account类
private int id;//创建Account的成员及方法
private double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
public Account() { //创建无参构造方法
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance) { //创建有id,余额 的构造方法
id = newId;
balance = newBalance;
dateCreated = new java.util.Date();
}
public int getId() {//创建getId()方法
return this.id;
}
public double getBalance() {//创建getBalance()方法
return balance;
}
public static double getAnnualInterestRate() {//创建getAnnualInterestRate()方法
return annualInterestRate;
}
public void setId(int newId) { //创建setId(int newId)方法
id = newId;
}
public void setBalance(double newBalance) {//创建setBalance()方法
balance = newBalance;
}
//创建setAnnualInterestRate(double newAnnualInterestRate)方法
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest() {//创建getMonthlyInterest()方法
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {//创建getDateCreated() 方法
return dateCreated;
}
public void withdraw(double amount) {//创建withdraw(double amount)方法
balance -= amount;
}
public void deposit(double amount) {//创建deposit(double amount)方法
balance += amount;
}
}