如何添加数组

时间:2013-12-12 22:35:23

标签: java arrays

我需要图书馆员班级来存储书籍,因为他们被创建成阵列......我只需要帮助这样做并理解如何做到这一点。 (我是初学者。)此外,实验室的一部分是抛出异常和try / catch。我已经插入了。我希望这是对的。谢谢。

  import java.io.PrintStream;
import java.util.Scanner;
import book.Book;

public class Librarian
{
    private final static Scanner keyboard = new Scanner(System.in);
    private final static PrintStream messagePane = new PrintStream(System.out);
    private final static PrintStream output = new PrintStream(System.out);

    public void provideLibrarianServices() throws java.lang.Exception
    {
        Book aBook = null;
        boolean provideLibrarianServices = true;

        do
        {
            output.println(
              "(C)reate, \n" +


              "(U)pdate, \n" +
              "(D)isplay, \n" +
              "(F)inish.");
            output.print("What would you like to do?: ");
            String response = keyboard.next();
            keyboard.nextLine();
            output.println();
            if ( response.trim().isEmpty() || response.trim().isEmpty() )
            {
                output.println("You did not enter anything. Please enter a valid response.");
                output.println();
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("C") )
            {
                if ( aBook == null )
                {
                    aBook = createBook();
                }
                else
                {
                    output.print("A book already exists. \n" +
                      "Creating a new one will delete the current book.\n" +
                      "Would you like to continue? (Y/N): ");
                    response = keyboard.next();
                    keyboard.nextLine();
                    output.println();
                    if ( response.trim().substring(0, 1).equalsIgnoreCase("Y") )
                    {
                        aBook = createBook();
                        if ( aBook == null )
                        {
                            output.println("Invalid information given - \n" +
                              "     A Book could not be created.\n");
                        }
                        else
                        {
                            output.println("\nThe Book was created successfully.\n");
                        }
                    }
                }
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("U") )
            {
                if ( aBook != null )
                {
                    updateBook(aBook);
                }
                else
                {
                    output.println("You must create a book before you can update it!");
                    output.println();
                }
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("D") )
            {
                if ( aBook != null )
                {
                    displayBook(aBook);
                }
                else
                {
                    output.println("You must create a book before you can display it!");
                    output.println();
                }
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("F") )
            {
                provideLibrarianServices = false;
            }
            else
            {
                output.println("You did not enter a valid response. Please try again.");
                output.println();
            }
        }
        while ( provideLibrarianServices );
    }

    private Book createBook() throws java.lang.Exception
    {
        try
        {
        }
        catch ( Exception anException )
        {
            messagePane.print("Sorry, could not create a book for the following reason:"
            + anException.getMessage());
        }


        String response;
        output.println("Please enter information about the book as requested\n");

        output.print("Title: ");
        String title = keyboard.nextLine();
        output.println();

        output.print("Author: ");
        String author = keyboard.nextLine();
        output.println();

        output.print("Copyright Date (Four digit year, no month or day): ");
        int copyrightDate = keyboard.nextInt();
        output.println();

        output.print("ISBN (10 or 13 characters): ");
        String ISBN = keyboard.next();
        keyboard.nextLine();
        output.println();

        output.print("Primary Topic: ");
        String primaryTopic = keyboard.nextLine();
        output.println();

        double listPrice = 0;
        output.print("Do you know the list price? (Y/N): ");
        response = keyboard.next();
        keyboard.nextLine();
        output.println();
        if ( response == null || response.trim().isEmpty() )
        {
        }
        else if ( response.trim().substring(0, 1).equalsIgnoreCase("Y") )
        {
            output.print("List Price: ");
            listPrice = keyboard.nextDouble();
            keyboard.nextLine();
            output.println();
        }

        double acqPrice = 0;
        output.print("Do you know the aquisition price? (Y/N): ");
        response = keyboard.next();
        keyboard.nextLine();
        output.println();
        if ( response == null || response.trim().isEmpty() )
        {
        }
        else if ( response.trim().substring(0, 1).equalsIgnoreCase("Y") )
        {
            output.print("Acquisition Price: ");
            acqPrice = keyboard.nextDouble();
            keyboard.nextLine();
            output.println();
        }

        Book aBook = Book.create(
          title, author, copyrightDate,
          ISBN, primaryTopic,
          listPrice, acqPrice);

        output.println();
        output.println(aBook.toString());
        output.println();

        return aBook;

    }

    private void displayBook( Book aBook )
    {
        if ( aBook != null )
        {
            output.println();
            output.print(aBook.getTitle() + " By: " + aBook.getAuthor() + ", " +
              aBook.getCopyrightDate() + ", ISBN: " + aBook.getISBN() +
              "\n      " + aBook.getPrimaryTopic() + "|List Price: $" + aBook.getListPrice() +
              "|Acquisition Price: $" + aBook.getAcquisitionPrice() + "\n");
            output.println();
        }
        else
        {
            output.println("     !!! The supplied object handle was null !!!");
        }
    }

    private void updateBook( Book aBook )
    {
        String oldBook = aBook.toString();
        boolean stillUpdating = true;
        do
        {
            output.println("Current book information: \n" + aBook);
            output.println();
            output.print(
              "(T)itle, \n" +
              "(Au)thor, \n" +
              "(C)opyright Date, \n" +
              "(P)rimary Topic, \n" +
              "(L)ist Price, \n" +
              "(A)quisition Price, \n" +
              "(F)inish. \n" +
              "What would you like to update?: ");
            String response = keyboard.next();
            keyboard.nextLine();
            output.println();
            if ( response == null || response.trim().isEmpty() )
            {
                output.println("You did not enter anything. Please enter a valid response.\n");
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("T") )
            {
                stillUpdating = updateTitle(aBook);
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("C") )
            {
                stillUpdating = updateCopyrightDate(aBook);
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("P") )
            {
                stillUpdating = updatePrimaryTopic(aBook);
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("L") )
            {
                stillUpdating = updateListPrice(aBook);
            }
            else if ( response.trim().substring(0, 1).equalsIgnoreCase("F") )
            {
                stillUpdating = false;
            }
            else if ( response.trim().length() > 1 )
            {
                if ( response.trim().substring(0, 2).equalsIgnoreCase("Au") )
                {
                    stillUpdating = updateAuthor(aBook);
                }
                else if ( response.trim().substring(0, 2).equalsIgnoreCase("Aq") )
                {
                    stillUpdating = updateAcquisitionPrice(aBook);
                }
                else
                {
                    output.println("Invalid Response. Please try again.");
                }
            }
            else
            {
                output.println("Invalid Response. Please try again.");
            }
        }
        while ( stillUpdating );
        output.println("Original Book: \n" + oldBook);
        output.println();
        output.println("New Book: \n" + aBook);
        output.println();
    }

    private boolean updateTitle( Book aBook )
    {
        boolean stillUpdating = true;
        int aSignal;
        String theSignal;
        String oldTitle = aBook.getTitle();
        output.println("Title: " + oldTitle);
        output.print("New Title: ");
        String title = keyboard.nextLine();
        aSignal = aBook.setTitle(title);
        theSignal = book.Book.getReturnSignal(aSignal);
        output.println(theSignal + "\n" + aBook);
        return stillUpdating;
    }

    private boolean updateCopyrightDate( Book aBook )
    {
        boolean stillUpdating = true;
        int aSignal;
        String theSignal;
        int oldCopyrightDate = aBook.getCopyrightDate();
        output.println("Copyright Date: " + oldCopyrightDate);
        output.print("Copyright Date (Four digit year, no month or day): ");
        int copyrightDate = keyboard.nextInt();
        keyboard.nextLine();
        aSignal = aBook.setCopyrightDate(copyrightDate);
        theSignal = book.Book.getReturnSignal(aSignal);
        output.println(theSignal + "\n" + aBook);

        return stillUpdating;
    }

    private boolean updatePrimaryTopic( Book aBook )
    {
        boolean stillUpdating = true;
        int aSignal;
        String theSignal;
        String oldPrimaryTopic = aBook.getPrimaryTopic();
        output.println("Primary Topic: " + oldPrimaryTopic);
        output.print("New Primary Topic: ");
        String primaryTopic = keyboard.nextLine();
        aSignal = aBook.setPrimaryTopic(primaryTopic);
        theSignal = book.Book.getReturnSignal(aSignal);
        output.println(theSignal + "\n" + aBook);

        return stillUpdating;
    }

    private boolean updateListPrice( Book aBook )
    {
        boolean stillUpdating = true;
        int aSignal;
        String theSignal;
        double oldListPrice = aBook.getListPrice();
        output.println("List Price: " + oldListPrice);
        output.print("New List Price: ");
        double listPrice = keyboard.nextDouble();
        keyboard.nextLine();
        aSignal = aBook.setListPrice(listPrice);
        theSignal = book.Book.getReturnSignal(aSignal);
        output.println(theSignal + "\n" + aBook);

        return stillUpdating;
    }

    private boolean updateAuthor( Book aBook )
    {
        boolean stillUpdating = true;
        int aSignal;
        String theSignal;
        String oldAuthor = aBook.getAuthor();
        output.println("Author: " + oldAuthor);
        output.print("New Author: ");
        String author = keyboard.nextLine();
        aSignal = aBook.setAuthor(author);
        theSignal = book.Book.getReturnSignal(aSignal);
        output.println(theSignal + "\n" + aBook);

        return stillUpdating;
    }

    private boolean updateAcquisitionPrice( Book aBook )
    {
        boolean stillUpdating = true;
        int aSignal;
        String theSignal;
        double oldAcquisitionPrice = aBook.getAcquisitionPrice();
        output.println("Acquisition Price: " + oldAcquisitionPrice);
        output.print("New Acquisition Price: ");
        double acqPrice = keyboard.nextDouble();
        keyboard.nextLine();
        aSignal = aBook.setAcquisitionPrice(acqPrice);
        theSignal = book.Book.getReturnSignal(aSignal);
        output.println(theSignal + "\n" + aBook);

        return stillUpdating;
    }


}

1 个答案:

答案 0 :(得分:1)

我有太多的代码要经历这一切,只是为了保存你想要将它们存储到List中的已创建书籍的记录,在这种情况下,ArrayList可能是最好的。

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

然后添加新书只需books.add(book);

要查看有多少books.size()

查看列表API文档,了解您可以使用它做什么。

要实现这四种方法 - 将它们添加到Librarian类中。该方法的签名在要求中描述,即第一个: 数组的实例化(无参数方法,默认数组大小为4;无返回类型)

所以方法名称为instantiateArray,没有输入参数,没有返回类型:

void instantiateArray() {

它表示它创建的默认数组大小为4:

    array = new Book[4];
}

您需要将数组声明为Librarian中的字段:

Book[] array;

这是你的方法。

按照创建它们的相同方式细分其他3种方法的要求。听起来这个任务要求你出于某种原因使用数组而不是List(即使List可能会更好)。也许以后的作业会让你切换到List。