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 7 of 7

Thread: Dont know whats happening

  1. #1
    Junior Member JJTierney's Avatar
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Dont know whats happening

    Hello, i am trying to create a program that prints out a stock list.

    These are my classes.
    package ca1;
     
    public abstract class Store 
    {
     
    	private static int partCode;
    	private static String description;
    	private static String binNum;
    	private static int qtyInStock;
    	private static int reOrderPoint;
    	private static int maxNum;
     
    	public Store(int partCode, String description, String binNum, int qtyInStock, int reOrderPoint, int maxNum)
    	{
    		Store.setPartCode(partCode);
    		Store.setDescription(description);
    		Store.setBinNum(binNum);
    		Store.setQtyInStock(qtyInStock);
    		Store.setReOrderPoint(reOrderPoint);
    		Store.setMaxNum(maxNum);	
    	}
     
    	public static void setPartCode(int partCode) 
    	{
    		Store.partCode = partCode;
    	}
     
    	public static int getPartCode() 
    	{
    		return partCode;
    	}
     
    	public static void setDescription(String description) 
    	{
    		Store.description = description;
    	}
     
    	public static String getDescription() 
    	{
    		return description;
    	}
     
    	public static void setBinNum(String binNum) 
    	{
    		Store.binNum = binNum;
    	}
     
    	public static String getBinNum() 
    	{
    		return binNum;
    	}
     
    	public static void setReOrderPoint(int reOrderPoint)
    	{
    		Store.reOrderPoint = reOrderPoint;
    	}
     
    	public static int getReOrderPoint() 
    	{
    		return reOrderPoint;
    	}
     
    	public static void setQtyInStock(int qtyInStock) 
    	{
    		Store.qtyInStock = qtyInStock;
    	}
     
    	public static int getQtyInStock() 
    	{
    		return qtyInStock;
    	}
     
    	public static void setMaxNum(int maxNum) 
    	{
    		Store.maxNum = maxNum;
    	}
     
    	public static int getMaxNum() 
    	{
    		return maxNum;
    	}
    }
     
     
     
     
    public abstract class Food extends Store
    {
     
    	private static String expiryDate;
     
    	public Food(int partCode, String description, String binNum, int qtyInStock, int reOrderPoint, int maxNum, String expiryDate) 
    	{
    		super(partCode, description, binNum, qtyInStock, reOrderPoint, maxNum);
    		Food.expiryDate = expiryDate;
    	}
     
    	public static void setExpiryDate(String expiryDate) 
    	{
    		Food.expiryDate = expiryDate;
    	}
     
    	public static String getExpiryDate() 
    	{
    		return expiryDate;
    	}
     
    }
     
     
    public class CheddarCheese extends Food 
    {
     
    	public CheddarCheese(int partCode, String description, String binNum, int qtyInStock, int reOrderPoint, int maxNum, String expiryDate) 
    	{
    		super(partCode, description, binNum, qtyInStock, reOrderPoint, maxNum, expiryDate);
    	}
    }
     
     
    public class DanishBlueCheese extends Food
    {
     
    	public DanishBlueCheese(int partCode, String description, String binNum, int qtyInStock, int reOrderPoint, int maxNum, String expiryDate) 
    	{
    		super(partCode, description, binNum, qtyInStock, reOrderPoint, maxNum, expiryDate);
    	}
    }
     
     
    public class StockList
    {
     
    	public static void printDetails()
    	{
    		System.out.println("Stock List");
    		System.out.println("Part Code" + "	Description" + "		Quantity In Stock");
    		System.out.println(CheddarCheese.getPartCode() + "		" + CheddarCheese.getDescription() + "		" + CheddarCheese.getQtyInStock());
    		System.out.println(DanishBlueCheese.getPartCode() + "		" + DanishBlueCheese.getDescription() + "		" + DanishBlueCheese.getQtyInStock());
    	}
    }
     
     
    public class TestStore 
    {
    	public static void main(String[] args) 
    	{
     
    		Food CheddarCheese = new CheddarCheese(324, "Cheddar Cheese", "5c", 15, 10, 20, "01/11/2009");
    		Food DanishBlueCheese = new DanishBlueCheese(396, "Danish Blue Cheese", "6c", 8, 8, 10, "03/11/2009");
     
    		StockList.printDetails();
    	}
     
    }

    This is the output.

    Stock List
    Part Code Description Quantity In Stock
    396 Danish Blue Cheese 8
    396 Danish Blue Cheese 8

    Can anyone tell me why it is outputting the same thing twice?
    I am a beginner programmer so i would appreciate if you could make it as basic as i have it.

    Jason.
    Last edited by copeg; December 3rd, 2010 at 07:41 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,271
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Dont know whats happening

    For future reference, please flank your code with the [highlight=java][/highlight] or the code tags. I've edited your post to account for this.

    All the variables are static, meaning if they change for one they change for all. See Understanding Instance and Class Variables

  3. #3
    Junior Member JJTierney's Avatar
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dont know whats happening

    So how do i fix it so that the variables are not static and that

    public class StockList
    {
     
    	public static void printDetails()
    	{
    		System.out.println("Stock List");
    		System.out.println("Part Code" + "	Description" + "		Quantity In Stock");
    		System.out.println(CheddarCheese.getPartCode() + "		" + CheddarCheese.getDescription() + "		" + CheddarCheese.getQtyInStock());
     
    		System.out.println(DanishBlueCheese.getPartCode() + "		" + DanishBlueCheese.getDescription() + "		" + DanishBlueCheese.getQtyInStock());
    	}
    }

    Does not ask me to change the CheddarCheese.getPartCode() etc to static variables again?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,271
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Dont know whats happening

    Study the link I posted above. Remove the statics from all the functions and variables and access them through an instance.
    CheddarCheese cc = new CheddarCheese(//with arguments//);
    cc.getPartCode();

  5. #5
    Junior Member JJTierney's Avatar
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dont know whats happening

    I have done that and it works, however this is how my test class looks.

    public class TestStore 
    {
    	public static void main(String[] args) 
    	{
     
    		CheddarCheese cc = new CheddarCheese(324, "Cheddar Cheese", "5c", 15, 10, 20, "01/11/2009");
    		DanishBlueCheese dbc = new DanishBlueCheese(396, "Danish Blue Cheese", "6c", 8, 8, 10, "03/11/2009");
     
    		System.out.println("Stock List");
    		System.out.println("Part Code" + "	Description" + "		Quantity In Stock");
    		System.out.println(cc.getPartCode() + "		" + cc.getDescription() + "		" + cc.getQtyInStock());
    		System.out.println(dbc.getPartCode() + "		" + dbc.getDescription() + "	" + dbc.getQtyInStock());
     
    	}
    }

    Is there a way for me to make my test class look like this

    public class TestStore 
    {
    	public static void main(String[] args) 
    	{
     
    		CheddarCheese cc = new CheddarCheese(324, "Cheddar Cheese", "5c", 15, 10, 20, "01/11/2009");
    		DanishBlueCheese dbc = new DanishBlueCheese(396, "Danish Blue Cheese", "6c", 8, 8, 10, "03/11/2009");
     
    		StockList.printDetails();
    	}
    }

    So that the test class access the printDetails method from stocklist and prints out properly? As in i want to pass in the getPartCode() etc method into the printDetails method in the StockList class and have it call the printDetails method from StockList.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,271
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Dont know whats happening

    Pass references of the variables to the printDetails method. You have to reform the method declaration to accept the variables
    public static void printDetails(Food food1, Food food2){
    ...
                System.out.println(food1.getPartCode() + "     " + food1.getDescription() + "     " + food1.getQtyInStock());
    ...
    }
    ....
    StockList.printDetails(cc, dbc);

  7. The Following User Says Thank You to copeg For This Useful Post:

    JJTierney (December 4th, 2010)

  8. #7
    Junior Member JJTierney's Avatar
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Dont know whats happening

    Thanks mate, i can finally finish this thing

Similar Threads

  1. could you tell me whats wrong....
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2010, 04:35 PM
  2. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2010, 09:07 AM
  3. I dont get it....please help
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 30th, 2010, 03:16 AM
  4. Was Happening
    By MikeSki3 in forum Member Introductions
    Replies: 2
    Last Post: May 12th, 2010, 07:08 AM
  5. Dont laugh at me
    By Neblin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2009, 08:18 AM

Tags for this Thread