2014-09-20 64 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