I have an assignment where you need to transfer money from one account to the other account... using objects of course. So far I have three different files...but I'm getting a class/enum error.
import java.util.Scanner; import java.text.NumberFormat; public class accountTest{ public static void main(String[] args){ Scanner input = new Scanner(System.in); Scanner input2 = new Scanner(System.in); Scanner input3 = new Scanner(System.in); Scanner input4 = new Scanner(System.in); Scanner input5 = new Scanner(System.in); NumberFormat formatter = NumberFormat.getCurrencyInstance(); System.out.println("Please enter your account balance: "); double balance = input.nextDouble(); Account mark = new Account(balance); Account joe = new Account(500); System.out.println("Your account balance is: " + formatter.format(mark.getAccountBalance())); System.out.println("Please enter the amount you want to deposit into the account: "); double deposit = input2.nextDouble(); System.out.println("The amount you are depositing is: " + formatter.format(deposit)); mark.enterDeposit(deposit); System.out.println("Your account balance is: " + formatter.format(mark.getAccountBalance())); System.out.println("Please enter the amount you want to withdraw: "); double withdrawal = input3.nextDouble(); System.out.println("The amount you are withdrawing is: " + formatter.format(withdrawal)); mark.takeWithdrawal(withdrawal); System.out.println("Your account balance is: " + formatter.format(mark.getAccountBalance())); System.out.println("Please enter the amount you wish to transfer: "); double transferAmount = input4.nextDouble(); System.out.println("Which account would you like to transfer " + formatter.format(transferAmount) + " to?"); String account = input5.nextLine(); Transaction trans = new Transaction(mark, joe); trans.transferMoney(transferAmount, account); System.out.println("You transfered " + transferAmount + " to " + account + "."); System.out.println("Mark's account balance is now " + formatter.format(mark.getAccountBalance())); System.out.println("Joe's account balance is now " + formatter.format(joe.getAccountBalance())); } }
public class Account{ private double balance = 0; public Account(double balance){ this.balance = balance; } public double getAccountBalance(){ return balance; } public double enterDeposit(double amount){ balance = balance + amount; return balance; } public double takeWithdrawal(double amount){ balance = balance - amount; return balance; } }
public class Transaction{ private Account mark; private Account joe; public Transaction(Account mark, Account joe){ this.mark = mark; this.joe = joe; } public void transferMoney(double amount, String account){ if(account == "joe") { mark.takeWithdrawal(amount); joe.enterDeposit(amount); } else if(account == "mark"){ joe.takeWithdrawal(amount); mark.enterDeposit(amount); } } }
That's what I have so far... but I'm having trouble with the transaction class. When I tell it to deposit and withdraw it doesn't withdraw or deposit and the amounts in both accounts stay the same. Any help is appreciated!