Having trouble understanding object relationships and using them correctly?
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.
Code :
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()));
}
}
Code :
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;
}
}
Code :
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!
Re: Having trouble understanding object relationships and using them correctly?
Your problem is with comparing Strings:
if(account == "joe")
String are objects, therefore when you are comparing for equivalency, you need to use the .equals() method instead of ==
if(account.equals("joe"))
Re: Having trouble understanding object relationships and using them correctly?
Quote:
Originally Posted by
aussiemcgr
Your problem is with comparing Strings:
if(account == "joe")
String are objects, therefore when you are comparing for equivalency, you need to use the .equals() method instead of ==
if(account.equals("joe"))
Thank you! That fixed the problem. I wasn't sure how to compare the two accounts, so I chose to have them enter a name instead.
Re: Having trouble understanding object relationships and using them correctly?
The "normal" way to compare two objects in java is to either:
1. override the .equals() method, in your Account class, that is inherited from the Object class
2. Implement the Comparable Interface (Comparable (Java Platform SE 6)) in your Account class.