I have two of the following java classes (listed below) Class BookInfo declares static block of arrays

         public class BookInfo {

        // Global arrays accessible by all methods

        private static String[] isbnInfo;
        private static String[] bookTitleInfo;
        private static String[] authorInfo;
        private static String[] publisherInfo;
        private static String[] dateAddedInfo;;
        private static int[] qtyOnHandInfo;
        private static double[] wholesaleInfo;
        private static double[] retailInfo;

        static {

            isbnInfo = new String[] {

                                    "978-0060014018",
                                    "978-0449221431",
                                    "978-0545132060",
                                    "978-0312474881",
                                    "978-0547745527"

                                    };

            bookTitleInfo = new String[] {

                                    "The Greatest Stories",
                                    "The Novel",
                                    "Smile",
                                    "The Bedford Introduction to Drama",
                                    "AWOL on the Appalachian Trail"

                                    };

            authorInfo = new String[]  {

                                     "Rick Beyer",
                                     "James A. Michener",
                                     "Raina Telgemeier",
                                     "Lee A. Jacobus",
                                     "David Miller"

                                    };

            publisherInfo = new String[] {

                                    "HerperResource",
                                    "Fawcett",
                                    "Graphix",
                                    "Bedford St. Martins",
                                    "Mariner Books"

                                    };

            dateAddedInfo = new String[] {

                "05/18/2003", 
                "07/07/1992", 
                "02/01/2010", 
                "09/05/2008", 
                "11/01/2011"

                };

            qtyOnHandInfo = new int[] {7, 5, 10, 2, 8};

            wholesaleInfo = new double[] {12.91, 7.99, 6.09, 54.99, 10.17};

            retailInfo = new double[] {18.99, 3.84, 4.90, 88.30, 14.95};

        }

        public static void BookInfo() {

            System.out.println("             Serendipity Booksellers");
            System.out.println("                Book Information\n");       


            for(int i = 0; i < isbnInfo.length; i++){

                System.out.println("ISBN: " + isbnInfo[i]);
                System.out.println("Title: " + bookTitleInfo[i]);
                System.out.println("Author: " + authorInfo[i]);
                System.out.println("Publisher: " + publisherInfo[i]);
                System.out.println("Date Added: " + dateAddedInfo[i]);
                System.out.println("Quantity-On-Hand: " + qtyOnHandInfo[i]);
                System.out.println("Wholesale Cost: $ " + wholesaleInfo[i]);
                System.out.println("Retail Price: $ " + retailInfo[i]);
                System.out.println();

            }
        }
        }

How do I access array list from this class? Only the following is working so far but how do I modify (add, delete, edit, etc) from this class (there is no main main in this class) BookInfo bookinfo = new BookInfo(); bookinfo.BookInfo(); System.out.println(bookinfo.isbnInfo[0]); how do I modify (add, delete, edit, etc) from the main menu

     import java.util.Scanner;

     public class InvMenu {
     public static void addBook(){

      System.out.println("\nYou selected Add a Book\n");
       BookInfo bookinfo = new BookInfo();
      bookinfo.BookInfo(); // only these two are working but I cannot modify arrays at all
      System.out.println(bookinfo.isbnInfo[0]);

        }

       public static void editBook(){

     System.out.println("\nYou selected Edit a Book's Record\n"); 

     }

     public static void deleteBook(){

      System.out.println("\nYou selected Delete a Book\n");

    }

    public static void printInvMenu(){

    String choice;
    int x = 0;
    boolean b;
    char letter;
    boolean menu = true;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Serendipity Booksellers");
    System.out.println("Inventory Database\n");
    System.out.println("       1. Look Up a Book");
    System.out.println("       2. Add a Book");
    System.out.println("       3. Edit a Book's Record");
    System.out.println("       4. Delete a Book");
    System.out.println("       5. Return to the Main Menu\n");

    do{

        System.out.print("Enter your choice: ");
        choice = keyboard.nextLine();
        b = true;

        try {
            x = Integer.parseInt(choice);
            System.out.println(x);

        }

        catch(NumberFormatException nFE) {

            b = false;
            System.out.println("You did not enter a valid choice. Try again!\n");

        }

           }while(b == false);

        do{

        else if(x == 1){

            addBook();

        }

        else if(x == 2){

            editBook();

        }

        else if(x == 3){

            deleteBook();

        }

        else if(x == 4){

            System.out.println("Returning to the Main Menu\n");
            break;

        }

        else{

            System.out.println("\nYou did not enter a valid choice. Try again!\n");

        }

        printInvMenu();

       }while(x == 5);

          }
         }

I can easily access some of the functionality from the other class main menu: BookInfo bookinfo = new BookInfo(); bookinfo.BookInfo(); System.out.println(bookinfo.isbnInfo[0]); How do I modify (add, delete, edit, etc) from the main menu? Any ideas, suggestions are greatly appreciated!

share|improve this question
I see you have an account on Stack Overflow too... Did you post this here accidentally? – Kevin Oct 19 '12 at 0:43
did you try BookInfo.isbnInfo[0] = "new value"? that should work – user902383 Oct 19 '12 at 8:23
feedback

migrated from serverfault.com Oct 19 '12 at 7:59

1 Answer

I think you need to rethink your design a bit. I included an example of how to get some of the functionality working with your current code but doing so will create some seriously nasty code.

Statics are generally used to to share information between instance where this information is meant to be unique. The usual example is as an instance counter. So every time a instance is created it increments the values so you can track unique instance.

The problem is that bookInfo does not know what it is mean't to be. It wants to be a data store but also describes a unique object. Think about what will happen if you want to create another BookInfo. Editing any static of one instance will affect the others.

You can easily edit the information in the static String[].

public int getBookLocation(Sting name){
    return bookTitleInfo.IndexOf(name)
}

Then you can manipulate a specific entry as you please.

public void SetBookName(String oldname, String newname){
    int index = getBookLocation(oldname);
    if(index > 0){
        bookTitleInfo[index] = newname;
}

That was not too bad for editing but deleting an entry will be very messy. Here is some pseudo code

first have to find the bookindes. - getBookLocation(...)

For each entry in static string[]
    create a new one of size-1, 
        For each entry in String[]
            add existing entry to new string[].

You need to do that for each array. ie, isbnInfo, bookInfoName, ...., That is lots of needless iteration.


The following, though won't solve your question is a better design approach, separating concerns into class.

  • One class should represent your Book Object. This class will provide functionality to store/set/retrive all the data for a single book object.
  • Another will represent the bookstore, which will store a list of book objects, initialise the data with the books, provide functionality to add, remove, edit books from the store.

    public class BookInfo {

    //Class variables
    private String isbnInfo;
    private String bookTitleInfo;
    //....
    private double wholesaleInfo;
    private double retailInfo;
    
    //Constructors
    BookInfo(){
        //put default behaviour here
    }
    
    BookInfo(String isbnInfo, String bookTitleInfo, .....){
        this.isbnInfo = isbnInfo;        
        this.bookTitleInfo = bookTitleInfo;
        this.authorInfo = authorInfo;
    
        //....
    
    
    //Setter Method
    public String getIsbnInfo(){
        return isbnInfo;
    }
    
    //Getter Method
    public void setBookTitleInfo(String isbnInfo){
        this.isbnInfo = isbnInfo;
    }
    
    //.....
    

So now you can create a Book info and set/get all its variables, encapsulated in a OOP manner.

Next we will look on the storage/access class of BookInfos.

public class BookStore {
    private isbnInfo = new String[] {"978-0060014018", "978-0449221431", "978-0545132060",
                                "978-0312474881", "978-0547745527" };

    private bookTitleInfo = new String[] { "The Greatest Stories", "The Novel", "Smile",
                                "The Bedford Introduction to Drama", 
                                 "AWOL on the Appalachian Trail" };

    //...rest of strings

    //here is where we store our book objects
    //we will create methods to add, remove, edit, later
    private BookInfo booklist = new List<BookInfo>();
    private String storename;

    //Default Constructor initialises booklist based on stored values
    public BookStore(){
         for(int i = 0; i < isbnInfo.lenght; i++){
             AddBook(isbnInfo [i], bookTitleInfo[i],.....,retailInfo[i]);
         )
    }

    //overloaded constructors you probably want to read from external file, db etc eventually.
    public BookStore(String filelocation){
         //Do stuff to read from a file and add books to booklist 
    }

    //Add a new book to the list by passing in all the info
    public void AddBook(String isbn, String title, 
         BookInfo newbook = new BookInfo(isbn, title, ....)
         booklist.add(newbook);

    //Add a new book by passing in a book object.
    public void AddBook(BookInfo newbook){
         booklist.add(newbook);
    }

    //Find Book
    public int getBook(String isbn){
        for(int i=0; i++; i < booklist.lenght){
            if(book.getIsbn() == isbn)
                return i;
        }
        return -1;

    //Remove Book from list
    public void removeBook(String isbn){
         int loc = getbook(isbn);
         if(loc > -1){
             booklist.remove(loc);
         }
         else{
             System.out.println(Could not find book);
         }
    }     

    public void DisplayBookList(){
    //.. print list
    }
}

So now all the required functionality that BookStore needs is located where it needs to be. You can now add any additional functionality as needed.

Your main driver class will need to be changed up too. Your driver class needs to create the BookStore.

If the user wants to remove a book, they would enter the Book name. You then call

bookstore.removeBook(userstring);

Adding a book would require the user to enter alot more values but essentially the same

String isbn = scanner.next();
String bookname = scanner.next();
.......
double wholesaleinfo = Double.ParseDouble(scanner.next());

Then you add your can either by passing the info as as params

bookstore.addBook(isbn, bookname, ...., wholesaleinfo);

or a book object.

BookInfo newbook = new BookInfo(isbn, bookname, ...., wholesaleinfo)
bookstore.addBook(newbook);

I'll leave the user logic to whatever way you want. Hopefully that has illustrated things. There are certainly improvements which could be made on what I have done.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.