-1

Is there any way to use the global variable in the static methods? My goal is to print the contents of an ArrayList containing objects (Food and nonFood). I thought a toString() method would be the best way to print it but cant seem to get it to work. I have tried to use global variables but when I use the this. keyword, it says cannot use this in static context. The methods have to be static because I don't want to have to instantiate the class. Any suggestions?

In the printInven() method I've also tried to do System.out.println(i+1 + " " + inven.get(i).toString()); I am completely available to use a different method of printing the objects in the ArrayList. I thought getting the global variables work would be the best way to use the toString() method.

import java.util.ArrayList;
import java.util.Scanner;

public class Inventory{
    public static ArrayList<Items> inven = new ArrayList<Items>();
    Scanner scan = new Scanner (System.in);
    private String name;
    private static int ID;
    private double price;
    private boolean in_stock;
    private boolean food;
    public static void main(String[] args) {
    
    }

    //Add Food Item
    public static void addFood(String name, int ID, double price, boolean in_stock, boolean food){
        name = name;
        ID = ID;
        price = price;
        in_stock = in_stock;
        food = food;

        Food newFood = new Food(name, ID, price, in_stock, food);
        inven.add(newFood);
    }

    //Add Non food item
    public static void addNonFood(String name, int ID, double price, boolean in_stock, boolean food){
        name = name;
        ID = ID;
        price = price;
        in_stock = in_stock;
        food = food;

        NonFood newNonFood = new NonFood(name, ID, price, in_stock, food);
        inven.add(newNonFood);
    }

    //Delete inventory
    public static void delInven(int num){
        inven.remove(num);
    }

     //Print Inventory
    public static void printInven(){
        for(int i = 0; i < inven.size(); i++){
            System.out.println(i+1 + " " + inven.get(i));
        }
    }
    

    //Clear Inventory
    public static void clearInven(){
        inven.clear();
    }

    //Replace item
    //Polymorphism for food and non food items
    public static void replaceNewNonFoodInven(int n, String name, int ID, double price, boolean in_stock, boolean food){
        name = name;
        ID = ID;
        price = price;
        in_stock = in_stock;
        food = food;
        NonFood newNonFood = new NonFood(name, ID, price, in_stock, food);
        inven.set(n, newNonFood);
    }    
}
zooboo
  • 33
  • 3
  • 1
    Your question is unclear. You want to do something you already do and explain an issue for an approach which isn't relevant/used anymore. What is your current issue? – Tom Mar 24 '21 at 03:12
  • You have a lot of pointless "x = x" assignments in your code, which makes me think you don't have a good grasp on either the scope of names, or the distinction between instance variables and class variables. – user15187356 Mar 24 '21 at 03:15
  • It would be a lot easier if you would just create an instantiated Inventory and forget about using static for this. – NomadMaker Mar 24 '21 at 03:49
  • I am trying to print the objects in the ArrayList. When I print it right now, it outputs `project.Food@7cc355be` Yes, I do not have a good grasp of java quite yet. I'm in high school and just started learning Java. If I instantiate the Inventory class, would I be able to instantiate the Food and NonFood classes as well? Any suggestions would be very much appreciated! – zooboo Mar 24 '21 at 04:38
  • Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Tom Mar 24 '21 at 10:21

1 Answers1

0

You can refer below code. I hope this will resolve you queries.

public class NonFood {

private String name;
private static int ID;
private double price;
private boolean in_stock;
private boolean food;

public NonFood(String name, int iD, double price, boolean in_stock, boolean food) {
    this.name = name;
    this.ID = iD;
    this.price = price;
    this.in_stock = in_stock;
    this.food = food;
}

@Override
public String toString() {
    return "Name: '" + this.name + "' ID: '" + this.ID + "' price: '" + this.price + "' in_stock: '" + this.in_stock + "' food: '" + this.food +"'";
}

}

public class Food {

private String name;
    private static int ID;
    private double price;
    private boolean in_stock;
    private boolean food;

public Food(String name, int iD, double price, boolean in_stock, boolean food) {
    this.name=name;
    this.ID=iD;
    this.price=price;
    this.in_stock=in_stock;
    this.food=food;
}

@Override
public String toString() {
    return "Name: '" + this.name + "' ID: '" + this.ID + "' price: '" + this.price + "' in_stock: '" + this.in_stock + "' food: '" + this.food +"'";
}

}

public class Inventory{

public static ArrayList<Object> inven = new ArrayList<Object>();
Scanner scan = new Scanner (System.in);

private String name;
private static int ID;
private double price;
private boolean in_stock;
private boolean food;

public static void main(String[] args) {

    for (int i = 0; i < 2; i++) {
        addFood("Food"+i, i, 100+i, true, true);
    }
    
    for (int i = 0; i < 2; i++) {
        addNonFood("NonFood"+i, i, 100+i, true, true);
    }
    
    printInven();
}

//Add Food Item
public static void addFood(String name, int ID, double price, boolean in_stock, boolean food){
  Food newFood = new Food(name, ID, price, in_stock, food);
    inven.add(newFood);
}

//Add Non food item
public static void addNonFood(String name, int ID, double price, boolean in_stock, boolean food){
    NonFood newNonFood = new NonFood(name, ID, price, in_stock, food);
    inven.add(newNonFood);
}

//Delete inventory
public static void delInven(int num){
    inven.remove(num);
}

 //Print Inventory
public static void printInven(){
    for(int i = 0; i < inven.size(); i++){
        System.out.println(i+1 + " " + inven.get(i));
    }
}


//Clear Inventory
public static void clearInven(){
    inven.clear();
}

//Replace item
//Polymorphism for food and non food items
public static void replaceNewNonFoodInven(int n, String name, int ID, double price, boolean in_stock, boolean food){
    NonFood newNonFood = new NonFood(name, ID, price, in_stock, food);
    inven.set(n, newNonFood);
}    

}

Ksh
  • 36
  • 2