2014-09-20 63 views
-3

我有一個類的任務,它是通過繼承和重載和重寫方法。我想我已經完成了所有要求。但是,我對重載有點困惑,如果我做得正確。其實不知道它是如何工作的,所以我不知道我是否做到了。因此,您不必通讀方向,我的主要問題是指問「在Account類中創建兩個重載存款方法的方向,以使其中一個將整數值作爲輸入參數,第二個取雙值作爲輸入參數「。我不確定我是否做得正確。Java,重載方法

感謝您的幫助!

斯科特

可根據要求提供說明。

這裏是我....

賬戶類(超類)

//Account class 
public class Account { 
    //create data fields 
    Scanner scan = new Scanner(System.in); 
    protected int id = 0; 
    protected double balance = 0; 
    protected double annualInterestRate = 0; 
    protected Date dateCreated; 

public Account() { 
    dateCreated = new Date(); 

} 
//constructor for account w/ with id and balance args. 
public Account(int newID, double newBalance, double interestRate) { 
    dateCreated = new Date(); 
    id = newID; 
    balance = newBalance; 
    annualInterestRate = interestRate; 
}//end account method 

//setter for account ID 
public int setID(int newID) { 
    return id = newID; 
}//end setID 

//getter for account ID 
public int getID() { 
    return id; 
}//end getID 

//setter for account balance 
public void setBalance(double newBalance) { 
    balance = newBalance; 
}//end setBalance 

//getter for account balance 
public double getbalance() { 
    return balance; 
}//end method getBalance 

//setter for accounts annual interest rate 
public void setAnnualInterestrate(double newAnnualInterestRate) { 
    annualInterestRate = newAnnualInterestRate; 
}//end setAnnualInterestRate 

//getter for accounts annual interest rate 
public double getAnnualInterestRate() { 
    return annualInterestRate; 
}//end getAnnualInterestRate 

//getter for date account was created 
public Date getDateCreated() { 
    return dateCreated; 
}//end getDateCreated 

//calls the annual interest rate and divides by 12 to get the monthly rate 
public double getMonthlyInterestRate() { 
    return (annualInterestRate)/12; 
}//end getMonthlyInterestRate 

//method to make a withdrawal from account  
public double withdraw(double withdrawAmount) { 
    balance -= withdrawAmount; 
    return withdrawAmount; 
} 

//two overload method to make a deposit from account 
public void deposit(double depositAmount) { 
    balance += depositAmount; 
} 

public void deposit(int depositAmount) { 
    balance += depositAmount; 
} 
} 

儲蓄賬戶類

public class SavingAccount extends Account { 

public SavingAccount(int newID, double newBalance, double interestRate) { 
    super(newID, newBalance, interestRate); 
} 

public double withdraw(double withdrawAmount){ 

    if(balance >= withdrawAmount){ 
     return super.withdraw(withdrawAmount); 
    } 
    else{ 
     System.out.println("You cannot be overdraw your Savings Account! \nThe max you will be allowed to withdraw is: " + balance + "\n"); 
     setBalance(0); 
     return balance; 
    } 
} 

@Override 
public String toString() { 
    return "Account ID: " + id + "\nBalance: " + balance + "\nAnnual Interest rate: " 
      + annualInterestRate + "\nDate of the Account Creation:" + dateCreated; 
} 

} 

支票賬戶類

public class CheckingAccount extends Account{ 
double overDraft = 5000; 

public CheckingAccount(int newID, double newBalance, double interestRate) { 
super(newID, newBalance, interestRate);  
} 

public double getOverdraft() { 
    return overDraft; 
    } 

public void setOverdraft(double overdraft) { 
    overDraft = 5000; 
} 
public double withdraw(double withdrawAmount){ 
    double balance = getbalance(); 


    if(balance - withdrawAmount >= -overDraft){ 
     return super.withdraw(withdrawAmount); 
} 
else{ 
    System.out.println("reach overdraf limit!"); 
    setBalance(-overDraft); 
    return overDraft + getbalance(); 
} 
} 

@Override 
public String toString() { 
    return "Account ID: " + id + "\nBalance: " + balance + "\nAnnual Interest rate: " 
     + annualInterestRate + "\nDate of the Account Creation:" + dateCreated; 
} 
} 

TESTACCOUNT CLASS

import java.util.*; 

public class TestAccount { 

public static void main(String[] args) { 

    Scanner scan = new Scanner(System.in); 

    int accID = 0; 
    double balance = 0; 
    double intRate = 0; 
    double withDraw = 0; 
    double Deposit = 0; 

    //savings account 
    System.out.println("Please enter the ID of the Savings Account: "); 
    accID = scan.nextInt(); 
    scan.nextLine(); 

    System.out.println("Please enter the initial Balance of the Savings Account: "); 
    balance = scan.nextDouble(); 
    scan.nextLine(); 

    System.out.println("Please enter the Interest Rate of the Savings Account: "); 
    intRate = scan.nextDouble(); 
    scan.nextLine(); 

    SavingAccount s = new SavingAccount(accID, balance, intRate); 
    System.out.println("Please enter an amount you would like to Withdraw from the Savings Account: "); 
    withDraw = scan.nextDouble(); 
    s.withdraw(withDraw); 
    System.out.println("Please enter an amount you would like to Deposit into the Savings Account: "); 
    Deposit = scan.nextDouble(); 
    s.deposit(Deposit); 
    System.out.println("\nSavings Account Status:\n" + s); 



    //Checking account 
    System.out.println("\nPlease enter the ID of the Checking Account: "); 
    accID = scan.nextInt(); 
    scan.nextLine(); 

    System.out.println("Please enter the initial Balance of the Checking Account: "); 
    balance = scan.nextDouble(); 
    scan.nextLine(); 

    System.out.println("Please enter the Interest Rate of the Checking Account: "); 
    intRate = scan.nextDouble(); 
    scan.nextLine(); 

    CheckingAccount c = new CheckingAccount(accID, balance, intRate); 
    System.out.println("Please enter an amount you would like to Withdraw from the Checking Account: "); 
    withDraw = scan.nextDouble(); 
    c.withdraw(withDraw); 
    System.out.println("Please enter an amount you would like to Deposit into the Checking Account: "); 
    Deposit = scan.nextDouble(); 
    c.deposit(Deposit); 
    System.out.println("\nChecking Account Status:\n" + c); 

} 
} 

此外,還有什麼你會改變,使程序更好?

+3

這是一個問題還是一篇文章? – 2014-09-20 01:22:46

+0

那麼,你的具體問題是什麼?你會得到什麼樣的行爲?你期望什麼? – 2014-09-20 01:25:17

+0

對不起,這是很長的。我對編程一般都非常陌生,我的問題是,方向要求在Account類中創建兩個重載存款方法,但我不確定是否正確。不太確定它的意思。每當我重讀本書/幻燈片時,我都會感到困惑。 – NoobCoderChick 2014-09-20 01:30:20

回答

1
//two overload method to make a deposit from account 
public void deposit(double depositAmount) { 
    balance += depositAmount; 
} 

public void deposit(int depositAmount) { 
    balance += depositAmount; 
} 

是的,這些都是重載的方法。你做得對。

+0

謝謝! – NoobCoderChick 2014-09-20 02:04:15

+0

@Whyzard你可以簡單解釋爲什麼他們是重載方法嗎? – NoobCoderChick 2014-09-20 02:06:08

+0

由於它們具有相同的名稱('deposit'),但具有不同的參數類型('double'和'int')。這就是超載的意義。 – Wyzard 2014-09-20 02:08:42