1

我正在为家庭作业编写一个书店程序。该程序为用户提供了 6 个不同的选项供他们选择。目前,我正在研究的是选项 1。选项 1 是在库存中添加更多书籍。当用户选择选项 1 时,它会要求用户输入书名,然后检查这些书是否已经有货。我遇到的问题是检查用户输入的书名是否已经有货。我有一个名为 inStock 的函数,但我不确定如何调用此函数。现在代码无法编译,因为我在一行上收到一个错误,说明if (bt == myBkStore.inStock(title, y))由于类型不兼容。但我不确定如何调用 inStock() 函数。到目前为止,这是我的编码:

import java.util.Scanner;
import java.util.ArrayList;
public class MyBookstore {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    Bookstore myBkStore = new Bookstore();


    int y = 0;
    int user_choice = 2;
    boolean quit = false;

    do {
        //display menu to user
        //ask user for his choice and validate it (make sure it is between 1 and 6)
        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 (in the Bookstore object)");
        System.out.println("4) List all the information about the books in stock (in the Bookstore object)");
        System.out.println("5) Print out the gross income of the bookstore");
        System.out.println("6) Quit");
        System.out.println();
        System.out.print("Enter choice [1-6]: ");
        user_choice = s.nextInt();
        switch (user_choice) {
            case 1: System.out.println("Enter a book title");


                    String bt = s.next();           //stores title of book user enters in

                    if (myBkStore.inStock(title, y))
                    {
                        System.out.println("How many more to add to the stock");
                        y = s.nextInt();
                        myBkStore.addBookQuantity(bt, y);
                    }
                    else
                    {
                        System.out.println("Enter the amount of pages of the book: ");
                        int pages = s.nextInt();
                        System.out.println("Enter the price of the book: ");
                        double price = s.nextDouble();
                        System.out.println("Enter the quantity to add: ");
                        int quant = s.nextInt();
                        //myBkStore.Book(bt, pages, price, quant);
                        myBkStore.addNewBook(book);
                    } 


                    break;
            case 2: System.out.println("Enter book title to buy: ");
                    String bookT = s.next();
                    myBkStore.sellBook(title, y);
                    break;
            case 3: myBkStore.listTitles();
                    break;
            case 4: myBkStore.listBooks();
                    break;
            case 5: myBkStore.getIncome();
                    break;
            case 6: System.out.println("Thanks for coming");
                    quit = true;
                    break;
            default: System.out.println("\nInvalid Choice");
        }
}
while (!quit);
}

static 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)

// Constructor: A new Bank object initially doesn’t contain any accounts.
public Bookstore() {
    books = new Book[100];
    totalBooks = 0;
    grossIncome = 0;
    }

// Creates a new bank account using the customer name and the opening balance given as parameters
// and returns the account number of this new account. It also adds this account into the account list
// of the Bank calling object.
public void addNewBook(Book b) {
    if(totalBooks < books.length) {
        books[totalBooks] = b;
        totalBooks++;

    }
    else
    {
        System.out.println("\nSorry, cannot add book to stock.");
    }


}


public void addBookQuantity(String title, int quantity) {
    for (int i =0; i<totalBooks; i++) {
        if (title == books[i].getTitle()  ) {
            books[i].addQuantity(quantity);
            System.out.println("Quantity added successfully");
            return;
        }
    }

    /*int i;

    for (i = 0; i < totalbooks; i++)
    {
        if((books[i].getTitle()).equals(title))
        {
            books[i].addQuantity(quantity);
            return;
        }
    }*/


}

public boolean inStock (String title, int quantity) {
    for (int i =0; i<totalBooks; i++) {
            if (title.equals(books[i].getTitle())) {
                if (quantity <= books[i].getQuantity()) {return true;}
                else {return false;}
            }
        }
    return false;

}

public boolean sellBook(String title, int quantity){
    int i;

        boolean sellflag=false;

        // Checks to see if the books are in stock.

        boolean retval = inStock(title, quantity);

        // If so, completes the sale.

        if (retval) {

          for (i=0; i<totalBooks && !sellflag; i++) {

            if (title.equals(books[i].getTitle())) {

              books[i].subtractQuantity(quantity);

              grossIncome += (books[i].getPrice()) * quantity;

              sellflag = true;

            }

          } // for

        } // if

        return retval;

      } // sellBook






public void listTitles()
{
    for (int i = 0; i < totalBooks; i++)
    {
        System.out.println(books[i].getTitle());
    }

}

public void listBooks()
{
    int i;

    System.out.println("\nList of Books\n======");
    for (i = 0; i < totalBooks; i++)
    {
        System.out.println(books[i]);
    }
    System.out.println();
}

public double getIncome()
{
    return grossIncome;
}





}





  static class Book{

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

       public String toString(){
           return "Title: " + title + "\nNumber of pages: " + numOfPages + "\nPrice:" + price +"\nQuantity: " + quantity + "\n";


       }


       public Book book(String thetitle, int pages, double cost, int num){
         /*title = thetitle;
         numOfPages = pages;
         price = cost;
         quantity = num;*/
         return book;

       }

       public String getTitle(){
         return title;
       }

       public double getPrice(){
         return price;
       }

       public int getQuantity(){
         return quantity;
       }

       public void addQuantity(int amount){
            quantity = quantity + amount;

    }
    public void subtractQuantity(int amount)
    {
          System.out.println("Amount to buy");
        Scanner s = new Scanner(System.in);
        quantity = s.nextInt();
        quantity = quantity - amount;   
    }

}//end of class
}
4

4 回答 4

2

bt 是一个字符串。inStock 方法的返回类型是布尔值。您需要将 inStock 的返回值存储为布尔值。然后在 if 语句中使用从 inStock 方法返回的任何内容。例如:

if (myBkStore.inStock(title, y)) { // this statement basically means if (myBkStore.inStock(title, y) == true)
    // then do stuff
}

OR

if (!myBkStore.inStock(title, y)) { // this statement basically means if (myBkStore.inStock(title, y) == false)
    // then do stuff
}
于 2013-04-09T23:35:41.210 回答
2

bt 是一个字符串:

String bt = s.next();          

但 inStock() 返回一个布尔值:

 public boolean inStock(...) 

所以当你打电话时:

       if (bt == myBkStore.inStock(title, y))

你是说“如果这个字符串(bt)等于这个布尔值(由 inStock 返回)”......

这里的类型不兼容,因此例外

编辑:要简单地检查 inStock 是否返回 true 或 false,您可以简单地说:

   if (myBkStore.inStock(title, y))
        // do stuff you want to do if the inStock() method returns true
   else 
        //do stuff you want to do if the inStock() method returns false 

注:(myBkStore.inStock(bt, y))是一种简写方式

 if (myBkStore.instock(title, y) == true)
于 2013-04-09T23:35:59.693 回答
0

只是尝试更换

if (bt == myBkStore.inStock(title, y))

和:

if (myBkStore.inStock(bt, y))
于 2013-04-09T23:38:15.693 回答
0

只需从您的代码中删除 bt == 即可。它应该工作

if (myBkStore.inStock(title, y))
{
    System.out.println("How many more to add to the stock");
    y = s.nextInt();
    myBkStore.addBookQuantity(bt, y);
}
于 2013-04-09T23:40:43.803 回答