0

This is an extension of one of my previous post, I have written the error part as a new class.

The idea behind is to create different objects, and use the get() method as user input, maybe I have omitted key concepts in Java, this is why I have not been able to run the code.

Any clarification would be greatly appreciated.

package ArrayListExample;

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

public class ArrayListTable {

class Food{
    
    private int  id;
    private String description;
    private String sellingPrice;
    private List<String> ingredients;
    
    Scanner sc = new Scanner(System.in);
    
    public Food(int id, String description, String sellingPrice, List<String> ingredients) {
        super();
        this.id = id;
        this.description = description;
        this.sellingPrice = sellingPrice;
        this.ingredients = ingredients;
    }

    public int getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public String getSellingPrice() {
        return sellingPrice;
    }

    public List<String> getIngredients() {
        return ingredients;
    }

    public void setId(int id) {
        System.out.println("Input food ID");
        id = sc.nextInt();
        this.id = id;
    }

    public void setDescription(String description) {
        System.out.println("Input food name");
        description = sc.next();
        this.description = description;
    }

    public void setSellingPrice(String sellingPrice) {
        System.out.println("Input food Price");
        sellingPrice = sc.next();
        this.sellingPrice = sellingPrice;
    }

    public void setIngredients(List<String> ingredients) {
        List<String> ingredient = new ArrayList<String>();
        
        int ingredientCount;
        System.out.println("Enter the number of Ingredients to add: ");
        ingredientCount = sc.nextInt();
        
        for(int i=0; i < ingredientCount; i++) {
            
            System.out.println("Enter the Ingredients: ");
            ingredient.add(sc.next());
        }
        this.ingredients = ingredients;
    }
    
    
}

public static void main(String[] args) {
    List<Food> foods = new ArrayList<>();
    foods.add(new Food(getId(), getDescription(), getSellingPrice(), getIngredients()));

    
    
    System.out.println("---------------------------------------------------------------------------");
    System.out.printf("%5s %15s %20s %31s", "FOOD ID", "DESCRIPTION", "SELLING PRICE(Rs)", "INGREDIENTS" + "\n"); 
    System.out.println("---------------------------------------------------------------------------");
    
    for (Object  food : foods) {
        System.out.format("%5s %17s %20s %30s", getId(), getDescription(), getSellingPrice(), getIngredients());
        System.out.println(); 
        
    }
    
}

private static int getId() {
    // TODO Auto-generated method stub
    return 0;
}

private static String getDescription() {
    // TODO Auto-generated method stub
    return null;
}

private static String getSellingPrice() {
    // TODO Auto-generated method stub
    return null;
}

private static List<String> getIngredients() {
    // TODO Auto-generated method stub
    return null;
}

}

In my opinion the error lies in the following line of code:

foods.add(new Food(getId(), getDescription(), getSellingPrice(), getIngredients()));

The following error is shown:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
No enclosing instance of type ArrayListTable is accessible. Must qualify the allocation   
with an enclosing instance of type ArrayListTable (e.g. x.new A() where x is an instance     
of ArrayListTable).
at ArrayList/ArrayListExample.ArrayListTable.main(ArrayListTable.java:80)
raj_b
  • 1
  • 3
  • Why is `Food` an inner class of `ArrayListTable`? Either make it a top-level class (e.g. by moving it out of `ArrayListTable`, preferably in its own file), or make it a nested class by declaring it as `static class Food ...`. Inner classes need to be created in the context of its enclosing class, but there is none as you're trying to create `Food` in a static method. – Mark Rotteveel Jun 19 '22 at 10:20
  • Here you are again, you accepted the answer on your previous question but seems it didn't satisfied you. Seems you don't underand some object-oriented programation basic, `getId()` can be called ON a food object only, not standalone like you od – azro Jun 19 '22 at 10:21

0 Answers0