我的教授給我的一個任務有點麻煩。以下是提示:Java方法和類
修改Customer類以包含changeStreetO,changeCityO,changeStateO和changeZipO方法。 修改Account類以包含具有street,city,state和zip參數的changeAddressO方法。修改銀行應用程序以測試changeAddressO方法。
我有changeStreetO,changeCityO,changeStateO和changeZipO。但是,我對ChangeAddressO方法的粗體部分感到困惑。我想我明白如何編寫這些參數,但我不確定實際方法中的內容,以及它如何適應程序的其他部分。這是我目前的代碼。
import java.text.NumberFormat;
import java.io.*;
import java.util.*;
public class BankModification
{
public class Account
{
private double balance;
private Customer cust;
public Account(double bal, String fName, String lName, String str, String city, String st, String zip)
{
balance = bal;
cust = new Customer(fName, lName, str, city, st, zip);
/**
* Returns the current balance
* pre: none
* post: The account balance has been returned
*/
}
public double getBalance()
{
return (balance);
}
/**
* A deposit is made to the account
* pre: none
* post: The balance has been increased by the amount of the deposit.
*/
public void deposit(double amt)
{
balance += amt;
/**
* A withdrawal is made from the account if there is enough money
* pre: none
* post: The balance has been decreased by the amount withdrawn.
*/
}
public void withdrawal(double amt)
{
if (amt <= balance)
balance -= amt;
else
System. out. println ("Not enough money in account.");
}
/**
* Returns a String that represents the Account object.
* pre: none
* post: A string representing the Account object has been returned.
*/
public String toString()
{
String accountString;
NumberFormat money = NumberFormat.getCurrencyInstance();
accountString = cust.toString();
accountString += "Current balance is " + money.format (balance);
return (accountString) ;
}
public changeAddressO(String street, String city, String state, zip_)
{
}
public class Customer
{
private String firstName, lastName, street, city,state, zip_;
/**
* constructor
* pre: none
* post: A Customer object has been created.
* Customer data has been initialized with parameters
*/
public Customer(String fName, String lName, String str,
String c, String s, String z)
{
firstName = fName;
lastName = lName;
street = str;
city = c;
state = s;
zip_ = z;
}
/**
* Returns a String that represents the Customer Object
* pre: none
* post: A string representing the Account object has been returned.
*/
public String toString()
{
String custString;
custString=firstName + " " + lastName + "\n";
custString +=street + "\n";
custString += city + ", "+state+ " " + zip_ + "\n";
return(custString);
}
public void ChangeStreetO(String newStreet)
{
street = newStreet;
}
public void ChangeCityO(String newCity)
{
city = newCity;
}
public void ChangeStateO(String newState)
{
state = newState;
}
public void ChangeZipO(String newZip)
{
zip_ = newZip;
}
}
}
}
我不知道如果我失去了一些東西必不可少,但沒有changeAddressO方法,程序編譯。因爲我使用chageStreetO,changeCityO,changeStateO和changeZipO創建了方法,所以我無法弄清楚我需要放入changeAddressO中。對於類和方法我仍然有點困難。有人能提供一些見解/指導來幫助解決這個問題嗎?感謝您的時間。
請記住,您可以從changeAddress()調用您自己的方法。 –
你確定'O's不是雙括號'()'嗎? – BitNinja
我會寫一個接受所有這些參數的方法,然後調用changeStreet,changeCity ...方法。 –