1

ここでJavaを初めて使用します。このプロジェクトの複数の部分で問題が発生し、模擬書店を作成しました。私が今いるところは、books[]配列に最初に入れた最初の本を超えて新しい本を正しく取得しています.bookstoreクラスでメソッド「listTitles」と「listBooks」を実行すると、最初の本だけが表示されます私が記録した本。それに加えて、他の 2 つのメソッド、「addNewBook」メソッドと「sellBook」メソッドも bookstore クラスに実装するのに問題があります。このページにいる場合は、これらのメソッドを機能させるために私が何をしているのか、何をしていないのか教えてください。各メソッドの意図したパラメーターのコメント付きコードが表示されます。少し面倒な場合は申し訳ありませんが、お時間をいただきありがとうございます。

クラス: 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
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 に答える