1.创建 banking 包
2. 在 banking 包下创建 Account 类。该类必须实现上述 UML 框图中的模型。
a. 声明一个私有对象属性:balance,这个属性保留了银行帐户的当前(或 即
时)余额。
b. 声明一个带有一个参数 (init_balance )的公有构造器 ,这个参数为
balance 属性赋值。
c. 声明一个公有方法 geBalance,该方法用于获取经常余额。
d. 声明一个公有方法 deposit,该方法向当前余额增加金额。
e. 声明一个公有方法 withdraw 从当前余额中减去金额。
3.打开TestBanking.java文件,按提示完成编写,并编译 TestBanking.java 文件。
4. 运行 TestBanking 类。可以看到下列输出结果:
Creating an account with a 500.00 balance
Withdraw 150.00
Deposit 22.50
Withdraw 47.62
The account has a balance of 324.88
package banking;
public class Account {
private static double balance; //这个属性保留了银行账户的当前余额
private double amt;
public Account(double init_balance){
this.balance=init_balance;
}
public double getBalance() {
return this.balance;
}
public void deposit(double amt){
balance+=amt;
System.out.println("Deposit "+amt+": true");
}
public boolean withdraw(double amt){
if(amt<balance){
balance-=amt;
System.out.println("Withdraw "+amt+": true");
return true;
}else{
System.out.println("Withdraw "+amt+": false");
return false;
}
}
}
package banking;
public class TestBanking {
public static void main(String[] args) {
Customer customer=new Customer("Jane","Smith");
//customer.setAccount(500.00);
Account admin=new Account(500.00);
System.out.println("Creating the customer "+customer.getFirstName()+" "+customer.getLastName());
System.out.println("Creating her account with a "+admin.getBalance()+" balance");
admin.withdraw(150.00);
admin.deposit(22.50);
admin.withdraw(47.62);
admin.withdraw(400.00);
System.out.println("Customer ["+customer.getLastName()+", "+customer.getFirstName()+"] has a balance of "+admin.getBalance());
}
}
package banking;
public class Customer {
private String firstName;
private String lastName;
private double account;
public Customer(String f,String l){
this.firstName=f;
this.lastName=l;
}
public String getFirstName(){
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public void setAccount(double account){
this.account=account;
}
public double getAccount(){
return this.account;
}
}