这里的java新手。在这个项目的多个部分制作模拟书店时遇到了麻烦。我现在在哪里正确地将新书放在book []数组中,而不是我放入的第一个。当我在书店类中运行方法“listTitles”和“listBooks”时,它只显示我第一个我记录的那本书。除此之外,我在实现其他两个方法时遇到了麻烦:“addNewBook”方法和书店类中的“sellBook”方法。如果你在这个页面上,请让我知道我正在做什么/不做什么来让这些方法起作用。您将看到每个方法的预期参数的注释代码。抱歉,如果它有点乱,谢谢你的时间。
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;
}
}
答案 0 :(得分:1)
private Book[] books; // all the books in this bookstore
private int totalBooks;
这是双重管理,如果不是所有图书都被列出,我的赌注是totalBooks = 1
但books.length
是> 1
。
最快的解决方案是放弃totalBooks
并在任何地方使用books.length
。
除此之外,我建议您使用ArrayList
来存储图书。
答案 1 :(得分:1)
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;
}
}
应该是:
public void listTitles(){
// Lists all of the titles of the books in the Bookstore object.
for (int i = 0; i<totalBooks; i++) {
System.out.println(books[i].getTitle());
}
return;
}
它只显示第一个的原因是因为你的代码当前循环的方式只执行一次然后点击return语句。 return意味着“返回main”,因此在这种情况下循环是无用的。
我将你的return语句移出循环并将“i ++”添加到你的循环中。它现在应该可以正常工作。
当然正如其他人所说的那样,ArrayList可能更适合这个,但我假设你正在尝试学习Java,所以你最好坚持你现在选择的东西,并了解你犯错误的地方。修复你的错误是最好的学习方法。
以后请记住ArrayLists。
-------------------------关于以下评论中的问题------------ -------- 强>
正如我所说,我仍然没有时间去看它并测试它,但这是我可以提供的线索所以你可以使用你的方法:
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 addNewBook(Book b) {
if (books.length != 0) {
// check if the title is already added
for (int i = 0; i < books.length; i++) {
if (books[i].getTitle().equals(b.getTitle())
// dont add book title again
else
// add book title
}
} else {
// add book title
}
}
正如flup已经建议的那样你可以使用Set然后将Set转换回数组。仅设置存储唯一值,Java具有用于将其转换为数组的集合的方法。