0

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.

1
  • Try to insert a print statement to see whats inside cat1 before the if-branch Commented May 12, 2014 at 13:34

1 Answer 1

5

You are using two different Lists in your program.

  • The first one is cat1in your main method (is empty)
  • The second in your constructor (is filled in the constructor)

You override the reference with the new created list in the constructor and fill that one instead. This is garbage collected after the constructor is finished and no reference is pointing on it.

In the toString method you are printing the the list that is passed via parameter which is the one from main (and empty).

Remove the cat1 = new ArrayList(); line from the constructor. Then it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.