好的,所以我很抱歉有这么多代码,我觉得所有这些都可以缩短这么多。无论如何,对于我的问题,我想向客户类添加changeStreet(), changeCity(), changeState(), changeZip()
方法,以便更改街道等。
我无法理解多态性以及我的变量如何相关,这看起来很冗余..有人可以帮助我理解这个主题和我的代码。
测试人员类:
public static void main(String[] args) {
Bank easySave = new Bank();
Scanner input = new Scanner(System.in);
String action, acctID;
Double amt;
/* display menu of choices */
do {
System.out.println("\nDeposit\\Withdrawal\\Check balance");
System.out.println("Add an account\\Remove an account");
System.out.println("Edit Account");
System.out.println("Quit\n");
System.out.print("Enter choice: ");
action = input.next();
if (action.equalsIgnoreCase("A")) {
easySave.addAccount();
} else if (!action.equalsIgnoreCase("Q")) {
System.out.print("Enter account ID: ");
acctID = input.next();
if (action.equalsIgnoreCase("D")) {
System.out.print("Enter deposit amount: ");
amt = input.nextDouble();
easySave.transaction(1, acctID, amt);
} else if (action.equalsIgnoreCase("W")) {
System.out.print("Enter withdrawal amount: ");
amt = input.nextDouble();
easySave.transaction(2, acctID, amt);
} else if (action.equalsIgnoreCase("C")) {
easySave.checkBalance(acctID);
} else if (action.equalsIgnoreCase("R")) {
easySave.deleteAccount(acctID);
} else if (action.equalsIgnoreCase("E")) {
System.out.println("Street(S)\\ State(ST) \\");
System.out.println("City(C) \\ Zip(Z) \\");
if (action.equalsIgnoreCase("S")){
easySave.changeStreet(Street);
} else if (action.equalsIgnoreCase("ST")){
easySave.changeState(State);
} else if (action.equalsIgnoreCase("C")){
easySave.changeCity(City);
} else if (action.equalsIgnoreCase("Z")){
easySave.changeZip(Zip);
}
}
}
} while (!action.equalsIgnoreCase("Q"));
}
客户类:
private String firstName;
private String lastName;
private String Street;
private String State;
private String City;
private String 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 s, String st, String c, String z) {
firstName = fName;
lastName = lName;
Street = s;
State = st;
City = c;
Zip = z;
}
public void changeStreet(String Street){
Street = this.Street;
System.out.println("yo");
}
public void changeState(String State){
State = this.State;
}
public void changeCity(String City){
City = this.City;
}
public void changeZip(String Zip){
Zip = this.Zip;
}
/**
* Returns a String that represents the Customer object.
* pre: none
* post: A string representing the Customer object has
* been returned.
*/
public String toString() {
String custString;
custString = firstName + " " + lastName + "\n"
+ Street + "\n" + City + State + " \n" + Zip ;
return(custString);
}
银行等级:
private ArrayList<Account> accounts;
/**
* constructor
* pre: none
* post: accounts has been initialized.
*/
public Bank() {
accounts = new ArrayList<Account>();
}
/**
* Adds a new account to the bank accounts.
* pre: none
* post: An account has been added to the bank's accounts.
*/
public void addAccount() {
Account newAcct;
double bal;
String fName, lName, s, st, c, z;
Scanner input = new Scanner(System.in);
p("First name: ");
fName = input.nextLine();
p("Last name: ");
lName = input.nextLine();
p("Street");
s = input.nextLine();
p("State");
st = input.nextLine();
p("City");
c = input.nextLine();
p("Zip");
z = input.nextLine();
p("Beginning balance: ");
bal = input.nextDouble();
newAcct = new Account(bal, fName, lName, s, st, c, z); //create acct object
accounts.add(newAcct); //add account to bank accounts
System.out.println("Account created. Account ID is: " + newAcct.getID());
}
/**
* Deletes an existing account.
* pre: none
* post: An existing account has been deleted.
*/
public void deleteAccount(String acctID) {
int acctIndex;
Account acctToMatch;
acctToMatch = new Account(acctID);
acctIndex = accounts.indexOf(acctToMatch); //retrieve location of account
if (acctIndex > -1) {
accounts.remove(acctIndex); //remove account
System.out.println("Account removed.");
} else {
System.out.println("Account does not exist.");
}
}
/**
* Performs a transaction on an existing account. A transCode of 1 is for deposits
* and a transCode of 2 is for withdrawals.
* pre: transCode is 1 or 2.
* post: A transaction has occurred for an existing account.
*/
public void transaction(int transCode, String acctID, double amt) {
int acctIndex;
Account acctToMatch, acct;
acctToMatch = new Account(acctID);
acctIndex = accounts.indexOf(acctToMatch); //retrieve location of account
if (acctIndex > -1) {
acct = accounts.get(acctIndex); //retrieve object to modify
if (transCode == 1) {
acct.deposit(amt);
accounts.set(acctIndex, acct); //replace object with updated object
System.out.println(acct);
} else if (transCode == 2) {
acct.withdrawal(amt);
accounts.set(acctIndex, acct); //replace object with updated object
System.out.println(acct);
}
} else {
System.out.println("Account does not exist.");
}
}
/**
* Displays the account information, including the current balance,
* for an existing account.
* pre: none
* post: Account information, including balance, has been displayed.
*/
public void checkBalance(String acctID) {
int acctIndex;
Account acctToMatch, acct;
acctToMatch = new Account(acctID);
acctIndex = accounts.indexOf(acctToMatch); //retrieve location of account
if (acctIndex > -1) {
acct = accounts.get(acctIndex); //retrieve object to display
System.out.println(acct);
} else {
System.out.println("Account does not exist.");
}
}
public static void p(String s) { System.out.println(s); }
帐户类:
private double balance;
private Customer cust;
private String acctID;
/**
* constructor
* pre: none
* post: An account has been created. Balance and
* customer data has been initialized with parameters.
*/
public Account(double bal, String fName, String lName, String s, String st, String z, String c) {
balance = bal;
cust = new Customer(fName, lName, s ,st ,z ,c);
acctID = fName.substring(0,1) + lName;
}
/**
* constructor
* pre: none
* post: An empty account has been created with the specified account ID.
*/
public Account(String ID) {
balance = 0;
cust = new Customer("", "", "", "", "", "");
acctID = ID;
}
/**
* Returns the account ID.
* pre: none
* post: The account ID has been returned.
*/
public String getID() {
return(acctID);
}
/**
* 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 true when objects have matching account ids.
* pre: none
* post: true has been returned when the objects are equal,
* false returned otherwise.
*/
public boolean equals(Object acct) {
Account testAcct = (Account)acct;
if (acctID.equals(testAcct.acctID)) {
return(true);
} else {
return(false);
}
}
/**
* 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 = acctID + "\n";
accountString += cust.toString();
accountString += "Current balance is " + money.format(balance);
return(accountString);
}
我在以下代码中收到错误:
if (action.equalsIgnoreCase("S")){
easySave.changeStreet(Street);
} else if (action.equalsIgnoreCase("ST")){
easySave.changeState(State);
} else if (action.equalsIgnoreCase("C")){
easySave.changeCity(City);
} else if (action.equalsIgnoreCase("Z")){
easySave.changeZip(Zip);
}
答案 0 :(得分:0)
这可能不会回答这个问题,但会让你知道多态性是什么......
让我们从某种&#34;地址&#34;
的基本概念开始public class Address {
private String street;
private String state;
private String city;
private String zip;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
@Override
public String toString() {
return "Address{" + "street=" + street + ", state=" + state + ", city=" + city + ", zip=" + zip + '}';
}
}
它有简单的方法来获取和设置对象的值......
现在,我们可以创建&#34;可寻址的对象&#34;
public class Bank extends Address {
}
public class Customer extends Address {
}
Bank
和Customer
都继承自Address
,这意味着它们会继承该类的所有功能。
现在,让我们创建一些适用于Address
...
public void updateAddress(Address address, String street, String city, String state, String zip) {
address.setStreet(street);
address.setCity(city);
address.setState(state);
address.setZip(zip);
}
public void formatAddress(Address address) {
System.out.println(address.getStreet());
System.out.println(address.getCity() + ", " + address.getState() + ", " + address.getZip());
}
非常基本,一个更新地址详细信息,一个打印出格式化的Addreess
...
现在为魔术......
让我们创建一个Bank
和Customer
Bank bank = new Bank();
Customer customer = new Customer();
现在,让我们更新地址详细信息......
updateAddress(bank, "Some street", "Some city", "Some state", "Some zip");
updateAddress(customer, "Other street", "Other city", "Other state", "Other zip");
打印出来......
formatAddress(bank);
formatAddress(customer);
但等等,这怎么可能?!?好吧,这就是多态性。 Bank
和Customer
都可以在预期Address
的任何地方使用,因为它们从这个共同的祖先类继承,它们具有“行为”的能力。像Address
那样。
现在,通常,我通过接口和抽象类来完成所有这些操作,并且我可能会维护与每个对象关联的List
个Address
类,但这是一个简单的例子来帮助证明一个观点...
你的二传手是错误的,而不是......
public void changeStreet(String Street){
Street = this.Street;
System.out.println("yo");
}
它应该是......
public void changeStreet(String Street){
this.Street = Street;
System.out.println("yo");
}
你遇到问题的原因......
if (action.equalsIgnoreCase("S")){
easySave.changeStreet(Street);
} else if (action.equalsIgnoreCase("ST")){
easySave.changeState(State);
} else if (action.equalsIgnoreCase("C")){
easySave.changeCity(City);
} else if (action.equalsIgnoreCase("Z")){
easySave.changeZip(Zip);
}
是因为Street
,State
,City
和Zip
在您尝试使用它们的上下文中未定义...
为了编辑帐户,您需要能够选择一个帐户,一旦您选择了帐户,您就可以操作它...
} else if (action.equalsIgnoreCase("E")) {
Account account = //... Some means by which the user can select the account
System.out.println("Street(S)\\ State(ST) \\");
System.out.println("City(C) \\ Zip(Z) \\");
if (action.equalsIgnoreCase("S")){
String street = //...
account.changeStreet(Street);
} else if (action.equalsIgnoreCase("ST")){
String State = //...
account.changeState(State);
} else if (action.equalsIgnoreCase("C")){
String City = //...
account.changeCity(City);
} else if (action.equalsIgnoreCase("Z")){
String Zip = //...
account.changeZip(Zip);
}
}
这可能暗示,你真的不需要多态性