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)