Introduction To Computer Science - Chapter 4 Lab: Writing Multiple Classes - 25 Points
Introduction To Computer Science - Chapter 4 Lab: Writing Multiple Classes - 25 Points
Introduction To Computer Science - Chapter 4 Lab: Writing Multiple Classes - 25 Points
In today’s lab, you will be writing code to simulate the operation of a bookstore’s inventory
system. I would highly recommend that you consult the bank account code that we reviewed
while working on this assignment.
Requirements:
● The BookInventoryDriver class will contain the main method. You will then do the
following:
o Make three objects from the BookInventory class to represent each of three
different books stocked by this store. Two of the three will use the first version
of the constructor above, and the last book will use the second version of the
constructor.
o Test out each of the methods above to ensure that they work. For example, you
might want to call the addInventory method on one of the objects and then call
the getNumCopiesInStock to see if the number of copies was updated
appropriately. Alternatively, if you use syso with the object name in
parentheses, as in:
System.out.println(book1);
it will automatically call the “toString” method on that object. Since we told our
“toString” method to print out all instance variables, you can use this approach
to quickly verify that things are working correctly.
When complete, you should copy and paste the code from both classes into a single email,
along with the outputs, and submit via Google Classroom.
BookInventory.Java
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public BookInventory (String title, double price, int pages, int copies, int users, double rating) {
bookTitle = title;
bookPrice = price;
numPages = pages;
numCopies = copies;
userRatings = users;
starRating = rating;
}
BookInventoryDriver.java
import java.text.DecimalFormat;
book1.addInventory(1000);
System.out.println("Number of copies of The Great Gatsby after adding inventory: " + book1.getStock());
book2.sellInventory(2000);
System.out.println("Number of copies of Animal Farm after sales: " + book2.getStock());
book2.updateRating(4.1);
System.out.println("New rating of Animal Farm: " + df.format(book2.getAverageRating()));
book3.setTitle("Holden Caulfield");
System.out.println("New title of Book 3: " + book3.getTitle());
book3.setPrice(25.99);
System.out.println("New price of Book 3: " + book3.getPrice());
book3.setPages(289);
System.out.println("New amount of pages in Book 3: " + book3.getPages());
System.out.println(book1);
System.out.println(book2);
System.out.println(book3);
}
}
Output:
Number of copies of The Great Gatsby after adding inventory: 3400
Number of copies of Animal Farm after sales: 1767
New rating of Animal Farm: 3.9
New title of Book 3: Holden Caulfield
Warning: The new price is 25% higher than the old price.
New price of Book 3: 25.99
New amount of pages in Book 3: 289
The Great Gatsby 218 $8.99 218 3400 67 4.3
Animal Farm 112 $7.99 112 1767 48 3.9
Holden Caulfield 289 $25.99 289 0 0 0.0