I'm pretty new to java and what i'm trying to do is make a model for a catalog storing available products in a computer parts shop, using collections. My instructor asked for one instance of each product in the catalog. This is what i came up with:
import java.util.*;
public class AvailablesCatalog {
public AvailablesCatalog(List cat1) {
cat1 = new ArrayList();
Motherboard item1 = new Motherboard("MD4652", 1995, "Lenovo", 100.50, "Intel", 32, 5);
CPU item2 = new CPU("MD4652", 1995, "Lenovo", 100.50, 2.9, 6);
Graphics item3 = new Graphics("MD4652", 1995, "Lenovo", 100.50, "AMD", 6);
RAM item4 = new RAM("MD4652", 1995, "Lenovo", 100.50, "DDR2", 4, 1600);
HD item5 = new HD("MD4652", 1995, "Lenovo", 100.50, "SSD", 2.5, 750);
Monitor item6 = new Monitor("MD4652", 1995, "Lenovo", 100.50, "LED", 17.5, "1920x1080", "HDMI");
Keyboard item7 = new Keyboard("MD4652", 1995, "Lenovo", 100.50, "Wireless");
Mouse item8 = new Mouse("MD4652", 1995, "Lenovo", 100.50, "Laser", "Wireless");
Printer item9 = new Printer("MD4652", 1995, "Lenovo", 100.50, "Laser", "Colored");
cat1.add(item1);
cat1.add(item2);
cat1.add(item3);
cat1.add(item4);
cat1.add(item5);
cat1.add(item6);
cat1.add(item7);
cat1.add(item8);
cat1.add(item9);
}
public String toString(List cat1, int i) {
for(i=0; i<cat1.size(); i++) {
System.out.println(cat1.get(i).toString());
}
return "----------------------------------------------------";
}
}
Now, through the shop's mainApp that i'm using to to print the catalog, i have stored an instance of the AvailablesCatalog object type in a variable called av. This is the mainApp:
public class mainApp {
public static void main(String[] args){
/* Variables for Menu System and Sub Menu System */
int MainMenu;
String SubMenu;
String ReturnToMenu;
String SubMenuReturnToMenu;
List cat1 = new ArrayList();
AvailablesCatalog av = new AvailablesCatalog(cat1);
/* Displays menu system to console */
System.out.println("..............MENU...............");
System.out.println("..............1 View All Available Products..............");
System.out.println("..............2 View Orders...................");
System.out.println("..............3 View Sales...................");
System.out.println("..............0 Exit...................");
System.out.print("Please select an option: ");
Scanner sc = new Scanner(System.in);
MainMenu = sc.nextInt();
if(MainMenu == 1){
for(int i = 0; i < cat1.size(); i++) {
System.out.println(av.toString(cat1, i));
}
}
else if(MainMenu == 2) {
System.out.println("lol");
}
else if(MainMenu == 3) {
System.out.println("lol3");
}
else if(MainMenu == 4) {
System.exit(0);
}
}
}
Everything compiles smoothly, and when i run mainApp the menu shows up correctly. But when i press 1 to print the available products catalog, the programm simply ends. Options 2 and 3 are simply placeholders for now btw. Thanks in advance.