Java bookstore
Hi guys,
I am trying to complete an assignment at my school, I have been able to make the program work some how but I still need some help.
I have a function for selling books, but when i run the program it give me the following error,, can some one PLS PLS PLS help me as to what I'm doing wrong?
Error
*******************************
| Welcome to My Book Shop|
||
| Please choose one of the|
| options below: |
*******************************
1. View all Book Details!
2. Find the details of one Book!
3. Buy a book!
4. Exit from the program...
Please select only one option: 3
Enter the name of the Book you would like to buy:java guru
What is the quantity you require?10
Exception in thread"main" java.lang.NumberFormatException: For input string:"1
6.99"
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at BookInventory.getPurchaseABook(BookInventory.java:136)
at BookInventory.callOptions(BookInventory.java:51)
at BookInventory.<init>(BookInventory.java:29)
at BookInventory.main(BookInventory.java:175)
Press any key to continue...
my code for the program is as follows
import java.io.*;
publicclass BookInventory
{
String bookTitle[] ={"Java Guru","Marketing","Programming","Super Jen"};
String quantity[] ={"120","150","200","300"};
String author[] ={"Jane","Mike","Linda","Josep"};
String publisher[] ={"Jane Press","www Press","Press 1234567","Internet Press"};
String price[] ={"16.99","65.49","25.99","50.00"};
String name;
int j;
boolean done =false;
InputStreamReader input;
BufferedReader br;
public BookInventory()throws IOException
{
input =new InputStreamReader(System.in);
br =new BufferedReader(input);
System.out.println("\n\t\t*******************************");
System.out.println("\t\t| Welcome to Amie's Book|");
System.out.println("\n\t\t| Super store.|");
System.out.println("\n\t\t| Please choose one of the|");
System.out.println("\n\t\t| options below: |");
System.out.println("\t\t*******************************");
callOptions();
}
publicvoid callOptions()throws IOException
{
System.out.println("\n1. View all Book Details!");
System.out.println("2. Find the details of one Book!");
System.out.println("3. Buy a book!");
System.out.println("4. Exit from the program...");
System.out.print("\n\tPlease select only one option:\t");
String readLine = br.readLine();
int read = Integer.parseInt( readLine );
if(read == 1)
{
getAllBooks();
}
elseif(read == 2)
{
getOneBook();
}
elseif(read == 3)
{
getPurchaseABook();
}
elseif(read == 4)
{
System.out.println("\nProgram exited");
System.exit(0);
}
else
{
System.out.println("\nPlease select only from the options above");
callOptions();
}
}
publicvoid getAllBooks()throws IOException
{
System.out.println("\nBook TitleAuthorPublisherQuantityPrice");
System.out.println("================================================================================");
System.out.println(bookTitle[0]+" "+author[0]+""+publisher[0]+""+quantity[0]+" "+price[0]);
System.out.println(bookTitle[1]+" "+author[1]+""+publisher[1]+""+quantity[1]+" "+price[1]);
System.out.println(bookTitle[2]+""+author[2]+""+publisher[2]+""+quantity[2]+" "+price[2]);
System.out.println(bookTitle[3]+" "+author[3]+""+publisher[3]+" "+quantity[3]+" "+price[3]);
callOptions();
}
publicvoid getOneBook()throws IOException
{
System.out.print("\nEnter the name of a Book:\t");
String name = br.readLine();
for(int i=0;i<bookTitle.length;i++)
{
if(name.equalsIgnoreCase(bookTitle[i]))
{
done =true;
if(bookTitle[i] =="Java Guru")
{
System.out.println("\nBook TitleAuthorPublisherQuantityPrice");
System.out.println("================================================================================");
System.out.println(bookTitle[i]+" "+author[i]+""+publisher[i]+""+quantity[i]+" "+price[i]);
}
elseif(bookTitle[i] =="Marketing")
{
System.out.println("\nBook TitleAuthorPublisherQuantityPrice");
System.out.println("================================================================================");
System.out.println(bookTitle[i]+" "+author[i]+""+publisher[i]+""+quantity[i]+" "+price[i]);
}
elseif(bookTitle[i] =="Programming")
{
System.out.println("\nBook TitleAuthorPublisherQuantityPrice");
System.out.println("================================================================================");
System.out.println(bookTitle[i]+""+author[i]+""+publisher[i]+""+quantity[i]+" "+price[i]);
}
elseif(bookTitle[i] =="Super Jen")
{
System.out.println("\nBook TitleAuthorPublisherQuantityPrice");
System.out.println("================================================================================");
System.out.println(bookTitle[i]+" "+author[i]+""+publisher[i]+""+quantity[i]+" "+price[i]);
}
}
}
if(done ==false)
{
System.out.println("\nBook not found:\t");
}
callOptions();
}
publicvoid getPurchaseABook()throws IOException
{
System.out.print("\nEnter the name of the Book you would like to buy:\t\t");
String name = br.readLine();
for(int i=0;i<bookTitle.length;i++)
{
if(name.equalsIgnoreCase(bookTitle[i]))
{
done =true;
System.out.print("What is the quantity you require?\t");
String acc = br.readLine();
int readAcc = Integer.parseInt(acc);
int prices = Integer.parseInt(price[i]);
double totalPrice = readAcc*prices;
String total = String.valueOf(totalPrice);
int realQuantity = Integer.parseInt(quantity[i]);
int quantities = realQuantity - readAcc;
String quant = String.valueOf(quantities);
System.out.println("\nYour purchase details are as follows:");
System.out.println("\nBook Title\t\t"+bookTitle[i]);
System.out.println("Author\t\t\t"+author[i]);
System.out.println("Publisher\t\t"+publisher[i]);
System.out.println("Quantity required\t"+readAcc);
System.out.println("Unit price\t\t"+price[i]);
System.out.println("Total Price\t\t"+total);
System.out.print("\nPurchase this book? (Y for yes / N for no)\t" );
String getIt = br.readLine();
if(getIt.equalsIgnoreCase("y") )
{
quantity[i] = quant;
System.out.println("\nThank you for visiting. I hope you come again!");
}
else
System.out.println("\nYou have required to stop the transaction. Merci bouque!");
}
}
if(done ==false)
{
System.out.println("\nBook not found:\t");
}
callOptions();
}
publicstaticvoid main(String args[])throws IOException
{
BookInventory obj =new BookInventory();
}
}
>
[13450 byte] By [
janeCa] at [2007-11-27 3:59:27]

int prices = Integer.parseInt(price[index]);
This is your trouble line in purchaseBook. The reason is the values you have in price[] are strings representing double values. If you make that array an array of doubles that would fix the problem.
I know this is just an assignment, but to give you further insight...
You can also make a Book class that would contain all your fields, i.e. name, author...etc. You would then have a Map (check out HashMap, Map in the java API) that would store all your books. It would make look up easier, and faster.
> Exception in thread "main" java.lang.NumberFormatException: For input string: "16.99"
>at >java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
>at java.lang.Integer.parseInt(Integer.java:456)
>at java.lang.Integer.parseInt(Integer.java:497)
>at BookInventory.getPurchaseABook(BookInventory.java:136)
How to read this: on line 136 in your code, in method getPurchaseABook,
you called Integer.parseInt, passing it the String "16.99". Since that is not
in the format of a whole number, the result was a NumberFormatException. That
wasn't so hard, was it?
thanks kdajani ,Im still very new to java programming, and i have very little know how on ,parsing strings can you help me pls...and the map stull? its like a fiction to me.. I m not yet up to that level yet ..
Look at the API for Integer: http://java.sun.com/javase/6/docs/api/java/lang/Integer.htmlAre there any methods that would parse strings like "16.99"?What would be the type of 16.99? Hint: it's not int, because intsare integers, whole numbers.
Ok I changed the int to double and it works but it does not subtract the quantity purchased from the stock total. how can i do it.. thanks again..
I just looked beyond the initial error you had to look at your code.
Suggestion: why not make quantity an array of int, not an array of String, and
why not make price an array of double, not an array of String?
parsing those hard-coded strings makes no sense. Start with the right types.
Thanks guys for all you help,But is there any way i can reduce the stock quantiy when an item is purchased? since i an using a predefined set of striong,, or should i use a flat db(not pad to store these valuse and load then on run time.. if so how can i achive that.. thanks
> But is there any way i can reduce the stock quantiy when an item is purchased?something like this:quantity[i] -= totalPrice;Loading or saving values is a separate problem. Solve one at a time.
Hi guys,
II'm very thankful for all ur help so far,
I took DrLaszloJamf''s advice and kinda solved part of my problem.
I have now created another class called Books. but when i want it instantiate this class in the main Invenetory class
by using
Books nb = new Books();
it gives me the following error
C:\Program Files\Xinox Software\JCreator Pro\MyProjects\BookInventory\BookInventory.java:196: cannot find symbol
symbol : constructor Books()
location: class Books
Books b1 = new Books();
Attached is my Books Class
public class Books
{
String bookTitle;
int quantity;
String author;
String publisher;
double price;
public Books(String t,int q,String a, String p, double pr)
{
bookTitle = t;
quantity = q;
author =a;
publisher = p;
price = pr;
}
public String toString()
{
return bookTitle;
}
public int getQuantity()
{
return quantity;
}
public String getAuthor()
{
return author;
}
public String getPublisher()
{
return publisher;
}
public double getPrice()
{
return price;
}
}
> it gives me the following error
> C:\Program Files\Xinox Software\JCreator
> Pro\MyProjects\BookInventory\BookInventory.java:196:
> cannot find symbol
> symbol : constructor Books()
> location: class Books
So what does this error message mean? The compiler is telling you quite clearly that it cannot find a constructor for Books that looks like this
Books()
Is there one in your books class? What does your Books constructor look like?
Thanks cotton.m,
I have above my Books Class and My BookInventory Class..
I'll appritiate if u can help me out..
And also is there a way i can load the arrays String bookTitle[] = {"Java Guru","Marketing","Programming","Super Jen"};
int quantity[] = {120,150,200,300};
String author[] = {"DJ","Prof Gordon","Maxine","List O"};
String publisher[] = {"Hello","www Press","Press 1234567","Internet Press"};
double price[] = {16.99,65.49,25.99,50.00};
from a text file?
> Thanks cotton.m,
> I have above my Books Class and My BookInventory
> Class..
> I'll appritiate if u can help me out..
Yes I saw your code and I am trying to help you out.
> And also is there a way i can load the arrays String
> bookTitle[] = {"Java
> Guru","Marketing","Programming","Super Jen"};
> int quantity[] = {120,150,200,300};
> String author[] = {"DJ","Prof Gordon","Maxine","List
> t O"};
> String publisher[] = {"Hello","www Press","Press
> s 1234567","Internet Press"};
> double price[] = {16.99,65.49,25.99,50.00};
>
> from a text file?
Yes but one problem at a time shall we.
Now back to the constructor. What does your Books constructor look like? I just want you to identify it, I know what it looks like because you already posted your code, I am just trying to help you understand the code that you posted.
I really appritiate it
it looks like
public Books(String t,int q,String a, String p, double pr)
{
bookTitle = t;
quantity = q;
author =a;
publisher = p;
price = pr;
}
> I really appritiate it
> it looks like
> > public Books(String t,int q,String a, String p,
> double pr)
>{
>bookTitle = t;
>quantity = q;
>author =a;
>publisher = p;
>price = pr;
>
>
> }
>
>
Okay.
Now see your constructor has a signature that looks like this
public Books(String t,int q,String a, String p, double pr)
You are trying to use a constructor that looks like this though..
public Books()
These are not the same! Which is what the compiler is telling you.
What you need to do to fix this is to make sure that from your inventory class you call the Books construtor that actually exists (the public Books(String t,int q,String a, String p, double pr) one).
If you have defined a parameterized constructor in your class then it is essential to have default constructor defined if your intend to use it.
Yes I tried using
in the book inventory class
Books nb = new Books(String t,int q,String a, String p, double pr);
nb.?
but it still gives me the same error
I really dont know why..
janeCa at 2007-7-21 21:00:07 >

or maybe i don't know how to call it correctly?
janeCa at 2007-7-21 21:00:07 >

> or maybe i don't know how to call it correctly?
Yes I think that would be it.
Look at this example.
public class Book{
private String title;
public Book(String title){
this.title = title;
}
public String toString(){
return this.title;
}
public static void main(String arg[]){
Book myBook = new Book("JLS 1.6");// see this
System.out.println(myBook);
}
}
Thanks again cotton.m ..u've been most helpful
janeCa at 2007-7-21 21:00:07 >

> Thanks again cotton.m ..> u've been most helpfulYou're welcome. So it's working now?
Yes it works now thanks to you, but I'm still struggling to see how i can make the following array dynamic
String bookTitle[] = {"Java","Marketing","Programming","Maths"};
int quantity[] = {120,150,200,300};
String author[] = {"Dr.","don","mie","Grants"};
String publisher[] = {" Co","Press","Press 1234567","Internet Press"};
double price[] = {16.99,65.49,25.99,50.00};
I want to be able to update these arrays at run time.
when ever i initialize int quantity[] = {0,0,0,0};
it does not allow me to add new books
janeCa at 2007-7-21 21:00:07 >

I am using this to add new books
char choice = 'y';
while(choice == 'y')
{
public void addABook() throws IOException
{
System.out.print("\nEnter the name of the Book you would like to buy:\t\t");
String name = br.readLine();
for(int i=0;i<bookTitle.length;i++)
{
if(name.equalsIgnoreCase(bookTitle[i]))
{
done = true;
System.out.print("How many book do you wish to add?\t");
String acc = br.readLine();
int readAcc = Integer.parseInt(acc);
int realQuantity = quantity[i];
if(quantity[i]>< readAcc)
{
System.out.println("\nRequired book quantity is more than stock!");
System.out.println("There is only"+" "+quantity[i]+ " "+"Of"+ " "+bookTitle[i]+" "+"In Stock..");
showMenu();
}
else
System.out.println("\nYou have required to stop the transaction. Merci bouque!");
{
quantity[i] += realQuantity;
int quantities = realQuantity + readAcc;
int quant = Integer.valueOf(quantities);
System.out.println("\nStock to be added:");
System.out.println("\nBook Title\t\t"+bookTitle[i]);
System.out.println("Author\t\t\t"+author[i]);
System.out.println("Publisher\t\t"+publisher[i]);
System.out.println("Quantity to Stock\t"+readAcc);
System.out.println("Unit price\t\t"+price[i]);
System.out.print("\nAdd this book? (Y for yes / N for no)\t" );
String getIt = br.readLine();
if(getIt.equalsIgnoreCase("y") )
{
quantity[i] = quant;
System.out.println("\nBook added!");
}
else
System.out.println("\nYou have required to stop the transaction. Merci bouque!");
}
}
janeCa at 2007-7-21 21:00:07 >

If you want to add books to the inventory, I would suggest using Vectors instead of arrays. They are very similar data structures. Adding new elements to a vector is as simple as saying myVector.add (myObject), and accessing it similar to accessing an Array from a high level point of view.
Check out the Vector class in the Java API.
For storing your values after the program has run, printing your inventory to a file would be a way to do it, but like it has been said, one problem at a time. Something to think about though.