2016-01-02 27 views
1

我对编程非常陌生,并且对于我正在使用的类的某些代码存在问题。我需要创建一个银行账户类,返回余额,添加一笔存款,进行提款,并打印一份当前利率的账单。我被困在试图让主类返回储蓄帐户类的值。相反,它会返回节点值,我无法弄清楚我做错了什么。任何帮助,将不胜感激。代码如下。 FinalExam.java代码返回节点而不是子类的值

import java.io.*; 
    import java.util.Scanner; 


public class FinalExam 
{ 
    SavingsAccount savings; 
    static String name; 
    static double balance;  

    public static void main (String[] args) throws NegativeAmountException, InsufficientFundsException { 
    double amount = 0; 
    SavingsAccount savings = null; 
    Scanner keyboard = new Scanner(System.in); 
    try 
    { 
     savings = new SavingsAccount("Penny Saved", 500.00, .05); 
     System.out.println(savings); 
    } 
    catch(NegativeAmountException e) 
    { 
     System.out.println("NegativeAmountException: " + e.getMessage()); 
     System.exit(1); 

    } 

    System.out.println(savings); 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter your deposit amount: "); 
    amount = keyboard.nextDouble(); 
    System.out.println(savings); 
    System.out.println("Enter your withdrawal amount: "); 
    amount = keyboard.nextDouble(); 
    savings.postInterest(); 
    System.out.println(savings); 
} 
} 

BankAccount.java

import java.util.InputMismatchException; 
import java.util.Scanner; //Import for scanner 


public class BankAccount { 
    public String name; 
    public double balance;  

    //set name and balance 
    // make sure balance is not negative 
    // throw exception if balance is negative 

    public BankAccount(String name, double balance)throws NegativeAmountException { 
     if (balance < 0) 
     throw new NegativeAmountException("Cannot create a BankAccount with a negative balance"); 


    } 

    public BankAccount(String name) 
      throws NegativeAmountException 
    { 
    // set name and use 0 balance 
     name = null; 
     balance = 0; 
    } 


    public void deposit(double amount) throws NegativeAmountException { 
     if (amount < 0) 
     throw new NegativeAmountException("Cannot deposit a negative amount: " + amount); 
     balance = balance + amount; 
    } 

    public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException { 
    if (amount > balance) 
     throw new InsufficientFundsException("Cannot withdraw more than the current balance of this BankAccount"); 
    if (amount < 0) 
     throw new NegativeAmountException("Cannot withdraw a negative amount: " + amount); 
    balance = balance - amount; 
    } 

    public double getBalance() { 

    return balance; 

    } 
    // print bank statement including customer name 
// and current account balance 

    public void printStatement() { 
     System.out.println("BankAccount owned by: " + name + " balance: $" + balance); 
    } 
} 

SavingsAcount.java

public class SavingsAccount extends BankAccount 
{ 
    double interest; 

    public SavingsAccount(String name, double balance, double interest) throws NegativeAmountException { 

     super(name, balance); 


    } 
    public double getInterest() 
    { 
    return interest; 
    } 
    public void postInterest() { 

     balance = balance + ((balance * interest)/12); 
    } 

    public void printStatement() { 
     super.printStatement(); 
     System.out.println("Account for " + name + " Saved" + balance + " balance: $" + "Current Interest Rate is" + interest); 
     } 
} 

我知道FinalExam.java的最后一部分是不完整的,但我试图让在我转移其他问题之前打印出的声明。任何建议,将不胜感激。

+0

什么是你所面临的问题? –

+0

除了其他答案,您没有在SavingsAccount构造函数中设置interest字段。在'super(name,balance)'之后,',添加'this.interest = interest;' –

+0

BankAccount构造函数也是如此;没有设置字段。 –

回答

1

我被困在试图让主类返回储蓄帐户类的值。相反,它会返回节点值,我无法弄清楚我做错了什么。

你的问题是,你在呼唤

savings = new SavingsAccount("Penny Saved", 500.00, .05); 
System.out.println(savings); 

println自动调用的参数是toString方法。由于您没有@OverridetoString方法SavingsAccount,它会打印出您称之为“节点值”的内容。

SavingsAccount有一个方法

public void printStatement() 

这似乎是打印你想要的信息。所以叫它而不是println

+0

就是这样,我知道我错过了一件简单的事情。我认为我可以使用的最后一个问题是,现在我正在调用正确的方法,它只返回利率而不是帐户名称和余额。相反,它返回BankAccount拥有:空余额:$ 0.0当前利率0.05。有什么建议么? – Probowlactic

+0

我其实从其他评论中得到了这个处理。谢谢您的帮助。 – Probowlactic

+0

@Probowlactic看看你正在调用的构造函数。你可以调用'SavingsAccount(String,double,double)',它什么都不做,它会调用superconstructor'BankAccount(String,double),它什么也不做。你需要调用那些类中的方法,而不仅仅是构造函数。 – user1803551

1

使用System.out.println(savings)方法打印对象时,它在内部调用该对象的toString()方法。您需要为类SavingsAccountBankAccount实施toString()方法。

对于SavingsAccount使用这样的:

@Override 
public String toString() { 
    return "SavingsAccount{" + 
     super.toString() + 
     ", interest=" + interest + 
     '}'; 
} 

对于BankAccount使用本:

@Override 
public String toString() { 
    return "BankAccount{" + 
     "name='" + name + '\'' + 
     ", balance=" + balance + 
     '}'; 
} 

目前你是不是在课堂上设置实例变量。您正在检查负余额并抛出异常。 你也需要提高的构造中BankAccount类如下:我已经注意到这一点,你正在创建扫描仪类的多个实例

public BankAccount(String name, double balance)throws NegativeAmountException { 
    if (balance < 0) { 
    throw new NegativeAmountException("Cannot create a BankAccount with a negative balance"); 
    } 
    this.name = name; 
    this.balance = balance; 
} 
// Update the SavingsAccount class constructor as well 
public SavingsAccount(String name, double balance, double interest) throws NegativeAmountException { 
    super(name, balance); 
    this.interest = interest; // this is missing in your code. 
} 

其他的事情。您可以在主要方法中的每个位置使用相同的keyboard实例。无需创建多个实例。其中之一是不必要的。

Scanner keyboard = new Scanner(System.in); 
Scanner input = new Scanner(System.in); 
1

在FinalExam.java类中,假设您打印SavingsAccount.java类的值,但您得到的是地址。此ID因为在FinalExam.java类System.out.println(savings);而不是你应该使用像savings.printStatement();。可能是我没有错我猜..

0

答案已经给出,但也有问题,在您的BankAccount类的构造函数,你没有指定实例变量构造函数的参数,你只要检查约束等等,

public BankAccount(String name, double balance)throws NegativeAmountException { 
     if (balance < 0) 
     throw new NegativeAmountException("Cannot create a BankAccount with a negative balance"); 
     this.name = name; 
     this.balance = balance; 
} 

public BankAccount(String name) { 
    // set name and use 0 balance 
     this.name = name; 
     this.balance = 0; 
} 

谢谢:)