1.文件Account.java包含一个描述银行帐户的类的部分代码。把它保存到您的目录并研究它,看它有哪些方法。然后按照如下所述完成这个Account类。注意,你还不能测试你的方法直到你在问题2中写下ManageAccount。
a、填充toString方法的代码,它应该返回一个字符串,包含姓名,帐号,和账户余额
b、填充方法chargeFee的代码,这应该从帐户中扣除服务费(这个的意思是,每次取钱都要支付一定的服务费,或者叫手续费)。
c、修改chargeFee方法的代码,使它不是返回void,而是返回一个新的账户余额。注意,你必须在两个地方做出修改。
d、填写changeName方法的代码,使得它将一个字串作为参数,同时改变帐户上的名字为该字符串。
2、ManageAccounts.java文件包含一个外壳程序,使用上面定义的帐户类。将它保存到你的目录,按以下注视要求完成这个程序。
3、修改ManageAccounts的代码,使得在调用chargeFees后,输出账户余额。不是像在存款和取款后使用getBalance方法进行输出,而是使用从chargeFees方法返回的余额。你可以把余额存在一个变量中,然后通过这个变量进行输出,也可以把这个方法直接嵌入在一个println语句中。
// **************************************************
// Account.java author:wang
//
// A bank account class with methods // change the name on, charge a fee
// **************************************************
import java.text.NumberFormat;
public class Account
{
private double balance;
private String name;
private long acctNum;
//----------------------------------------------
//Constructor -- initializes balance, //----------------------------------------------
public Account(double initBal, String owner,long number) {
balance = initBal;
name = owner;
acctNum = number;
}
//----------------------------------------------
// Checks to see if balance is sufficient // If so, decrements balance by amount; //----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient " +amount);
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
NumberFormat fmt =NumberFormat.getCurrencyInstance();
return acctNum + "t" + "name" +"t" +fmt.format(balance);
}
//----------------------------------------------
// Deducts $10 service fee
//----------------------------------------------
public void chargeFee(double fee)
{
balance=balance-10;
}
//----------------------------------------------
// Changes the name on the account
//----------------------------------------------
public void changeName(String newName)
{
name = newName;
}
}
// ****************************************************************
// ManageAccounts.java
//
// Use Account class to create and manage Sally and Joe's
// bank accounts
// ****************************************************************
public class ManageAccounts {
public static void main(String[] args) {
Account acct1, acct2;
//create account1 for Sally with $1000
acct1 = new Account(1000, "Sally", 1111);
//create account2 for Joe with $500
acct2 = new Account(500, "Joe", 2222);
//deposit $100 to Joe's account
acct2.deposit(100);
//print Joe's new balance (use getBalance())
System.out.println("Joe balance after deposit" + acct2.getBalance());
//withdraw $50 from Sally's account
acct1.withdraw(50);
//print Sally's new balance (use getBalance())
System.out.println("Sally balance after withdraw" + acct1.getBalance());
//charge fees to both accounts
acct1.chargeFee(10);
acct2.chargeFee(10);
//change the name on Joe's account to Joseph
acct2.changeName("Joseph");
//print summary for both accounts
System.out.println(acct1);
System.out.println(acct2);
}
}


