1

这里是java新手。在这个项目的多个部分制作模拟书店时遇到了麻烦。我现在所处的位置是正确地将新书粘贴到 books[] 数组中,超过了我放入的第一本。当我在书店类中运行方法“listTitles”和“listBooks”时,它只显示第一个我登录的书。除此之外,我在实现其他两种方法时遇到了麻烦:书店类中的“addNewBook”方法和“sellBook”方法。如果您在此页面上,请让我知道我在做什么/不做什么来让这些方法发挥作用。您将看到每个方法的预期参数的注释代码。抱歉,如果它有点乱,谢谢你的时间。

类:MyBookStore

public class MyBookstore {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        Bookstore mybookstore = new Bookstore();

        int user_choice;

        do {

            System.out.println();
            System.out.println("1) Add a book to the stock");
            System.out.println("2) Sell a book in stock");
            System.out.println("3) List the titles of all the books in stock");
            System.out.println("4) List all the information about the books in stock");
            System.out.println("5) Print out the gross income of the bookstore");
            System.out.println("6) Quit");
            System.out.println();
            System.out.print("please select one of the six options");
            System.out.println();
            System.out.print("your choice:");
            user_choice = s.nextInt();
            switch (user_choice) {
                case 1:
                    System.out.println("Enter a title");
                    String title = s.next();
                    System.out.println("how many pages is the book?");
                    int pages = s.nextInt();

                    System.out.println("how much does this book cost?");
                    double price = s.nextInt();
                    System.out.println("how many of these books are there in stock?");
                    int stock = s.nextInt();

                    Book c = new Book(title, pages, price, stock);
                    mybookstore.addNewBook(c);

                    break;
                case 2:
                    System.out.println("Selling books:");
                    System.out.println("Enter the title...");
                    String an = s.next();
                    System.out.println("Enter a quantity");
                    int da = s.nextInt();
                    mybookstore.sellBook(an, da);
                    break;
                case 3:
                    mybookstore.listTitles();
                    break;
                case 4:
                    mybookstore.listBooks();
                    break;
                case 5:
                    mybookstore.getIncome();

                    break;
                default:
                    System.out.println("please select from one of the six options");

            }
        } while (user_choice != '6');
    }
}

类别:书店

class Bookstore {

    private Book[] books;    // all the books in this bookstore
    private int totalBooks;   // the number of books in this bookstore
    private double grossIncome;   //the gross income of the bookstore (will be incremented when books are sold)

    public Bookstore() {

        books = new Book[100];
        totalBooks = 0;
        grossIncome = 0;

    }
    //If it is already in stock, simply ask the user to enter how many extra books to stock, and then do so. 

    public void addNewBook(Book b) {
        books[totalBooks] = b;
        totalBooks++;

        for (int i = 0; i < totalBooks; i++) {
            if (b.getTitle() == books[i].getTitle()) {
                String name = b.getTitle();
                Scanner m = new Scanner(System.in);
                System.out.println("books is already in stock, how many additonal books would you like to stock?");
                int stock = m.nextInt();


                addBookQuantity(name, stock);
            }
        }

        System.out.println("book has been logged");
        return;
    }

    public void addBookQuantity(String title, int quantity){

        // Adds quantity number of books to the book already in stock in the Bookstore object with
        // the title title. If the book is not in the Bookstore object, nothing is done.

        for (int i =0; i<totalBooks; i++) {
            if (title == books[i].getTitle()) {
                books[i].addQuantity(quantity);
                System.out.println("quantity added successfully");
                return;
            }
    }
        System.out.println("book not found.");
    }

    // Returns true if quantity or more copies of a book with the title title are contained in the Bookstore object.
    public boolean inStock(String title, int quantity){
        for (int i =0; i<totalBooks; i++) {
            if (title == books[i].getTitle()) {
                if (quantity <= books[i].getQuantity()) {return true;}
                else {return false;}
            }
        }
        return false;       
    }

        // Executes selling quantity number of books from the Bookstore object with the title title to the
        // buyer. The Bookstore object is changed to reflect the sale. The gross income of the bookstore is incremented 
        //accordingly. The method returns true is the sale was executed successfully, false otherwise.
        public boolean sellBook(String title, int quantity){
             for ( int i = 0; i < totalBooks;) {
                    if (title == books[i].getTitle()  ) {
                        books[i].subtractQuantity(quantity);
                        double l = books[i].getPrice();
                        double profit = l*quantity;
                        grossIncome = grossIncome + profit;
                      //rework this
                        System.out.println("books sold. Total store profits:" + profit);

                    }

             }
        return false;//System.out.println("Book not in stock");
        }


    public void listTitles(){
        // Lists all of the titles of the books in the Bookstore object.
        for (int i = 0; i<totalBooks; ) {
            System.out.println(books[i].getTitle()); 


                return;
            }
        }

    // Lists all of the information about the books in the Bookstore object. 
    public void listBooks(){

        for (int i = 0; i<totalBooks;) {
            System.out.println(books[i].toString()); 


              return;
            }



    }

    // Returns the total gross income of the Bookstore object.
    public double getIncome(){
        return grossIncome;


    }
}

类别:书

class Book {

    private String title;
    private int numOfPages;
    private double price;
    private int quantity;

    public Book(String theTitle, int pages, double cost, int num) {
        title = theTitle;
        numOfPages = pages;
        price = cost;
        num = quantity;
    }

    public String getTitle() {
        return title;
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }

    public String toString() {
        return "Title:" + title + "\nNumber of pages: " + numOfPages + "\nPrice:" + price + "\nquantity left:" + getQuantity();
    }

    public void subtractQuantity(int amount) {
        quantity = quantity - amount;
    }
}
4

2 回答 2

1
public void listTitles(){
        // Lists all of the titles of the books in the Bookstore object.
        for (int i = 0; i<totalBooks; ) {
            System.out.println(books[i].getTitle()); 


            return;
        }
 }

应该:

public void listTitles(){
        // Lists all of the titles of the books in the Bookstore object.
        for (int i = 0; i<totalBooks; i++) {
            System.out.println(books[i].getTitle()); 
        }
        return;
 }

它只显示第一个的原因是因为你的代码当前循环只会执行一次然后点击返回语句。return 本质上意味着“返回 main”,因此在这种情况下循环是无用的。

我将您的 return 语句移出循环并将“i++”添加到您的循环中。它现在应该可以正常工作了。

当然,正如其他人所建议的那样,ArrayList 可能更适合于此,但我假设您正在尝试学习 Java,因此最好坚持您现在选择的内容并了解您在哪里犯了错误。改正错误是最好的学习方法。

不过,请记住 ArrayLists 以备后用。

--------------关于下面评论中的问题------ --

正如我所说,我仍然没有时间查看和测试它,但是我可以提供一条线索,以便您可以使用您的方法:

public void addNewBook(Book b){
     books[totalBooks] = b;
        totalBooks ++; 

        for (int i =0; i<totalBooks; i++) {
            if ( b.getTitle() == books[i].getTitle()) {
                String name = b.getTitle();
                Scanner m = new Scanner(System.in);
                System.out.println("books is already in stock, how many additonal books would you like to stock?");
                int stock = m.nextInt();


               addBookQuantity(name, stock);
            }
        }

        System.out.println("book has been logged");
                return;


    }

您上面的方法首先将书籍添加到您的数组中。您可能希望因此更改方法:

public void addNewBook(Book b) {
    if (books.length != 0) {
        // check if the title is already added
        for (int i = 0; i < books.length; i++) {
             if (books[i].getTitle().equals(b.getTitle())
                   // dont add book title again
             else
                  // add book title
        }
    } else {
        // add book title
    }
}

正如flup已经建议的那样,尽管您可以使用 Set 然后将 Set 转换回数组。集合只存储唯一值,Java 有一种方法可以将集合转换为数组。

于 2013-04-06T22:58:08.590 回答
1
private Book[] books;    // all the books in this bookstore
private int totalBooks;  

这是一个双重管理,如果不是所有的书都被列出,我敢打赌,totalBooks = 1但是。books.length> 1

最快的解决方案是丢弃totalBooks并在books.length任何地方使用。

除此之外,我建议您使用ArrayList代替来存储书籍。

于 2013-04-06T22:47:54.143 回答