2

[EDIT: I've solved the problem. I just need to make the arrays static. I can't believe I didn't of that. Thanks for everyone's help!]

I have a bookstore program where people can buy a maximum of 5 different books. Once they choose a title, it will be added to an array for the invoice later. Choosing the titles and putting it into the array is in 2 different classes. Just for trial, I'm buying 2 books: Athletics and Autosport.

Expected output:

You bought 2 book(s)!
1) Athletics Weekly Magazine
2) Autosport Magazine

Edited List.java: I've tried both things. First is changing to i-1

In SportsMag.java:

import java.util.Scanner;

public class SportMag extends Magazine{

    Scanner scan = new Scanner(System.in);

    public void title() {
        System.out.println("");
        System.out.println("What do you want to buy?");
        System.out.println("1. Athletics Weekly");
        System.out.println("2. Autosport");
        int choice = scan.nextInt();

        List l = new List();

        if (choice==1) {
            l.totalbooks(1);
            l.booknames(1,"Athletics Weekly", "Magazine");  
        } else {
            l.totalbooks(1);
            l.booknames(1,"Autosport", "Magazine"); 
        }

        System.out.println("Do you want to go back to menu? (Yes/No)");
        String back = scan.next();

        if (back.equalsIgnoreCase("Yes")) {
            BookType b = new BookType();
            b.bookMenu();
        }

        if (back.equalsIgnoreCase("No")) {
            l.printInvoice();
        }

    }
}

In List.java (where I print the invoice):

public class List {

    static int total=0;
    public void totalbooks(int num) {
        total+=num;
    }

    String[] bookname = new String[5];
    String[] booktype = new String[5];
    static int a=0;
    public void booknames(String newBookName, String newBookType) {
        bookname[a]=newBookName;
        booktype[a]=newBookType;
        a++;
    }

    public void printInvoice() {
        System.out.println("You bought "+total+" book(s).");
        for (int i=0; i<total; i+=1) {
            System.out.println((i+1)+") "+bookname[i]+" "+booktype[i]);
        }
    }
}

The output for this is:

You bought 2 book(s).
1) null null
2) Autosport Magazine

I also tried using ArrayList:

In SportMag.Java:

//same as above, only a little difference here
        List l = new List();

        if (choice==1) {
            l.totalbooks(1);
            bookname.add("Athletics Weekly");   
        } else {
            l.totalbooks(1);
            bookname.add("Autosport");  
        }

In the List.java:

ArrayList<String> bookname = new ArrayList<String>();

public void printInvoice() {
        System.out.println("You bought "+total+" book(s).");
        for (int i=0; i<total; i+=1) {
            System.out.println(bookname.get(i));
        }
    }

I got an error in the SportMag.java that says bookname cannot be resolved. A quick fix offered was to create a local variable bookname but then it won't go to the array in List.java

I haven't learned about ArrayList so I'm not really sure what to do here.

I also tried making another version where everything is in the main method and only calls the methods in other classes to display the titles, not actually scanning the input in the other methods. I did this so that no objects die after each functions. The main became really long tho.

I don't know if this works fine for 2 books because I can't loop since everything is in the main.

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        BookType b = new BookType();
        List l = new List();

        b.bookMenu();
        int booktypechoice = scan.nextInt();

        if (booktypechoice ==1) {
            Magazine mag = new Magazine();
            mag.magtype();
            int magtypechoice = scan.nextInt();

            if (magtypechoice==1) {
                SportMag smag = new SportMag();
                smag.title();
                int smagchoice = scan.nextInt();
                SportMag sportmag = new SportMag();

                if (smagchoice==1) {
                    l.totalbooks(1);
                    l.booknames("Athletics Weekly", "Magazine");
                    System.out.println("Do you want to go back to menu? (Yes/No)");
                    String goback = scan.next();

                    if (goback.equalsIgnoreCase("Yes")) {
                        b.bookMenu();
                    }
                    if (goback.equalsIgnoreCase("No")) {
                        l.printInvoice();
                    }
                } else {
                    l.totalbooks(1);
                    l.booknames("Autosport", "Magazine");
                    System.out.println("Do you want to go back to menu? (Yes/No)");
                    String goback = scan.next();

                    if (goback.equalsIgnoreCase("Yes")) {
                        b.bookMenu();
                    }
                    if (goback.equalsIgnoreCase("No")) {
                        l.printInvoice();
                    }
                }
            } else {
                //all the other book choices goes here.
                //It's really long, but it's just like sportmag up there
            }
         }
      }
   }

How do I input the book names into the array and have it displayed correctly?

5
  • User ArrayList instead of array Commented Apr 13, 2020 at 14:55
  • @NishanthMatha OP is clearly a beginner Java student. Easy there cowboy. Commented Apr 13, 2020 at 14:58
  • 1
    @NishanthMatha I tried declaring ArrayList<String> bookname = new ArrayList<String>(); but I can't use bookname.add("Autosport"); in the other class. It will give an error that bookname cannot be resolved. When I tried using a method specifically to add the name, it still gives the same error. Commented Apr 13, 2020 at 15:20
  • please update the question with what you've tried Commented Apr 13, 2020 at 23:50
  • 1
    Sorry for late reply. I've updated the question. Commented Apr 14, 2020 at 12:48

1 Answer 1

1

I think you are having trouble with the scope of the variable List l. You create this variable inside the function title and you work with it, inserting in it the product the client requested. But then, where does the variable go from there? It just dies out at the end of the function. This object should be in a scope that will exist for as long as it is interesting.

For exemple, you can transform this variable into a property of your main class. It can be even a static class. You should find the better way to preserve your List object. As it is, it is dying as soon as the title function ends.

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

2 Comments

Oh sorry, I was using total at first and forgot to change it. I've changed it to i-1 and now the output is 1) Autosport (Magazine) 2) null (null)
You should edit your question then, since this error is not part of the question. I'll edit my answer.

Your Answer

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