Java Bookstore program

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

I am writing a bookstore program for homework. The program gives the user 6 different options for them to select from. Currently, the one I am working on is option 1. Option 1 is to add more books to the stock. When the user selects option 1, it asks the user to enter the book title, it then checks if the books is already in stock. The problem that I am having is checking to see if the book title that the user enters in is already in stock. I have a function called inStock, but I am unsure of how to call this function. Right now the code does not compile because I am receiving an error on the line that says if (bt == myBkStore.inStock(title, y)) because of incompatible types. But I am not sure of how to call the inStock() function. This is my coding so far:

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 } 

回答1:

bt is a String:

String bt = s.next();           

but inStock() returns a boolean:

 public boolean inStock(...)  

so when you call:

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

you are saying "if this string (bt) is equal to this boolean (returned by inStock)"...

the types here are incompatible, hence the exception

EDIT: To simply check whether the inStock returns true or false, you can simply say:

   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  

Note: (myBkStore.inStock(bt, y)) is a shorthand way of saying

 if (myBkStore.instock(title, y) == true) 


回答2:

bt is a String. The return type of your inStock method is a boolean. You need to store inStock's return value to a boolean. And then use whatever is returned from your inStock method in your if statement. For example:

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 } 


回答3:

Jus try to replace

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

with:

if (myBkStore.inStock(bt, y)) 


回答4:

Just remove bt == from your code. It should work

if (myBkStore.inStock(title, y)) {     System.out.println("How many more to add to the stock");     y = s.nextInt();     myBkStore.addBookQuantity(bt, y); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!