这里是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;
}
}