Java书店程序
我正在为家庭作业写一个书店程序。该程序为用户提供了6个不同的选项供他们选择。目前,我正在处理的是选项1。选项1是在股票中增加更多的书籍。当用户选择选项1时,它要求用户输入图书标题,然后检查图书是否已经有库存。我遇到的问题是检查用户输入的书名是否已经有库存。我有一个名为inStock的函数,但我不确定如何调用此函数。现在代码没有编译,因为我在一行中收到一个错误,因为不兼容的类型,如果bt==myBkStore.inStocktitle,y。但是我不知道如何调用inStock函数。这是我迄今为止的编码:Java书店程序,java,Java,我正在为家庭作业写一个书店程序。该程序为用户提供了6个不同的选项供他们选择。目前,我正在处理的是选项1。选项1是在股票中增加更多的书籍。当用户选择选项1时,它要求用户输入图书标题,然后检查图书是否已经有库存。我遇到的问题是检查用户输入的书名是否已经有库存。我有一个名为inStock的函数,但我不确定如何调用此函数。现在代码没有编译,因为我在一行中收到一个错误,因为不兼容的类型,如果bt==myBkStore.inStocktitle,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
}
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
}
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
}
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.inStockbt,y是表示
if (myBkStore.instock(title, y) == true)
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.inStockbt,y是表示
if (myBkStore.instock(title, y) == true)
JU试图取代
if (bt == myBkStore.inStock(title, y))
与:
JU试图取代
if (bt == myBkStore.inStock(title, y))
与:
只需从代码中删除bt==即可。应该行得通
if (myBkStore.inStock(title, y))
{
System.out.println("How many more to add to the stock");
y = s.nextInt();
myBkStore.addBookQuantity(bt, y);
}
只需从代码中删除bt==即可。应该行得通
if (myBkStore.inStock(title, y))
{
System.out.println("How many more to add to the stock");
y = s.nextInt();
myBkStore.addBookQuantity(bt, y);
}
顺便说一下,AddBookQuantity方法中的if语句将不起作用,因为您使用==来比较字符串。您应该像在其他实例中一样使用equals。好的,这样代码行应该是iftitle.equalsbooks[i].getTitle,对吗?顺便说一下,AddBookQuantity方法中的if语句不起作用,因为您使用==来比较字符串。您应该像在其他实例中一样使用equals。好的,那么代码行应该是iftitle.equalsbooks[i]。getTitle正确吗?是的,我理解那个错误。由于某种原因,我想不出如何打电话给我的库存部去检查那本书是否有存货。非常感谢drewmore。现在我不喜欢在case语句中调用我的inStock,也不喜欢在函数本身的行中接收到错误,即if books[i].getTitle.equalsttitle{@bobGlenn请编辑您的问题或发布一个新问题,包括更新的代码和引发的实际异常。不喜欢没有给我们任何可处理的东西。PS-为什么在您的问题得到完全回答之前您会接受答案?代码会编译,但控制台窗口显示有错误。它只给出了错误发生的行,而不是实际的错误信息。没问题,我能弄清楚。我确实有另一个问题。它涉及到我的减法数量。它编译并有一定的效果,但当我选择选项4时,我选择购买的数量现在是新的库存数量。而不是采用当前的qua数量并从用户想要购买的金额中减去是的,我理解这个错误。由于某种原因,我无法想出如何调用我的inStock来检查书是否在库存中。非常感谢drewmore。现在我不喜欢在case语句中调用我的inStock,也不喜欢在函数本身的行中接收到错误,即如果书在库存中[i] .getTitle.equalstitle{@bobGlenn请编辑您的问题或发布一个新问题,包括更新的代码和引发的实际异常。不喜欢没有给我们任何可处理的东西。PS-为什么在您的问题得到完全回答之前您会接受答案?代码会编译,但控制台窗口显示有错误。它只给出了错误发生的行,而不是实际的错误信息。没问题,我能弄清楚。我确实有另一个问题。它涉及到我的减法数量。它编译并有一定的效果,但当我选择选项4时,我选择购买的数量现在是新的库存数量。而不是采用当前的qua数量,并从用户想要购买的金额中减去。谢谢,这正是我想要的。不知道为什么我想不出来。呵呵。但看起来不像
到我的inStock。@bobGlenn inStock方法是否不起作用,或者您是否收到更多错误?我在if语句的case语句中收到一个错误。如果书籍已经有库存,它不会生成真实的部分,提示用户输入还要添加多少库存。如果books[i].getTitle.equalstitle代码已编译,则inStock函数中的这一行也会收到错误,但当我测试图书是否有库存时,会在控制台上收到错误消息window@bobGlenn好吧,我试着看了看你的代码并仔细检查了一遍,但似乎有很多地方出错了。我建议将问题分解成更小的部分,然后从那里构建程序。对于所有程序,您都应该尝试一次添加一个功能,然后在继续下一个程序之前测试并确保其运行良好。如果,在你的程序崩溃后,你仍然无法理解它,那么回到这个网站,像你在这里做的那样问一些具体的问题。谢谢,这正是我想要的。不知道为什么我想不起来。呵呵。但是对我的inStock来说似乎不是这样。@bobGlenn是inStock方法不起作用,还是您得到了更多的错误?我在if语句的case语句中得到了一个错误。如果书籍已经有库存,它不会生成真实的部分,提示用户输入还要添加多少库存。如果books[i].getTitle.equalstitle代码已编译,则inStock函数中的这一行也会收到错误,但当我测试图书是否有库存时,会在控制台上收到错误消息window@bobGlenn好吧,我试着看了看你的代码并仔细检查了一遍,但似乎有很多地方出错了。我建议将问题分解成更小的部分,然后从那里构建程序。对于所有程序,您都应该尝试一次添加一个功能,然后在继续下一个程序之前测试并确保其运行良好。如果,在你的程序崩溃后,你仍然无法理解它,那么回到这个网站,像你在这里做的那样问一些具体的问题。