如何存储60个位置字符串和int的数组

时间:2014-04-30 12:07:51

标签: java arrays

我想创建一个图书馆系统,它有一个菜单:

1。)添加新书
2.)编辑一本书 3.)从ISBN中搜索书籍 4.)显示所有书籍 5.)从ISBN中删除一本书 6.)退出

为了保存书籍,我必须使用60个数组的数组。这本书将有:
1)Isbn(字符串)
2)姓名(字符串)
3)作者(字符串)
4)书籍数量(int)

到目前为止,我已经做到了这一点。


//This goes above your class declaration, this is importing the Scanner class.
import java.lang.reflect.Array;
import java.lang.Object.*;
import java.util.Scanner;
import java.util.Arrays;




public class BookLibrary {

public static void main (String[] args)
{

     // allocates memory for 60 slots
    String[] book_array;

    //Program keeps running till this condition updates. Only updates
    //if the user "chooses" quit option.
    boolean keepRunning = true;
    //User choice from the menu is stored here.
    int userChoice = 0;




    Scanner keyboard = new Scanner(System.in);

     while (true) 
     { 
     //your code here 
    System.out.println("");
    System.out.println(""); 
    System.out.println("Choose:");

    System.out.println("1:Add a new Book");
    System.out.println("2:Edit Book Infos");
    System.out.println("3:Search a Book (From ISBN)");
    System.out.println("4:Show all the Books");
    System.out.println("5:Delete a Book (From ISBN)");
    System.out.println("6:Exit");


     int  choose = keyboard.nextInt();

    if (choose == 1)
    {
        System.out.println("You Choose to Add a New Book1 ");
        String zero = keyboard.nextLine();
        System.out.println("Book Name:");
        String book = keyboard.nextLine();
        System.out.println("Author:");
        String author = keyboard.nextLine();
        System.out.println("ISBN:");
        String isbn = keyboard.nextLine();
        System.out.println("# of Books:");
        int numbooks = keyboard.nextInt();
        System.out.println("");
        System.out.println("");



        String bookinfo = book + "    ," + author + "    ," + isbn;

        //String[] splits = bookinfo.split(",");

       System.out.println(bookinfo);
       System.out.println("Name of Book  |   " + "Author   |   " + "ISBN");
      // System.out.println(splits[0] +" | "+ splits[1] +" | "+ splits[2]);



    }
    else
        if (choose == 2)
        {
            System.out.println("You Choose to Edit an existing  Book ");
            String zero = keyboard.nextLine();

        }
        else
            if (choose == 3)
            {
                System.out.println("You Choose to Search a  Book ");
                String zero = keyboard.nextLine();

            }
            else
                if (choose == 4)
                {
                    System.out.println("You Choose to  view all       the Books  ");
                    String zero = keyboard.nextLine();

                }
                else
                    if (choose == 5)
                    {
                        System.out.println("You Choose to Delete a Book ");
                        String zero = keyboard.nextLine();
                    }
                    else
                        if(choose == 6)
                        {
                            System.out.println("Library System Terminated!!! ");
                            String zero = keyboard.nextLine();
                            System.exit(0);
                        }
                        else
                        {
                            System.out.println("Wrong Choice");
                        }
    }
}

}

你能帮忙吗?

2 个答案:

答案 0 :(得分:2)

String[] book_array = new String[60];

不确定我理解你的问题。

更好的方法: 只需使用Book:

类型的数组或列表
Book[] books = new Book[60];

虽然您的图书类型包含以下内容:

public class Book {

  private String isbn;
  private String name;
  private String author;
  private int number;

  // getters and setters

}

答案 1 :(得分:0)

使用面向对象的编程概念。使用ISBN,Name,Author和No作为属性创建Book对象。创建书籍类型列表。

试试这个:这是书类和构造函数

    public class Books
    {
     private String ISBN;
     private String Name;
     private String Author;
     private int NoofBooks;
    }

    public Books(String isbn,String name, string author, int noofbooks)
    {
    this.ISBN = isbn;
    this.Name = name;
    this.Author = author;
    this.NoofBooks = noofbooks;
    }

这是您修改的代码:

   List<Book> books = new ArrayList<Book>();

   if (choose == 1)
    {
    System.out.println("You Choose to Add a New Book1 ");
    String zero = keyboard.nextLine();
    System.out.println("Book Name:");
    String book = keyboard.nextLine();
    System.out.println("Author:");
    String author = keyboard.nextLine();
    System.out.println("ISBN:");
    String isbn = keyboard.nextLine();
    System.out.println("# of Books:");
    int numbooks = keyboard.nextInt();
    System.out.println("");
    System.out.println("");



    String bookinfo = book + "    ," + author + "    ," + isbn;

    books.add(new Books(isbn,book,author,numbooks); // u can keep on adding books

    }