Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Having trouble understanding object relationships and using them correctly?

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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!
    Last edited by orbin; July 10th, 2012 at 10:22 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default 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"))
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding object relationships and using them correctly?

    Quote Originally Posted by aussiemcgr View Post
    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.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Replies: 3
    Last Post: July 8th, 2012, 03:44 PM
  2. Replies: 2
    Last Post: July 5th, 2012, 07:46 PM
  3. Help understanding this syntax -- new object returned
    By wy125 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 5th, 2011, 12:18 PM
  4. Having trouble understanding what teacher said for build Tree.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 07:22 PM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM