2011-04-04 218 views
0
//******************************************************* 
// Account.java 
// 
// A bank account class with methods to deposit to, withdraw from, 
// change the name on, charge a fee to, and print a summary of the account. 
//******************************************************* 
import java.text.NumberFormat; 

public class Account 
{ 
    private double balance; 
    private String name; 
    private long acctNum; 

    //---------------------------------------------- 
    //Constructor -- initializes balance, owner, and account number 
    //---------------------------------------------- 
    public Account(double initBal, String owner, long number) 
    { 
    balance = initBal; 
    name = owner; 
    acctNum = number; 
    } 

    //---------------------------------------------- 
    // Checks to see if balance is sufficient for withdrawal. 
    // If so, decrements balance by amount; if not, prints message. 
    //---------------------------------------------- 
    public void withdraw(double amount) 
    { 
    if (balance >= amount) 
     balance -= amount; 
    else 
     System.out.println("Insufficient funds"); 
    } 

    //---------------------------------------------- 
    // 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 double chargeFee() 
    { 
    balance=balance-10; 
    return balance; 
    } 

    //---------------------------------------------- 
    // Changes the name on the account 
    //---------------------------------------------- 
    public void changeName(String newName) 

    { 
    name=String.toString(newName); 
    } 

} 

我需要该程序的最后部分的帮助//更改帐户上的名称。我需要将它作为一个字符串(名称)作为参数,并将其更改为新字符串(newName),那么正确的语法是什么?我无法在我的书中找到它。字符串作为参数

+0

为什么要调用String.toString(newName),如果newName已经是一个字符串?它应该是name = newName; – Andre 2011-04-04 19:22:37

回答

1

这将会是:

public void changeName(String newName) 
{ 
    name=newName; 
} 
2
name = newName; 

会工作得很好。字符串是不可变的,所以不能在之后改变。