0

i have been tinkering around with this for about 5 hours and i think i am close, but am still having compile errors. I had the base program working before i started adding the exceptions but now that i started adding them i think i am actually getting farther from complete. If anyone can offer assistance i would appreciate it.

What i need to accomplish:

For your new program (#5 above), create a class called BankAccount. The BankAccount class should contain a String to store the customer name and a double to store the account balance. The BankAccount class should have two constructors, as follows:
  public BankAccount(String name, double balance)
            throws NegativeAmountException 
  {
    // set name and balance 
    // make sure balance is not negative
    // throw exception if balance is negative
  }
  public BankAccount(String name)
            throws NegativeAmountException 
  {
    // set name and use 0 balance 
  }
As can be seen, the first constructor throws a NegativeAmountException if the balance being used to create the bank account is negative. You will have to create this exception class yourself.
The BankAccount class should also contain methods to make a deposit, make a withdrawal, get the current balance, and print a bank account statement. The interfaces for these methods should appear as follows:
 // update balance by adding deposit amount
// make sure deposit amount is not negative 
// throw exception if deposit is negative 

public void deposit(double amount) throws NegativeAmountException
  // update balance by subtracting withdrawal amount
// throw exception if funds are not sufficient
// make sure withdrawal amount is not negative 
// throw NegativeAmountException if amount is negative 
// throw InsufficientFundsException if balance < amount
  
  public void withdraw(double amount) 
                 throws InsufficientFundsException, NegativeAmountException
 // return current balance
  
  public double getBalance()
 // print bank statement including customer name
// and current account balance
  public void printStatement();
Use the BankAccount class as the superclass for a SavingsAccount class. In addition to the behaviors of a BankAccount, a SavingsAccount also accumulates interest; therefore, the SavingsAccount class contains a double that is populated with the current interest rate. In addition to its constructors (you decide what the constructors should be), the SavingsAccount class should contain the following methods:
  // post monthly interest by multiplying current balance 
// by current interest rate divided by 12 and then adding 
// result to balance by making deposit
  
  public void postInterest()
// print bank statement including customer name
// and current account balance (use printStatement from
// the BankAccount superclass)
// following this also print current interest rate
  
  public void printStatement()
Once these two classes are completed, create a driver class called FinalExam containing a main method that tests the SavingsAccount class. Within the driver test class, create a SavingsAccount object and then use it to make deposits and withdrawals, and to post the monthly interest. 
To make the program simpler, you can incorporate the initial data for the Savings Accounts directly in the program (e.g., no need to prompt for the account holder name or starting balance). The only things you need to prompt for are the deposit amount and the withdrawal amount. Also, to simplify the task, the only exceptions that you should handle are the NegativeAmountException and the InsufficientFundsException. If either of these exception conditions occurs, print an appropriate error message and terminate the application. You can simply re-throw any IOExceptions from the main.

What i have so far is:

  package finalexam1;
import java.util.*;
import java.io.*;

public class FinalExam1 {
    public static void main (String[] args) throws IOException
    {
        double amount = 0;
        SavingsAccount account = null;
        Scanner in = new Scanner(System.in);

        try{
            account = new SavingsAccount("Nicholas Lynskey", 500.00, .05);
            System.out.print("Enter your deposit amount: ");
            amount = in.nextDouble();
            account.deposit(amount);
        }catch(NegativeAmountException e){
            System.out.println("NegativeAmountException: " + e.getMessage());
            System.exit(1);
        }        
        System.out.print("Enter your withdraw amount: ");
        try{
            amount = in.nextDouble();
            account.withdraw(amount);
        }catch(NegativeAmountException e){ 
            System.out.println("NegativeAmountException: " + e.getMessage());
            System.exit(1);
        }catch(InsufficientFundsException e){
            System.out.println("InsufficientFundsException: " + e.getMessage());
            System.exit(1);
        }
    }//end main
}//end class
----------------------------------------------------------------------
/*
 * 
 */
package finalexam1;

public class SavingsAccount extends BankAccount {
    private double interest;

    public SavingsAccount(String name, double balance, double interest){
        super(name, balance);
        this.interest = interest;
    }
    public void postInterest(){
        super.accBalance = super.accBalance +((interest / 12)* super.accBalance); 
        System.out.printf("\n");   
    }
    public void print(){ 
        super.print();
        System.out.printf("%%%.2f Current rate of interest\n",interest);   
    }
}
----------------------------------------------------------------------
/*
 * 
 */
package finalexam1;

public class SavingsAccount extends BankAccount {
    private double interest;

    public SavingsAccount(String name, double balance, double interest){
        super(name, balance);
        this.interest = interest;
    }
    public void postInterest(){
        super.accBalance = super.accBalance +((interest / 12)* super.accBalance); 
        System.out.printf("\n");   
    }
    public void print(){ 
        super.print();
        System.out.printf("%%%.2f Current rate of interest\n",interest);   
    }
}
----------------------------------------------------------------------
/*
 * 
 */
package finalexam1;

class NegativeAmountException extends Exception {
    public NegativeAmountException() {
    }
    public NegativeAmountException(String str) {
        super(str);
    }
    public String toString() {
        return "NegativeAmountException";
    }

}
----------------------------------------------------------------------
/*
 * 
 */
package finalexam1;
import java.util.*;
import java.io.*;

class NegativeAmountException extends Exception {
    public NegativeAmountException() {
    }
    public NegativeAmountException(String str) {
        super(str);
    }
    public String toString() {
        return "NegativeAmountException";
    }

}

Current Errors:

Compiling 5 source files to E:\DEVELOP\Proj\FinalExam1\build\classes
E:\DEVELOP\Proj\FinalExam1\src\finalexam1\SavingsAccount.java:12: error: unreported exception NegativeAmountException; must be caught or declared to be thrown
        super(name, balance);
1 error
E:\DEVELOP\Proj\FinalExam1\nbproject\build-impl.xml:929: The following error occurred while executing this line:
E:\DEVELOP\Proj\FinalExam1\nbproject\build-impl.xml:269: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
Sk3y.0n3
  • 19
  • 1
  • 2
    Not what you asked, but it's almost never a good idea to use `double` to store an amount of money. Bad things happen when you start adding and subtracting `double` values, and expecting them to work as if they were decimals. Get into the habit of using the `BigDecimal` class when you're working with money, unless you have a compelling reason not to and you truly understand what you're letting yourself in for. – Dawood ibn Kareem Jan 01 '16 at 04:22
  • If the linked duplicate doesn't help you, then choose another from the [many similar questions that can easily be found on Google](https://www.google.com/?gws_rd=ssl#q=java+unreported+exception+must+be+caught+or+declared+to+be+thrown+site:http:%2F%2Fstackoverflow.com%2F) (please check link). – Hovercraft Full Of Eels Jan 01 '16 at 04:24
  • When you use `throws`, you're essentially saying "let the caller handle it". When Savings Account calls the super constructor, it sees that an exception may be thrown. The super constructor is neither surrounded by a `try` `catch`, nor is a `throws` statement present. It is not enough to include a `throws` declaration where the exception is first thrown. You must handle the exception **each and every step of the way** from where it is first thrown to where it is caught (if at all). If you want an alternative, there is something called a `RuntimeException`. Compiler does not check for them. – Monkeygrinder Jan 01 '16 at 05:22
  • You should also pay attention to the error message here. What it is saying: At line 12 of your SavingsAccount class, there is an exception that is neither caught nor handled with a `throws` declaration. Even if you don't quite understand why something is a problem, the error message may be enough to provide you with a working solution. – Monkeygrinder Jan 01 '16 at 05:28

0 Answers0