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

Thread: Why is my code storing input data in all three of my product objects?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Why is my code storing input data in all three of my product objects?

    Hello,

    I have been trying to figure out this problem for the past day and yet every tweak I make I seem to mess it up even more. I should add that I am relatively new to java and programming in general.

    My problem is that in my program, I have the user input data for one of three product objects, however when I read the data for all three objects, the same data is stored in all of them.
    Anyway this is the method from the Interface class:

    private void readInput() // the only method in the program that accepts product data from the user
        {
            Store matesStore = new Store();
            String name;
            int demandRate, productChoice;
            double setupCost, unitCost, inventoryCost, sellingPrice;
     
            Scanner console = new Scanner(System.in); 
            System.out.println("Please choose a product: (1), (2), (3)"); //this is where the user chooses which product they wish to enter data for.
            productChoice = console.nextInt();
            System.out.println("Please enter the product's name: ");
            name = console.next();   
            Store.check(name); 
            System.out.println("Please enter the product's demand rate: ");
            demandRate = console.nextInt();                         
            System.out.println("Please enter the product's setup cost: ");
            setupCost = console.nextDouble();
            System.out.println("Please enter the product's unit cost: ");
            unitCost = console.nextDouble();
            System.out.println("Please enter the product's inventory cost: ");
            inventoryCost = console.nextDouble();
            System.out.println("Please enter the product's selling price: ");
            sellingPrice = console.nextDouble();
            matesStore.addData(productChoice, name, demandRate, setupCost, unitCost, inventoryCost, sellingPrice); 
            // uses the addData() method in the Store class to set the data in the created Store object matesStore
            //continueOption();  //is meant to ask the user whether they are ready to continue but I still have some problems with it
            showInterface();
            chooseOption();
        }

    And here is the method from the Store class:

    public static void addData(int option, String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice) 
        //sets the product data for a particular product
        {
            if (option==1) setData(product1, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
            else if (option==2) setData(product2, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
            else /*(option==3)*/ setData(product3, newName, newDemand, newSetup, newUnit, newInventory, newPrice);      
        }
        private static void setData(Product product, String name, int demandRate, double setupCost, double unitCost, double inventoryCost, double sellingPrice)
        //uses the Product class' mutator methods to set the data
        {        
            Product.setName(name);
            Product.setDemand(demandRate);
            Product.setSetup(setupCost);
            Product.setUnit(unitCost);
            Product.setInventory(inventoryCost);
            Product.setPrice(sellingPrice);        
        }

    If there's any additional info you need to help, I'm happy to provide it. Any help would be greatly appreciated.

    Cheers

    Edit: The problem I had was with static variables and methods. Any one having similar problems should read about the difference between static and instance variables and methods.
    Last edited by MrPigeon; May 8th, 2014 at 12:48 AM. Reason: Solved on my own


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why is my code storing input data in all three of my product objects?

    Thanks for providing guidance for others with similar problems.

Similar Threads

  1. Storing data
    By imsuperman05 in forum Collections and Generics
    Replies: 0
    Last Post: December 23rd, 2011, 05:36 PM
  2. Help with storing objects in array? :(
    By spoonemore11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 2nd, 2011, 05:28 PM
  3. Storing User Data
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: January 21st, 2011, 09:07 AM
  4. Storing data
    By Joyce in forum Collections and Generics
    Replies: 1
    Last Post: September 20th, 2010, 09:16 AM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM

Tags for this Thread