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

Thread: Problems with an Inventory Manager

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with an Inventory Manager

    I'm trying write a program to help a fictional restaurant know what things need to be ordered. The problem I'm running into right now is when I'm trying to read and store the objects' variables to an array of that object.
     //read all files found by array and fill new array with box objects
            boxObjArray = new Box[boxListArray.length];
            int k = 0;
            String temp, temp2;
            for(int j = 0; j < boxListArray.length; j++){boxObjArray[j] = new Box("", 0, 0, 0, "");}
            for(String n : boxListArray)
            {
                temp = boxFile.readFile(n);
                prevLineBrk = 0;
                for(int i = 0; i < 5; i++) //less than five because i'm lazy and there are 5 lines per file
                {
                    temp2 = temp.substring(prevLineBrk, temp.indexOf("\n", prevLineBrk));
                    prevLineBrk = temp.indexOf("\n", prevLineBrk) + 1;
                    System.out.println(temp2 + "---temp2 when k = " + k + " and i = " + i);
                    if(i == 0){boxObjArray[k].setContents(temp2);}
                    else if(i == 1){boxObjArray[k].setPrice(Double.parseDouble(temp2));}
                    else if(i == 2){boxObjArray[k].setAmount(Integer.parseInt(temp2));}
                    else if(i == 3){boxObjArray[k].setCurrentStock(Double.parseDouble(temp2));}
                    else if(i == 4){boxObjArray[k].setLast(temp2);}
                    else{System.out.println("Shit's Fucked");}
                }
                k++;
            }
            System.out.println();
            for(int i = 0; i < boxObjArray.length; i++){System.out.println(boxObjArray[i].getAll());}
    This is my code that I'm referring to, where I'm having issues is that is seems to overwrite itself, but I don't know where or how. For instance, this is the output for the code:

    lettuce---temp2 when k = 0 and i = 0
    94.99---temp2 when k = 0 and i = 1
    8---temp2 when k = 0 and i = 2
    2.0---temp2 when k = 0 and i = 3
    2019-06-14---temp2 when k = 0 and i = 4
    Beef---temp2 when k = 1 and i = 0
    32.5---temp2 when k = 1 and i = 1
    10---temp2 when k = 1 and i = 2
    2.0---temp2 when k = 1 and i = 3
    2019-06-14---temp2 when k = 1 and i = 4
    onions---temp2 when k = 2 and i = 0
    26.22---temp2 when k = 2 and i = 1
    6---temp2 when k = 2 and i = 2
    0.5---temp2 when k = 2 and i = 3
    2019-06-14---temp2 when k = 2 and i = 4
    tomatoes---temp2 when k = 3 and i = 0
    34.98---temp2 when k = 3 and i = 1
    4---temp2 when k = 3 and i = 2
    3.0---temp2 when k = 3 and i = 3
    2019-06-30---temp2 when k = 3 and i = 4

    tomatoes
    34.98
    4
    2.0
    2019-06-14

    tomatoes
    34.98
    4
    2.0
    2019-06-14

    tomatoes
    34.98
    4
    0.5
    2019-06-14

    tomatoes
    34.98
    4
    3.0
    2019-06-30

    and as you can see, the variable I'm telling it to store, temp2, holds the correct data, but as soon as the main nested for loop ends, and I print it back out, all of the values are overwritten, all except for when i = 3 and I call the .setAmount() method which confuses me even more. Anyway, this problem's been bothering me for a couple weeks now, so any help is greatly appreciated. Also if you're wondering what the class code for the Box class is, it's this:
    public class Box
    {
        //declare private statics
        private static String contents;
        private static double price;
        private static int amount;
     
        //declare private vars
        private double currentStock;
        private String last;
        private boolean hasChanged = false;
     
        //constructor
        public Box(String c, double p, int a, double cs, String l)
        {
            contents = c;
            price = p;
            amount = a;
            currentStock = cs;
            last = l;
        }
     
        //setters
        public void setContents(String c){contents = c;}
        public void setPrice(double p){price = p;}
        public void setAmount(int a){amount = a;}
        public void setCurrentStock(double cs){currentStock = cs;}
        public void setLast(String l){last = l;}
     
        //getters
        public String getContents(){return contents;}
        public double getPrice(){return price;}
        public int getAmount(){return amount;}
        public double getCurrentStock(){return currentStock;}
        public String getLast(){return last;}
        public String getAll(){return contents + "\n" + price + "\n" + amount + "\n" + currentStock + "\n" + last + "\n";}
    }
    Thanks!
    Last edited by Decyphorus; July 1st, 2019 at 09:27 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,333
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Problems with an Inventory Manager

    Please post the code as text. Be sure tp wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Also post the program's output as text, not as an image.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with an Inventory Manager

    I’m not at my computer right now, so I can’t test it, but I’m now pretty sure the problem is in the box class. I declared the contents, price, and amount variables as static for whatever reason. As soon as I test it I’ll edit this reply.

    ---That is exactly what the problem was, once I removed it, the code works as intended!
    Last edited by Decyphorus; July 1st, 2019 at 11:19 PM.

Similar Threads

  1. Inventory CSV files
    By mstratmann in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: December 24th, 2013, 11:26 PM
  2. inventory System
    By w4nz in forum What's Wrong With My Code?
    Replies: 25
    Last Post: March 17th, 2013, 10:55 AM
  3. Inventory Management System
    By Luffy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 26th, 2013, 02:11 AM
  4. Please help, my inventory code is not working
    By ggarrett in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 17th, 2013, 05:39 AM
  5. Help with inventory program
    By curmudgeon in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2013, 09:53 PM