So, My class called InventoryList
that has an Arraylist, and has methods that add new books, deletes a book, get the price of all books, ect.
Now, it all works but when I run it and I try to output the list of books i added it all come out the same. So if I input the following:
- ISBN: 1111 ----- ISBN: 2222
- Ttile: cool ------- Ttile: why
- Year: 2018 ----- Year: 2018
- Author: llama -- Author: llama
- Price: 20.00 ---- Price: 20.00
When I out put it as a list I get
2222 why 2018 llama 20.00
2222 why 2018 llama 20.00
I don't know whats wrong, and I've searched everywhere.
What is wrong with my code?
This is the Inventory class
where I store the ISBN, title, year, author, and price.
package bookStore;
public class Inventory {
private int isbn;
private String title;
private int year;
private String author;
private double price;
public Inventory() {
this.isbn = 0;
this.title = "";
this.year = 0;
this.author = "";
this.price = 0.0;
}
public Inventory(int isbn, String title, int year, String author, double price) {
this.isbn = isbn;
this.title = title;
this.year = year;
this.author = author;
this.price = price;
}
//Getters
public int getIsbn() {
return this.isbn;
}
public String getTitle() {
return this.title;
}
public int getYear() {
return this.year;
}
public double getPrice() {
return this.price;
}
public String getAuthor() {
return this.author;
}
//Setters
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public void setTitle(String title) {
this.title = title;
}
public void setYear(int year) {
this.year = year;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return ("ISBN: " + isbn + "\t"
+ "Title: " + title + "\t"
+ "Year: " + year + "\t"
+ "Author: " + author + "\t"
+ "Price: " + price);
}
}
This is the Edited InventoryList
class with the ArrayList and it's methods.
package bookStore;
import java.util.ArrayList;
public class InventoryList {
private int isbn;
private String title;
private int year;
private String author;
private double price;
Inventory books = new Inventory(isbn, title, year, author, price);
ArrayList<Inventory>list = new ArrayList<Inventory>();
//adding new books
public void addBook(int isbn, String title, int year, String author, double price) {
list.add(new Inventory(isbn, title, year, author, price));
}
//delete a book using its ISBN number
//given by professor
public void delete(int isbn) {
int index = 0;
for(Inventory listBook : list) {
if(books.getIsbn() == isbn) {
index = list.indexOf(listBook);
delete(index);
}
}
}
//Searches for a book
public int searchBook(int isbn) {
int index = 0;
for(Inventory listBook : list) {
if(books.getIsbn() == isbn) {
index = list.indexOf(listBook);
}
}
return index;
}
//print out books of year chosen by user
public void bookYear(int year) {
for(Inventory listBook : list) {
if(books.getYear() == year) {
list.indexOf(listBook);
}
}
}
//print out the sum of all books price
public double priceAll(double price) {
int price1 = 0;
for(Inventory listBook : list) {
if(books.getPrice() == price) {
list.indexOf(listBook);
price1 += price;
}
}
return price1;
}
//print out all books
public void listBooks(int isbn, String title, int year, String author, double price) {
for(Inventory listBook : list) {
System.out.println(listBook.getIsbn() + "\t"
+ listBook.getTitle() + "\t"
+ listBook.getYear() + "\t"
+ listBook.getAuthor() + "\t"
+ listBook.getPrice());
}
}
}
Edit :This is the Main I'm using
package bookStore;
import java.util.Scanner;
public class InventoryClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
int isbn = 0;
String title = "";
int year = 0;
String author = "";
double price = 0.0;
int menu = 0;
int isbn2 = 0;
int isbn3 = 0;
InventoryList book = new InventoryList();
Scanner scan = new Scanner(System.in);
do {
System.out.println("\n1 - New Book");
System.out.println("2 - Books By Year");
System.out.println("3 - Total of Inventory Price");
System.out.println("4 - Search Book");
System.out.println("5 - Erase Book");
System.out.println("6 - List of All Books");
System.out.println("7 - Exit");
System.out.print("\nEnter Number from Menu: ");
menu = scan.nextInt();
if(menu == 1) {
book.addBook(isbn, title, year, author, price);
System.out.print("Enter ISBN: ");
isbn = scan.nextInt();
System.out.print("Enter Title: ");
title = scan.next();
System.out.print("Enter Year: ");
year = scan.nextInt();
System.out.print("Enter Author: ");
author = scan.next();
System.out.print("Enter Price: ");
price = scan.nextDouble();
}
if(menu == 2) {
}
if(menu == 3) {
System.out.println(book.priceAll(price));
}
if(menu == 4) {
System.out.println("Enter ISBN of Book you wish to find: ");
isbn3 = scan.nextInt();
book.searchBook(isbn3);
}
if(menu == 5) {
System.out.println("Enter ISBN of Book you wish to delete: ");
isbn2 = scan.nextInt();
book.delete(isbn2);
System.out.println("Book Deleted");
}
if(menu == 6) {
book.listBooks(isbn, title, year, author, price);
}
}while(menu != 7);//Exit
System.out.println("\nGood Bye!");
}
}
Don't try and lazily add your books. This
Inventory books = new Inventory(isbn, title, year, author, price);
ArrayList<Inventory>list = new ArrayList<Inventory>();
//adding new books
public void addBook(int isbn, String title, int year, String author, double price) {
list.add(books);
books.setIsbn(isbn);
books.setTitle(title);
books.setYear(year);
books.setAuthor(author);
books.setPrice(price);
}
should look like
List<Inventory> list = new ArrayList<>();
// adding new books
public void addBook(int isbn, String title, int year, String author, double price) {
list.add(new Inventory(isbn, title, year, author, price));
}
This:
public void addBook(int isbn, String title, int year, String author, double price) {
list.add(books);
books.setIsbn(isbn);
books.setTitle(title);
books.setYear(year);
books.setAuthor(author);
books.setPrice(price);
}
Should be:
public void addBook(int isbn, String title, int year, String author, double price) {
books.setIsbn(isbn);
books.setTitle(title);
books.setYear(year);
books.setAuthor(author);
books.setPrice(price);
list.add(books);
}
And, this:
public void listBooks(int isbn, String title, int year, String author, double price) {
for(Inventory listBook : list) {
books.setIsbn(isbn);
books.setTitle(title);
books.setYear(year);
books.setAuthor(author);
books.setPrice(price);
System.out.println(books.getIsbn() + "\t"
+ books.getTitle() + "\t"
+ books.getYear() + "\t"
+ books.getAuthor() + "\t"
+ books.getPrice());
//return listBook;
}
//return books.getIsbn();
}
Should be:
public void listBooks() {
for(Inventory listBook : list) {
System.out.println(listBook.getIsbn() + "\t"
+ listBook.getTitle() + "\t"
+ listBook.getYear() + "\t"
+ listBook.getAuthor() + "\t"
+ listBook.getPrice());
}
}
EDIT This is wrong inside your main.
if(menu == 1) {
book.addBook(isbn, title, year, author, price);
System.out.print("Enter ISBN: ");
isbn = scan.nextInt();
System.out.print("Enter Title: ");
title = scan.next();
System.out.print("Enter Year: ");
year = scan.nextInt();
System.out.print("Enter Author: ");
author = scan.next();
System.out.print("Enter Price: ");
price = scan.nextDouble();
}
You are first adding book with empty values, and then assigning values to variables. You should put it like this:
if(menu == 1) {
System.out.print("Enter ISBN: ");
isbn = scan.nextInt();
System.out.print("Enter Title: ");
title = scan.next();
System.out.print("Enter Year: ");
year = scan.nextInt();
System.out.print("Enter Author: ");
author = scan.next();
System.out.print("Enter Price: ");
price = scan.nextDouble();
// this should be at end, after you assign values to variables
book.addBook(isbn, title, year, author, price);
}
Your code has couple of bugs let's fix one by one
1) To add Book object to list
//adding new books
public void addBook(int isbn, String title, int year, String author, double price) {
list.add(new Inventory(int isbn, String title, int year, String author, double price));
}
OR
//adding new books
public void addBook(int isbn, String title, int year, String author, double price) {
Inventory books = new Inventory();
books.setIsbn(isbn);
books.setTitle(title);
books.setYear(year);
books.setAuthor(author);
books.setPrice(price);
list.add(books);
}
2) To display all the books in list, just print the book properties
//print out all books
public void listBooks(int isbn, String title, int year, String author, double price) {
for(Inventory listBook : list) {
System.out.println(listBook.getIsbn() + "\t"
+ listBook.getTitle() + "\t"
+ listBook.getYear() + "\t"
+ listBook.getAuthor() + "\t"
+ listBook.getPrice());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.