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.
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://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());}
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:
Thanks!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";} }