如何编辑.ser文件中的用户输入(序列化)

时间:2014-11-24 19:20:03

标签: java eclipse serialization user-input edit

我正在为图书馆系统制作一个程序。用户可以将书放入库(ser文件)并保存。但现在我希望能够编辑用户刚输入的数据。

例如,我要求他们提供本书的ID,名称,作者和价格。我希望能够通过要求用户输入他们想要输入的书的ID来编辑这些字段中的任何一个,然后它们将再次在屏幕上逐个显示并要求用户输入字段数据再次。我使用switch语句来获取用户输入并从那里开始。我如何编辑已在.ser文件中输入的数据?

public static void main(String[] args) {
    while (running) { // while running is set to true the while will run
        System.out.println("Enter 0 to load a library"
                + "\nEnter 1 to save and quit"
                + "\nEnter 2 to list all books in library"
                + "\nEnter 3 to add book to library"
                + "\nEnter 4 to edit a book in the library");

        int answer = userInput.nextInt();   //  takes the answer and stores it in user input

        switch (answer) {
        case 0:
            System.out.println("Enter file name to load"); // choose which library to load                                                              
            fileName = userInput.next();

            loadScript(fileName);
            break;

        case 1: // case to save and quit
            saveAndQuit();
            break;

        case 2:
            System.out.println(lib.toString());
            break;

        case 3:
            addBook();
            break;

        case 4:
            edit();
            break;

        } // close switch

    } // end while

    System.exit(0); // exits program once while loop is done
} // end main

还有另外两个类,一个是图书馆和一个书类,虽然我不认为你需要看到它们。这是我用来添加书籍的代码,我想你可以把它混合一点吗?

    public static void addBook() {  //  this method will add a book to the library
    int id;
    double price;
    String title, author;

    System.out.print("\nEnter book id: ");
    id = userInput.nextInt();

    System.out.print("\nEnter book title(one word/use _ for space): ");
    title = userInput.next();

    System.out.print("\nEnter book author: ");
    author = userInput.next();

    System.out.print("\nEnter book price: ");
    price = userInput.nextDouble();

    Book b = new Book(id, title, author, price);
    lib.addBook(b); // adds the book to the library

} // end addBook method

0 个答案:

没有答案