0

I have a small problem with this code. I added lecturer in case 1 and I trying to add books to this lecturer in case 3. problem is that it seems that case 3 doesn't recognize the created lecturer.

Is there any way to pass this value.

The solution is probably very simple but at this hour I just can not get it...

public class Menu {
static Scanner in = new Scanner (System.in);
LectureList lec = new LectureList(100);

BookList bl = new BookList(0);
public Menu(){

}

public int mainMenu(){
    int option = 0;

    System.out.println("---------------------------------------------------------");
    System.out.println("                    Lecturer Menue                       ");
    System.out.println("*********************************************************");
    System.out.println("1) Add Lecturer");
    System.out.println("2) Find Lecturer by ID");
    System.out.println("3) Add book to Lecturer BookList");
    System.out.println("4) Remove book from Lecturer BookList ");
    System.out.println("5) Search for a book using the ISBN number");
    System.out.println("6) Calculate the yearly book payment");
    System.out.println("7) Output all of the book details in the system to a file");
    System.out.println("8) Exit");
    boolean selected = false;

    while (selected == false)
    {


        try 
        {
            option = in.nextInt();
            in.nextLine();
            if 
                ((option == 8)){

                System.out.println("Goodbye!");
                System.exit(0);}


            else if 
            ((option <= 0) || (option > 8))
                System.out.println("Sorry but you have to choose an option between 1 and 8");
            else
                selected = true;



        }
        catch (InputMismatchException e) 
        {
            System.out.println("Sorry you did not enter a valid option");

            in.next();
        }       

    }

    return option;

}
public void menuSwitch(){
    boolean finish = false;
    if (finish == false){
        int option = mainMenu();

        switch (option){

        case 1: 
            String LecName = " ";

            System.out.println("Please enter Lecturer's  name");
            LecName = in.nextLine();
            Lecturer l = new Lecturer(LecName);


            lec.add(l);

            break;
        case 2:

            break;
        case 3:

            String name = "";
            Double price = 00.00 ;
            String isbn ="";
            String author = "";

            System.out.println("Please enter Book title ");
            name = in.nextLine();
            System.out.println("Please enter Book's price ");
            price = in.nextDouble();
            System.out.println("Please enter Book's isbn number ");     
            isbn = in.next();       
            System.out.println("Please enter book author's name");
            author = in.next();
            Book b = new Book( name,  price,  isbn,  author);

            l.addBook(b);



            break;``
            default: 
            finish = true;
            break;
        }
        menuSwitch();
    }
}
}
2

1 Answer 1

0

Every case statement in your switch finishes with a break;, and the default case does as well. In that case, for any pass through the switch statement, only a single case label will be executed.

If the first pass executes the first case, and the second pass executes the second case, the variables defined in the first case will no longer be around -- they will have fallen out of scope when the switch statements completes the first time.

Can you get a reference to your Lecturer from the LectureList collection?

In case 1: you can create it and put it in the collection; in case 3: you can obtain it back from the collection and update it.

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.