Reputation: 23
I'm very unskilled with java and programming, though I am trying to do one of my friend's home exercises! :P
The exercise consists in creating a system that shows a list of products available in a store, giving the user the ability to choose what he/she wants, asking for confirmation, then adding to a cart for a later checkout.
The problem is, I'm trying to make a class template for the products, then putting them all together in an ArrayList of objects from the class "Product". Works fine if I declare said list and add all the objects I want to it inside a function, but I can't declare it inside a class (as if it is a variable of sorts).
Here's part of the code:
package system;
class Product {
public int id;
public String name;
public double price;
public Product(int startId, String startName, double startPrice){
startId = id;
startName = name;
startPrice = price;
}
public int getId(){
return id;
}
public double getPrice(){
return price;
}
public String getName(){
return name;
}
}
//Main Class (The one that has the 'main()' function)
public class System {
ArrayList<Product> list = new ArrayList<Product>();
list.add(new Product(0, "Coca-Cola", 3.00));
list.add(new Product(1, "Mountain Dew", 2.00));
list.add(new Product(2, "Mango Smoothie", 4.50));
list.add(new Product(3,"Orange Juice", 5.00));
list.add(new Product(4, "Dr. Pepper", 4.50));
list.add(new Product(5, "Sandwich", 3.00));
list.add(new Product(6, "Hamburger", 3.50));
list.add(new Product(7, "Light Sandwich", 4.00));
//Yadda Yadda Yadda
}
The purpose of doing this is to be able to access the product object in question by simply inputting its id, which is identical to its position in the ArrayList.
e.g.:
System.out.println(list[0].getName); //Prints "Coca-Cola"
This strategy makes it easier to access each product, and modularizes it better, allowing me to add changes in the future.
The only problem is, as I've said, I can't declare it at the beginning of a class. It can only be declared inside a method, and only used in said method, apparently.
Any suggestions on how I may proceed? I tried researching a bit but couldn't find/understand any suitable answers.
Also, any tips on how to make this code neatier is very appreciated.
Thanks!
Upvotes: 2
Views: 46658
Reputation: 5420
Have a constructor in your System
class and then
class System {
List<Product> list = new ArrayList<Product>();
public class System() {
list.add(new Product(0, "Coca-Cola", 3.00));
list.add(new Product(1, "Mountain Dew", 2.00));
list.add(new Product(2, "Mango Smoothie", 4.50));
list.add(new Product(3,"Orange Juice", 5.00));
list.add(new Product(4, "Dr. Pepper", 4.50));
list.add(new Product(5, "Sandwich", 3.00));
list.add(new Product(6, "Hamburger", 3.50));
list.add(new Product(7, "Light Sandwich", 4.00));
}
}
Or,
You can
class System {
List<Product> list = new ArrayList<Product>(){
{
add(new Product(0, "Coca-Cola", 3.00));
add(new Product(1, "Mountain Dew", 2.00));
add(new Product(2, "Mango Smoothie", 4.50));
add(new Product(3,"Orange Juice", 5.00));
add(new Product(4, "Dr. Pepper", 4.50));
add(new Product(5, "Sandwich", 3.00));
add(new Product(6, "Hamburger", 3.50));
add(new Product(7, "Light Sandwich", 4.00));
}
};
}
But this is not a good way of writing this, You should not hard code the values in your code like this,
And you can not do
System.out.println(list[0].getName); //Prints "Coca-Cola"
because list is a List,
You can do
System.out.println(list.get(0).getName()); //Prints "Coca-Cola"
Upvotes: 4
Reputation: 406
If I understand correctly, you can't access the ArrayList outside of your constructor?
You can declare the ArrayList within the class, but then add items within the constructor.
So...
public class System{
ArrayList<Product> list = new ArrayList<Product>();
public System(){
list.add(param1, param2, param3);
//add stuff here.
}
}
Upvotes: 1