You could use a better Oriented Object approach to solve your problem.
Store extends Items this has not sense. The store contains items, so you only need a variable like your listOfItems for save all the items of the store.
Your public class Store is a good candidate to use the Singleton Pattern.
About the construction of your listOfItems: When a List is enough, then simply you should use just a List. Also, java 7 provide the type inference for generic instance creation.
From The Java SE Documentation: You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the diamond.
So, you should use List<Item> listOfItems = new ArrayList<>() instead of ArrayList<Item> listOfItems = new ArrayList<Item>()
Each time that you use a Scanner you should close it.
The Singleton:
public class Store {
private static final Store INSTANCE = new Store();
private List<Item> listOfItems = new ArrayList<>();
private Store() {
// Private Constructor
// will prevent the instantiation of this class directly
}
public static Store getInstance() {
return INSTANCE;
}
public void addItems(List<Item> newlistOfItems) {
listOfItems.addAll(newlistOfItems);
}
public String printListOfItems() {
StringBuilder sb = new StringBuilder();
for (Item item : listOfItems) {
sb.append(" [NAME : " + item.getName() + ", PRICE : " + item.getPrice() + "]");
}
return sb.toString();
}
}
The POJO Item class
public class Item {
private String name;
private double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
The service interface for the management of items :
public interface ItemManagerService {
List<Item> createListOfItems();
}
The implementation:
public class ItemManagerServiceImpl implements ItemManagerService {
@Override
public List<Item> createListOfItems() {
List<Item> newListOfItems = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
try {
do {
System.out.println("ENTER NAME");
String name = scanner.nextLine();
System.out.println("ENTER PRICE");
while (!scanner.hasNextDouble()) {
System.out.print("You must enter a valid number! Try again: ");
scanner.next();
}
double price = scanner.nextDouble();
Item item = new Item(name, price);
newListOfItems.add(item);
System.out.println("Continue?[Y/N]");
scanner.nextLine();
} while (scanner.nextLine().equalsIgnoreCase("y"));
} finally {
scanner.close();
}
return newListOfItems;
}
}
A simple test :
public class MainApp {
public static void main(String[] args) {
ItemManagerService itemManagerService = new ItemManagerServiceImpl();
List<Item> newlistOfItems = itemManagerService.createListOfItems();
Store.getInstance().addItems(newlistOfItems);
System.out.println(Store.getInstance().printListOfItems());
}
}
The console output:
ENTER NAME
table
ENTER PRICE
12
Continue?[Y/N]
y
ENTER NAME
car
ENTER PRICE
50,8
Continue?[Y/N]
n
[NAME : table, PRICE : 12.0] [NAME : car, PRICE : 50.8]