Results 1 to 4 of 4
  1. #1
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default Class relationships - Chaining multiple classes - HELP!!

    I am currently working through a textbook and i am up to class relationships. i am totally lost. I have just been staring at the screen for the last 2 hours trying to get a grasp on creating new objects and passing parameters to methods in other classes.

    I am wondering if anyone can recommend an online tutorial that will help me understand the concepts behind this.

    Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    I bet yiou understand more than you think you do.
    Java Code:
    String message = "Hello World";
    System.out.println(message.indexOf("or"));
    Do you recognise/understand that code? Well guess what, it creates an object, calls two methods and passes parameters. All the things you say you don't understand.

  3. #3
    jonytek is offline Member
    Join Date
    Feb 2011
    Posts
    38
    Rep Power
    0

    Default

    To tell you the truth, not really. What i am trying to do is set up a program to purchase stocks. I have a stock, customer, transaction classes and a tester. I want the customer to initialise a new transaction and the tester initialises a new stock and customer. I want all conditions to be checked in the transaction class. So i need to be able to access parameters there. Hope this makes sense.

    I have posted the code below.

    Help is really appreciated.

    All im looking for is a couple of hints not a completed project.

    Java Code:
    public class Stock {
    	private int code;
    	private String stockName;
    	private int stockQuantity;
    	private double stockPrice;
    	
    	public Stock(int code, String stockName, int stockQuantity, double stockPrice)
    	{
    		this.code = code;
    		this.stockName = stockName;
    		this.stockQuantity = stockQuantity;
    		this.stockPrice = stockPrice;
    	}
    	public int getCode()
    	{
    		return this.code;
    	}
    	public String getStockName()
    	{
    		return this.stockName;
    	}
    	public int getStockQuantity()
    	{
    		return this.stockQuantity;
    	}
    	public double getStockPrice()
    	{
    		return this.stockPrice;
    	}
    }


    Java Code:
    public class Customer {
    	private String custName;
    	private	String address;
    	private String stockNameWanted;
    	private int stockAmountWanted;
    	private double bidPrice;
    	
    	public Customer(String custName, String address, String stockNameWanted, int stockAmountWanted, double bidPrice)
    	{
    		this.custName = custName;
    		this.address = address;
    		this.stockNameWanted = stockNameWanted;
    		this.stockAmountWanted = stockAmountWanted;
    		this.bidPrice = bidPrice;
    	}
    	public String getCustName()
    	{
    		return this.custName;
    	}
    	public String getAddress()
    	{
    		return this.address;
    	}
    	public String getStockNameWanted()
    	{
    		return this.stockNameWanted;
    	}
    	public int getStockAmountWanted()
    	{
    		return this.stockAmountWanted;
    	}
    	public double getBidPrice()
    	{
    		return this.bidPrice; 
    	}
    	public void transactionInit()
    	{
    		Transaction t = new Transaction(stockNameWanted, stockAmountWanted, bidPrice);
    	}
    	
    }


    Java Code:
    public class Transaction {
    	private double priceMargin;
    	private double totalCost;
    	
    	public Transaction (String stockNameWanted, int stockAmountWanted, double bidPrice)
    	{
    		this.priceMargin = s.getStockPrice() - c.getBidPrice();
    	}
    }


    Java Code:
    public class StockTester {
    	public static void main(String[] args)
    	{
    		
    		Stock s = new Stock(165567, "AGL", 60, 6.90);
    		Customer c = new Customer("Jony", "56A Mary St Surry Hills", "AGL", 50, 4.2);
    		
    		System.out.println();
    		
    	}
    }
    Last edited by jonytek; 02-21-2011 at 04:52 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Since I do not know what your full assignment is I can only make a few hazy guesses. However a couple of things come to mind. You have created one Stock and one Customer object but I imagine you will need to make numerous of both. Once you do that you need somewhere to store them, either an array or a List. Then you need somewhere to keep the List so you can access all the elements inside. Either in the main or in another class. eg StockManager.

    Were you told to write the transationInit method? Were you told it's return value would be null? The reason I ask is that once you have a List of Customers you could iterate over that List and ask each Customer what it's Transaction is. Now if all this is done is some central driver location, such as a main method, it has access to everything you have done so far: the List of Stock objects, the List of Customer objects, the List of Transaction objects. Then it is simply a matter of checking each Transaction to see if there is enough Stock to match.

Similar Threads

  1. help with GUI and multiple classes
    By sssss in forum Advanced Java
    Replies: 14
    Last Post: 01-16-2011, 10:08 PM
  2. Problem with multiple string in classes
    By sjaakie in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 02:48 PM
  3. uploading applet with multiple classes help
    By alacn in forum New To Java
    Replies: 3
    Last Post: 08-02-2010, 06:37 AM
  4. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 03:18 AM
  5. How to use multiple timer classes in swings
    By theone3nu in forum AWT / Swing
    Replies: 12
    Last Post: 12-30-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •