Deposit And Withdrawl

AgentC4
AgentC4 used Ask the Experts™
on
Im lookin for someone to see why i keep getting errors messages for this code. this is what the error is:

DepositAndWithdrawl.java:9: invalid method declaration; return type required
      public Savings(double a, double b)
             ^
DepositAndWithdrawl.java:126: reached end of file while parsing
}

THIS IS THE ASSIGNEMENT.

Use notepad to create a text file named Deposits.txt. the file should contain the following numbers, one per line:

100.00
124.00
78.92
37.55
Next, create a text file named Withdrawls.txt. The file should contain the following number, one per line:

29.88
110.00
27.52
50.00
12.90

The numbers in the Deposits.txt file are the amounts of deposit that were made to a savings account during the month, and the numbers in the Withdrawls.txt are the aomunts of withdrawls tha twere made during the month. Write a program that creates an instance of the SavingsAccount class that you wrote. The starting balance for the object is 500.00. The program should read the values from the Deposits.txt file and use the object's method to add them to the account balance. The program should read the values from the Withdrawls.txt file and use the object's method to subtract them from the account balance. The program should call the class method to calculate the monthly interest, and then display the ending balance and the total interest.

HERE IS MY SAVINGS ACCOUNT CLASS CODE. i keep getting error messoges.
public class SavingsAccount 
{
	private double balance;
	private double interestRate;
	private double totalInterest;
	private double totalDeposit;
	private double totalWithdraw;
 
	public Savings(double a, double b)
	{
		balance = a;
		interestRate = b;
		totalDeposit = 0.00;
		totalWithdraw = 0.00;
		totalInterest = 0.00;
	}
 
	public void setInterestRate(double b)
	{
		interestRate = b;
	}
 
	public void setBalance(double a)
	{
		balance = a;
	}
 
	public double getInterestRate()
	{
		return interestRate;
	}
 
	public double getBalance()
	{
		return balance;
	}
 
	public void deposit(double amount1)
	{
		totalDeposit += amount1;
		balance += amount1;
	}
 
		public void withdraw( double amount2 )
		{
			totalWithdraw += amount2;
			balance -= amount2;
		}
 
		public void computeInterest()
		{
			totalInterest += balance * ((interestRate/100)/12);
			balance += balance * ((interestRate/100)/12);
 		}
 
		public boolean equals(Savings account)
		{
			if(interestRate == account.interestRate && balance == account.balance) {
			return true;
		}
		else 
		{
			return false;
		}
}
 
 
	@Override
	public String toString()
	{
		return(" Total amount deposited: " + totalDeposit + "\n" +
		" Total amount withdrawn: " + totalWithdraw + "\n" +
		" Final balance: " + balance + "\n" +
		" Total Interest: " + totalInterest);
	}
 
}
 
 
 
 
public class SavingsAccount 
{
	public static void main(String[] args) 
	{
		int i;
		int option = 0;
 
		Scanner keyboard = new Scanner(System.in);
 
		System.out.println("Please enter the month: ");
		int month = keyboard.nextInt();
		System.out.println("Please enter the balance: ");
		double balance = keyboard.nextDouble();
		System.out.println("Please enter the annual interest: ");
		double interestRate = keyboard.nextDouble();
		 
		Savings account = new Savings(balance, interestRate);
		 
		for(i = 1; i <= month; i++)
		{
			System.out.println("********************...");
			System.out.println("*1. Deposit *");
			System.out.println("*2. Withdrawal *");
			System.out.println("********************..");
			option = keyboard.nextInt();
		if(option == 1)
		{
			System.out.println("Please enter the deposited amount: ");
			account.deposit(keyboard.nextDouble());
			account.computeInterest(); 
		}
		else if(option == 2)
		{
			System.out.println("Please enter the withdrawal amount: ");
			account.withdraw(keyboard.nextDouble());
			account.computeInterest();
		}
		else
		{
			System.out.println("Wrong Input!");
			System.exit(0);
		}
		System.out.println(account);
		}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
You have two classes called SavingsAccount ,,,
Top Expert 2009

Commented:
The constructor should be named after the class:

public class SavingsAccount
{
        private double balance;
        private double interestRate;
        private double totalInterest;
        private double totalDeposit;
        private double totalWithdraw;
 
        public Savings(double a, double b)  <----
        {


That should be: public SavingsAccount(double a, double b)

Author

Commented:
SavingsAccount.java:82: class SavingsAccount2 is public, should be declared in a file named SavingsAccount2.java
public class SavingsAccount2
       ^
SavingsAccount.java:56: cannot find symbol
symbol  : class Savings
location: class SavingsAccount
            public boolean equals(Savings account)
                                  ^
SavingsAccount.java:89: cannot find symbol
symbol  : class Scanner
location: class SavingsAccount2
            Scanner keyboard = new Scanner(System.in);
            ^
SavingsAccount.java:89: cannot find symbol
symbol  : class Scanner
location: class SavingsAccount2
            Scanner keyboard = new Scanner(System.in);
                                   ^
SavingsAccount.java:98: cannot find symbol
symbol  : class Savings
location: class SavingsAccount2
            Savings account = new Savings(balance, interestRate);
            ^
SavingsAccount.java:98: cannot find symbol
symbol  : class Savings
location: class SavingsAccount2
            Savings account = new Savings(balance, interestRate);
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Top Expert 2016

Commented:
and

>>public Savings(double a, double b)

should be
public SavingsAccount(double a, double b)

Author

Commented:
i had to take out the "public" there were two of them.

Author

Commented:
i got rid of one of the PUBLIC names and that took care of that issue. i changed the class names to SavingsAccount  for the first class and Savings_Account for the second class.
Top Expert 2009

Commented:
You still need to name the constructors properly, as noted in http:#24874469 or your code wont function as expected. Constructors need to match the name of their class.

Author

Commented:

public class SavingsAccount
{
	private double balance;
	private double interestRate;
	private double totalInterest;
	private double totalDeposit;
	private double totalWithdraw;
 
	public SavingsAccount(double a, double b)
	{
		balance = a;
		interestRate = b;
		totalDeposit = 0.00;
		totalWithdraw = 0.00;
		totalInterest = 0.00;
	}
 
	public void setInterestRate(double b)
	{
		interestRate = b;
	}
 
	public void setBalance(double a)
	{
		balance = a;
	}
 
	public double getInterestRate()
	{
		return interestRate;
	}
 
	public double getBalance()
	{
		return balance;
	}
 
	public void deposit(double amount1)
	{
		totalDeposit += amount1;
		balance += amount1;
	}
 
		public void withdraw( double amount2 )
		{
			totalWithdraw += amount2;
			balance -= amount2;
		}
 
		public void computeInterest()
		{
			totalInterest += balance * ((interestRate/100)/12);
			balance += balance * ((interestRate/100)/12);
 		}
 
		public boolean equals(Savings account)
		{
			if(interestRate == account.interestRate && balance == account.balance) {
			return true;
		}
		else 
		{
			return false;
		}
}
 
 
	@Override
	public String toString()
	{
		return(" Total amount deposited: " + totalDeposit + "\n" +
		" Total amount withdrawn: " + totalWithdraw + "\n" +
		" Final balance: " + balance + "\n" +
		" Total Interest: " + totalInterest);
	}
 
}
 
class Savings_Account
{
	public static void main(String[] args) 
	{
		int i;
		int option = 0;
 
		Scanner keyboard = new Scanner(System.in);
 
		System.out.println("Please enter the month: ");
		int month = keyboard.nextInt();
		System.out.println("Please enter the balance: ");
		double balance = keyboard.nextDouble();
		System.out.println("Please enter the annual interest: ");
		double interestRate = keyboard.nextDouble();
		 
		Savings account = new Savings(balance, interestRate);
		 
		for(i = 1; i <= month; i++)
		{
			System.out.println("********************...");
			System.out.println("*1. Deposit *");
			System.out.println("*2. Withdrawal *");
			System.out.println("********************..");
			option = keyboard.nextInt();
		if(option == 1)
		{
			System.out.println("Please enter the deposited amount: ");
			account.deposit(keyboard.nextDouble());
			account.computeInterest(); 
		}
		else if(option == 2)
		{
			System.out.println("Please enter the withdrawal amount: ");
			account.withdraw(keyboard.nextDouble());
			account.computeInterest();
		}
		else
		{
			System.out.println("Wrong Input!");
			System.exit(0);
		}
		System.out.println(account);
		}
	}
}

Open in new window

Author

Commented:
i put in import java.util.*; right before the second class becuase im using the Scanner method and i get an error message:

SavingsAccount.java:79: class, interface, or enum expected
 import java.util.*;
Top Expert 2009

Commented:
You should put all imports at the top of the file, its a good practice and you probably simply put it inside something besides the global namespace.

Author

Commented:
for some reason i tried that before and it didnt work and now it does. Now i get this.

SavingsAccount.java:58: cannot find symbol
symbol  : class Savings
location: class SavingsAccount
            public boolean equals(Savings account)
                                  ^
SavingsAccount.java:98: cannot find symbol
symbol  : class Savings
location: class Savings_Account
            Savings account = new Savings(balance, interestRate);
            ^
SavingsAccount.java:98: cannot find symbol
symbol  : class Savings
location: class Savings_Account
            Savings account = new Savings(balance, interestRate);

Author

Commented:
i changed the name from (Savings account) to SavingsAccount and it went away. but now i get this.

SavingsAccount.java:58: <identifier> expected
            public boolean equals(SavingsAccount)

there is an arrow under the word "equals"
Top Expert 2016

Commented:
There are a few minor corrections but the source and binaries are here:

http://technojeeves.com/tech/savings.jar

Run as


java -jar savings.jar

Author

Commented:
im confused a bit. what do i do with these files
Top Expert 2016
Commented:
>>im confused a bit. what do i do with these files

Run the app using the command i just posted. If you want to unpack the jar, do

jar xf savings.jar

in a new directory

Author

Commented:
how do i make the program take the Deposits and Withdrawls inside the txt files i created and apply them to the balance in the code. im having a hard time making the I/O
Top Expert 2016

Commented:
You need to use another Scanner to read from each file

Author

Commented:
thank you everyone for the help.
Top Expert 2016

Commented:
:-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial