4 Replies - 1036 Views - Last Post: 11 September 2012 - 02:20 PM Rate Topic: -----

#1 chamo0683   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 05-September 12

Error calculating current balance...

Posted 07 September 2012 - 12:25 AM

Hi:A made a post yesterday about this program. I finally finished it but It does not calculate the current balance after either a deposit or a withdrawl. Can you please help me find out what am I doing wrong?? I am supposed to use inheritance in this program. Any help is appreciated... Thanks

//test program
import java.text.*;
import java.util.*;
public class TestInheritedAccounts {

    public static void main(String[] args) {

	int answer;
	int choice;
	Scanner keyboard = new Scanner(System.in);



	do{
		System.out.println("Which account do you want to ascess:");
		System.out.println("1.Checking\n2.Savings\n");
		answer = keyboard.nextInt();

		if (answer==1){


   		Account checking = new CheckingAccount();//creates an object of the checking account
   		CheckingAccount c = new CheckingAccount();
   		checking.setBalance(234);

   		c.readCheckingAccount();//read information for checking account

   		System.out.println(c.toString());//print info from the toString method
   		System.out.println("********************************************************************");
   		}


   		if (answer==2){

   		Account savings = new SavingsAccount();//creates an object of the savings account
   		SavingsAccount s = new SavingsAccount();

		s.readSavingsAccount();//read information for savings account

		System.out.println(s.toString());
		System.out.println("********************************************************************");
		}

		System.out.println("Do you want to do a different transaction:");
		System.out.println("1.Yes\n2.No");
		answer = keyboard.nextInt();

	}while (answer != 2);



    }
}


//account class
import java.util.*;
public class Account {

	/************** data fields ****************/

	private int id;
	private double balance;
	private double annualInterestRate;
	private Date dateCreated;//defines the date

	/************** constructors **************/
	public Account() {
   		balance = 0;
   		annualInterestRate = 0;
  	}


	public Account(int id, double balance){
		this.id = id;
		this.balance = balance;
		dateCreated = new Date();
	}



	/************** mutator methods ************/

	public void setID(int id){

		this.id = id;
	}

	public void setBalance(double balance){

		this.balance = balance;
	}

	public void setAnnualInterestRate(double annualInterestRate){

		this.annualInterestRate = annualInterestRate;
	}

	/****************** Accesor method *******************/

	 public int getId(){

	 	return id;
	 }

	 public double getBalance(){

	 	return balance;
	 }

	 public double getAnnualInterestRate(){

	 	return annualInterestRate;
	 }

	 public Date getDateCreated(){

	 	return dateCreated;
	 }
	/**************** Additional methods ***********/

	 public double getMonthlyInterest(){
	 	return balance * (annualInterestRate / 1200);//calculates monthly interest rate
	 }

	 public void withdraw (double amount){//calculates balance after withdraw

	  	balance -= amount;

	 }

	 public void deposit (double amount){//calculates balance after deposit

	balance += amount;

	 }

}



//checking class
import java.util.*;
public class CheckingAccount extends Account {

	int overdraft = 250;//data field
	private double balance;
	double withdraw;
	double deposit;
	int choice;

	CheckingAccount(){
	}

	public void readCheckingAccount(){
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Checking account");
		System.out.print("Enter 1 to deposit or 2 to withdraw:");
		choice =keyboard.nextInt();
	if (choice==1){

		System.out.print("Enter amount to be deposit:" );
		deposit =keyboard.nextDouble();
	}
	else if (choice==2){

		System.out.print("Enter amount to be withdraw:");
		withdraw = keyboard.nextDouble();
	}

	else {System.out.print("Invalid choice");
	}
	}

	public void withdraw(double withdraw){


	if (withdraw > (balance + overdraft)){

		System.out.println("You cannot withdraw $" + withdraw +". Your overdraft limit is "+ overdraft+ ". The available balance in your checking account is" + getBalance());
	}
	else{

		 balance =- withdraw;}
	}

	public String toString(){

		return "The balance on your checking account is:" + getBalance();
}

}


//savings class
import java.util.*;
public class SavingsAccount extends Account {

	int choice;
	double withdraw;
	double deposit;
	private double balance;

	SavingsAccount(){
	}

	public void readSavingsAccount(){

	Scanner keyboard = new Scanner(System.in);
	System.out.println("Press 1 to deposit or 2 to withdraw:");
	choice = keyboard.nextInt();

	if (choice == 1){
		System.out.print("Enter amount to deposit:" );
		deposit = keyboard.nextDouble();
	}

	if (choice == 2){
		System.out.println("Sorry, you cannot withdraw from your savings account at this moment.");
	}

	}


	public String toString(){

		return "The balance on your savings account is:" + getBalance();
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Error calculating current balance...

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Error calculating current balance...

Posted 07 September 2012 - 12:51 AM

You are not supposed to declare a balance variable in your subclasses, you are supposed to use the methods you inherit from the super class.

I see that you have added some rules in your CheckingAccount class, and you will want the balance to change in the class inherited from (Account). You can call the super class method like this
super.withdraw(20);


This post has been edited by CasiOo: 07 September 2012 - 12:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 chamo0683   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 05-September 12

Re: Error calculating current balance...

Posted 07 September 2012 - 08:44 PM

View PostCasiOo, on 07 September 2012 - 12:51 AM, said:

You are not supposed to declare a balance variable in your subclasses, you are supposed to use the methods you inherit from the super class.

I see that you have added some rules in your CheckingAccount class, and you will want the balance to change in the class inherited from (Account). You can call the super class method like this
super.withdraw(20);


Ok... I deleted the balance variables but still cannot get the balance up to date after a transaction... Also I am getting an error on my checking class... here is the updated checking class...Can u please help me out?... Thanks

import java.util.*;
public class CheckingAccount extends Account {

	int overdraft = 250;//data field
	double withdraw;
	double deposit;
	int choice;
	double newBalance;


	CheckingAccount(){
	}

	public void readCheckingAccount(){
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Checking account");
		System.out.print("Enter 1 to deposit or 2 to withdraw:");
		choice =keyboard.nextInt();
	if (choice==1){

		System.out.print("Enter amount to be deposit:" );
		deposit =keyboard.nextDouble();
	}
	else if (choice==2){

		System.out.print("Enter amount to be withdraw:");
		withdraw = keyboard.nextDouble();
	}

	else {System.out.print("Invalid choice");
	}
	}

	public void withdraw(double withdraw){


	if (withdraw > (getBalance() + overdraft)){

		System.out.println("You cannot withdraw $" + withdraw +". Your overdraft limit is "+ overdraft+ ". The available balance in your checking account is" + getBalance());
	}
	else{

		 newBalance= getBalance()- withdraw;}
	}

	public String toString(){

		return "The balance on your checking account is:" + getBalance();
}

}


Was This Post Helpful? 0
  • +
  • -

#4 natecat   User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 233
  • Joined: 19-December 11

Re: Error calculating current balance...

Posted 07 September 2012 - 10:40 PM

Are you making sure to update the textbox to the current variable value?
Was This Post Helpful? 0
  • +
  • -

#5 WannaJava?   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Error calculating current balance...

Posted 11 September 2012 - 02:20 PM

when you put

newBalance = getBalance() - withdraw;

you are never calling a set method to set your balance to the newBalance

that is what I am thinking is wrong

so it is working properly, but you are not setting the balance to represent the most recent amount
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1