Question

If I enter a negative value the program throws an error and the withdrawal amount is...

If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why?

public abstract class BankAccount {

private double balance;

private int numOfDeposits;

private int numOfwithdrawals;

private double apr;

private double serviceCharge;

//Create getter and setter methods

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

public int getNumOfDeposits() {

return numOfDeposits;

}

public void setNumOfDeposits(int numOfDeposits) {

this.numOfDeposits = numOfDeposits;

}

public int getNumOfwithdrawals() {

return numOfwithdrawals;

}

public void setNumOfwithdrawals(int numOfwithdrawals) {

this.numOfwithdrawals = numOfwithdrawals;

}

public double getApr() {

return apr;

}

public void setApr(double apr) {

this.apr = apr;

}

public double getServiceCharge() {

return serviceCharge;

}

public void setServiceCharge(double serviceCharge) {

this.serviceCharge = serviceCharge;

}

//Create constructor which accepts balance and apr as parameters

public BankAccount(double balance, double apr) {

this.balance = balance;

this.apr = apr;

}

//Create deposit method

public void deposit(double x){

balance +=x;

numOfDeposits++;

}

//Create withdraw method

public void withdraw(double x){

balance -= x;

numOfwithdrawals++;

}

//Create method to calculate interest

public void calcInterest(){

double monthlyApr = ((apr/12)/100);

double monthlyInterest = this.balance * monthlyApr;

balance +=monthlyInterest;

}

public void monthlyService(){

balance -= serviceCharge;

calcInterest();

numOfwithdrawals =0;

numOfDeposits =0;

serviceCharge = 0;

}

}

public class SavingsAccount extends BankAccount{

private boolean status;

public SavingsAccount(double balance, double apr) {

super(balance, apr);

if(balance >=25){

status =true;

}

else{

status = false;

}

}

public void withdraw(double x){

if (status){

super.withdraw(x);

}

if(super.getBalance()<25){

status =false;

}

}

public void deposit(double x){

if(!status){

double available = super.getBalance() + x;

if (available >=25){

status = true;

}

}

   super.deposit(x);

}

public void monthlyService(){

int withdrawalCount = super.getNumOfwithdrawals();

if(withdrawalCount >4){

super.setServiceCharge(withdrawalCount -4);

}

super.monthlyService();

if(super.getBalance() <25)

status = false;

}

//Create getter and setter

public boolean isStatus() {

return status;

}

public void setStatus(boolean status) {

this.status = status;

}

}

import java.text.DecimalFormat;
import java.util.*;

public class SavingsAccountDemo {

public static void main(String args[]) {

double balance;
double apr;
String input = "";
  
//Format balance to print with 2 decimals
DecimalFormat formatter = new DecimalFormat("0.00");
  

// Create a scanner object to receive user input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter beginning balance :$");
balance = keyboard.nextDouble();
System.out.print("Enter interest rate(whole number) :%");
apr = keyboard.nextDouble();
keyboard.nextLine();
// Create a savings account using the input
SavingsAccount newAccount = new SavingsAccount(balance, apr);
do {
menu();
  
input = keyboard.nextLine();
switch (input.toUpperCase().charAt(0)) {
case 'D':
System.out.print("Enter the amount you want to Deposit :$");
double amount = keyboard.nextDouble();
keyboard.nextLine();
if (amount < 0) {
System.out.println("Error: Must enter positive value");
System.out.println();
} else {

newAccount.deposit(amount);
}
break;

case 'W':
  
      
  
System.out.print("Enter the amount you want to withdraw :$");
amount = keyboard.nextDouble();
keyboard.nextLine();

if (amount < 0) {
System.out.println("Error: Must enter positive value");
  
  
}
if (amount > newAccount.getBalance()) {
   System.out.println("$ERROR: Transaction declined!! This transaction will cause overdraft or zero balance");
  
  
}
if(newAccount.getNumOfwithdrawals() >=4) {
       System.out.print("You have exceeded monthly limit of withdrawals. Fee of $1 charged\n");
       newAccount.withdraw(amount);
       newAccount.setServiceCharge(1);
       }
else {

newAccount.withdraw(amount);
}
break;
case 'B':
System.out.print("Your Balance is: ");
System.out.println(formatter.format(newAccount.getBalance()));
  
break;
case 'M':
newAccount.monthlyService();
System.out.print("Your Balance after Monthly process is: ");
System.out.println(formatter.format(newAccount.getBalance()));
break;
case 'E':
System.out.print("Balance : $");
System.out.println(formatter.format(newAccount.getBalance()));
System.out.println("Thank you. Bye");
break;

default:
System.out.println("Invalid choice. Try again\n");
break;
}
} while (input.toLowerCase().charAt(0) != 'e');

}

private static void menu() {

System.out.println("Enter D to deposit");
System.out.println("Enter W to Withdraw");
System.out.println("Enter B for Balance");
System.out.println("Enter M for Monthly Process");
System.out.println("Enter E to Exit");

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the updated code below:::

BankAccount.java  

package classess14;

public abstract class BankAccount {

private double balance;

private int numOfDeposits;

private int numOfwithdrawals;

private double apr;

private double serviceCharge;

//Create getter and setter methods

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

public int getNumOfDeposits() {

return numOfDeposits;

}

public void setNumOfDeposits(int numOfDeposits) {

this.numOfDeposits = numOfDeposits;

}

public int getNumOfwithdrawals() {

return numOfwithdrawals;

}

public void setNumOfwithdrawals(int numOfwithdrawals) {

this.numOfwithdrawals = numOfwithdrawals;

}

public double getApr() {

return apr;

}

public void setApr(double apr) {

this.apr = apr;

}

public double getServiceCharge() {

return serviceCharge;

}

public void setServiceCharge(double serviceCharge) {

this.serviceCharge = serviceCharge;

}

//Create constructor which accepts balance and apr as parameters

public BankAccount(double balance, double apr) {

this.balance = balance;

this.apr = apr;

}

//Create deposit method

public void deposit(double x){

balance +=x;

numOfDeposits++;

}

//Create withdraw method

public void withdraw(double x){

balance -= x;

numOfwithdrawals++;

}

//Create method to calculate interest

public void calcInterest(){

double monthlyApr = ((apr/12)/100);

double monthlyInterest = this.balance * monthlyApr;

balance +=monthlyInterest;

}

public void monthlyService(){

balance -= serviceCharge;

calcInterest();

numOfwithdrawals =0;

numOfDeposits =0;

serviceCharge = 0;

}

}

SavingsAccount.java

package classess14;

public class SavingsAccount extends BankAccount{

private boolean status;

public SavingsAccount(double balance, double apr) {

super(balance, apr);

if(balance >=25){

status =true;

}

else{

status = false;

}

}

public void withdraw(double x){

if (status){

super.withdraw(x);

}

if(super.getBalance()<25){

status =false;

}

}

public void deposit(double x){

if(!status){

double available = super.getBalance() + x;

if (available >=25){

status = true;

}

}

super.deposit(x);

}

public void monthlyService(){

int withdrawalCount = super.getNumOfwithdrawals();

if(withdrawalCount >4){

super.setServiceCharge(withdrawalCount -4);

}

super.monthlyService();

if(super.getBalance() <25)

status = false;

}

//Create getter and setter

public boolean isStatus() {

return status;

}

public void setStatus(boolean status) {

this.status = status;

}

}

SavingsAccountDemo.jav

package classess14;


import java.text.DecimalFormat;
import java.util.*;

public class SavingsAccountDemo {

   public static void main(String args[]) {

       double balance;
       double apr;
       String input = "";

       //Format balance to print with 2 decimals
       DecimalFormat formatter = new DecimalFormat("0.00");


       // Create a scanner object to receive user input
       Scanner keyboard = new Scanner(System.in);
       System.out.print("Enter beginning balance :$");
       balance = keyboard.nextDouble();
       System.out.print("Enter interest rate(whole number) :%");
       apr = keyboard.nextDouble();
       keyboard.nextLine();
       // Create a savings account using the input
       SavingsAccount newAccount = new SavingsAccount(balance, apr);
       do {
           menu();

           input = keyboard.nextLine();
           switch (input.toUpperCase().charAt(0)) {
           case 'D':
               System.out.print("Enter the amount you want to Deposit :$");
               double amount = keyboard.nextDouble();
               keyboard.nextLine();
               if (amount < 0) {
                   System.out.println("Error: Must enter positive value");
                   System.out.println();
               } else {

                   newAccount.deposit(amount);
               }
               break;

           case 'W':

               System.out.print("Enter the amount you want to withdraw :$");
               amount = keyboard.nextDouble();
               keyboard.nextLine();

               if (amount < 0) {
                   System.out.println("Error: Must enter positive value");


               }else if (amount > newAccount.getBalance()) {
                   System.out.println("$ERROR: Transaction declined!! This transaction will cause overdraft or zero balance");


               }else if(newAccount.getNumOfwithdrawals() >=4) {
                   System.out.print("You have exceeded monthly limit of withdrawals. Fee of $1 charged\n");
                   newAccount.withdraw(amount);
                   newAccount.setServiceCharge(1);
               }
               else {

                   newAccount.withdraw(amount);
               }
               break;
           case 'B':
               System.out.print("Your Balance is: ");
               System.out.println(formatter.format(newAccount.getBalance()));

               break;
           case 'M':
               newAccount.monthlyService();
               System.out.print("Your Balance after Monthly process is: ");
               System.out.println(formatter.format(newAccount.getBalance()));
               break;
           case 'E':
               System.out.print("Balance : $");
               System.out.println(formatter.format(newAccount.getBalance()));
               System.out.println("Thank you. Bye");
               break;

           default:
               System.out.println("Invalid choice. Try again\n");
               break;
           }
       } while (input.toLowerCase().charAt(0) != 'e');

   }

   private static void menu() {

       System.out.println("Enter D to deposit");
       System.out.println("Enter W to Withdraw");
       System.out.println("Enter B for Balance");
       System.out.println("Enter M for Monthly Process");
       System.out.println("Enter E to Exit");

   }

}

output:

Quic ingsAc Console3 SavingsAccountDemo Java Application] C:\Program Files\ Javajre7\bin\javaw.exe (May 5, 20 Enter beginning

Add a comment
Know the answer?
Add Answer to:
If I enter a negative value the program throws an error and the withdrawal amount is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank...

    please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank you so much! public abstract class BankAccount {    //declare the required class variables    private double balance;    private int num_deposits;    private int num_withdraws;    private double annualInterest;    private double serviceCharges;       //constructor that takes two arguments    // one is to initialize the balance and other    // to initialize the annual interest rate       public BankAccount(double balance,...

  • /* * To change this license header, choose License Headers in Project Properties. * To change...

    /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package checkingaccounttest; // Chapter 9 Part II Solution public class CheckingAccountTest { public static void main(String[] args) {    CheckingAccount c1 = new CheckingAccount(879101,3000); c1.deposit(350); c1.deposit(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.withdraw(350); c1.withdraw(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.deposit(1000); System.out.println(c1); System.out.println("After calling computeMonthlyFee()"); c1.computeMonthlyFee(); System.out.println(c1); } } class BankAccount { private int...

  • I need to update the code listed below to meet the criteria listed on the bottom....

    I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying...

    This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • NOTE: Use the Account class codes in the section below these questions. Modify the main method...

    NOTE: Use the Account class codes in the section below these questions. Modify the main method in the CheckingAccountDemo class: Take out all previous code. Declare an object array with 10 accounts. Create a for loop to ask user to input each account’s information (name, account number, and initial balance) from keyboard and then initialize for each account object. Create a while loop to allow one to work on depositing and withdrawing operations on any account till one want to...

  • Point out errors in the following codes and correct them public class CheckingAccount {       //...

    Point out errors in the following codes and correct them public class CheckingAccount {       // Create attributes accountNum, Balance       private int accountNum; // Account Number       private double balance; // Account balance       // Constructor       public void CheckingAccount(int aNum, double balance) {             // Call setters to initialize attribute values             setAccountNumber(aNum);             setBalance;       }       // Setters       public void setAccountNumber(int aNum) {             if (accountNum >= 100) {                   accountNum = aNum;             }...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT