我的教授给我的一项任务有点麻烦。这是提示:
修改Customer类以包含changeStreetO,changeCityO,changeStateO和changeZipO方法。 修改Account类以包含具有street,city,state和zip参数的changeAddressO方法。修改Bank应用程序以测试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中放入什么。我对类和方法仍然有点困难。有人可以提供一些见解/指导来帮助解决这个问题吗?谢谢你的时间。
答案 0 :(得分:1)
您只需要更改客户的地址:
public void changeAddressO(String street, String city, String state, String zip)
{
cust.changeAddress(street);
cust.changeCity(city);
cust.changeState(state);
cust.changeZip(zip);
}
答案 1 :(得分:1)
public void changeAddressO(String street, String city, String state, String zip_)
{
this.street = street;
this.city = city;
this. state = state;
this.zip_ = zip_;
}
您可能希望将它放在您的Customer类中,因为您将街道,城市,州和邮编设为私有。
答案 2 :(得分:0)
首先,改变这个
public changeAddressO(String street, String city, String state, zip_)
{
}
到
public void changeAddressO(String street, String city, String state, String zip_)
{
}
所有函数都需要返回类型,并且所有参数在名称前都需要一个类型。此函数的返回类型为void
,这意味着它不会向调用函数返回任何内容。根据需要将其更改为String
或int
或其他内容。
至少,现在你的代码将被编译。
此外,所有嵌套类都非常混乱。你希望教授不会这样给你。
首先改变这个:
public class BankModification
{
public class Account
{
//Lots of stuff
public class Customer
{
//Lots of stuff
}
}
}
到此(消除除第一个之外的所有public
):
public class BankModification
{
}
class Account
{
//Lots of stuff
}
class Customer
{
//Lots of stuff
}
这不会在功能上改变任何东西,但至少它不会让人感到困惑(注意BankModification
中没有任何内容对此没什么帮助。)
“修改Bank应用程序以测试changeAddressO方法。”
什么银行申请?我希望看到一个
public static final void main(String[] as_cmdLineParams)
在那里的某个地方。这是在命令行上键入java BankModification
时实际执行的函数(因为BankModification
是公共类,只会执行其main
。)
一些随机观察至少让你思考。