1.写一个名为Account的类模拟账户。
该类的属性和方法如下所示。
该类包括的属性:账户id,余额balance,年利率annualInterestRate;
包含的方法:各属性的set和get方法。取款方法withdraw(),存款方法deposit()
2.写一个测试程序
(1)创建一个Customer,名字叫Jane Smith,他有一个账号为1000,余额为2000,年利率为1.23%的账户
(2)对Jane Smith操作:
存入100元,再取出960元,再取出2000。
打印Jane Smith的基本信息
信息如下显示:
成功存入:100
成功取出:960
余额不足,取钱失败
Customer [Smith,Jane] has a account :id is 1000 annualInterestRate is 1.23% balance is 1140.0
Customer.java
public class Customer{
//姓名
private String name;
//......
//Account ..
public Customer(){
}
public Customer(String n){
name = n;
}
public String getName(){
return name;
}
public void setName(String n){
name = n;
}
}
Account.java
public class Account{
//id
private String id;
//balance
private double balance;
//年利率
private String annualInterestRate;
public Account(){
}
public Account(String i,double b,String a){
id = i;
balance = b;
annualInterestRate = a;
}
public String getId(){
return id;
}
public double getBalance(){
return balance;
}
public String getAnnualInterestRate(){
return annualInterestRate;
}
public void setId(String i){
id = i;
}
public void setBalance(double b){
balance = b;
}
public void setAnnualInterestRate(String a){
annualInterestRate = a;
}
public void withdraw(double b){
if(balance - b < 0){
System.out.println("余额不足,取钱失败");
return;
}
balance -= b;
System.out.println("成功取出:" + b);
}
public void deposit(double b){
balance += b;
System.out.println("成功存入:" + b);
}
//public void sendImfomation(){
//}
}
AccountTest.java
public class AccountTest02{
public static void main(String[] args){
Customer c1 = new Customer("Jane Smith");
//System.out.println(c1.getName());
Account a2 = new Account("1000",2000,"1.23%");
a2.deposit(100);
a2.withdraw(960);
a2.withdraw(2000);
System.out.println("Customer[" + c1.getName() + "] has a account : id is " + a2.getId() + ", annualInterestRate is " + a2.getAnnualInterestRate() + ", balance is " + a2.getBalance());
}
}
运行结果: