Link to home
Start Free TrialLog in
Avatar of AgentC4
AgentC4

asked on

Deposit And Withdrawl

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You have two classes called SavingsAccount ,,,
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)
Avatar of AgentC4
AgentC4

ASKER

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);
and

>>public Savings(double a, double b)

should be
public SavingsAccount(double a, double b)
Avatar of AgentC4

ASKER

i had to take out the "public" there were two of them.
Avatar of AgentC4

ASKER

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.
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.
Avatar of AgentC4

ASKER


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

Avatar of AgentC4

ASKER

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.*;
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.
Avatar of AgentC4

ASKER

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);
Avatar of AgentC4

ASKER

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"
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
Avatar of AgentC4

ASKER

im confused a bit. what do i do with these files
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AgentC4

ASKER

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
You need to use another Scanner to read from each file
Avatar of AgentC4

ASKER

thank you everyone for the help.
:-)