好吧,所以我很抱歉有這麼多的代碼,我覺得所有這一切都可以縮短這麼多。無論如何,對我的問題,我想添加changeStreet(), changeCity(), changeState(), changeZip()
方法的客戶類,以改變街道等 我無法理解多態性和我的任何變量如何相關,這一切似乎很冗餘..有人可以幫助我理解這個主題和我的代碼。多態銀行申請
Tester類:
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"));
}
Customer類:
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);
}
你制定者是倒退,而不是'街= this.Street;'應該是'this.Street =街;' – MadProgrammer 2014-12-04 04:11:24
廢話讓我翻轉他們,對不起,它的深夜,我幾乎不明白多態性的東西 – 2014-12-04 04:13:09
您可能想閱讀[Java TM編程語言的代碼約定](http://www.oracle.com /technetwork/java/codeconvtoc-136057.html),它將使人們更容易閱讀你的代碼並讓你閱讀其他人 – MadProgrammer 2014-12-04 04:17:19