Introduction To Computer Science - Chapter 4 Lab: Writing Multiple Classes - 25 Points

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

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:

● A project called “Book Inventory Lab”


● 2 classes, one called BookInventory and one called BookInventoryDriver. The latter will
contain the main method.
● The BookInventory class will have the following:
o Instance variables for the book title, price, number of pages, number of copies in
stock, the number of user ratings, and the average user rating on a scale from 1
to 5 stars (partial stars accepted). You should choose the appropriate data types
for each.
o Two versions of the constructor. The first version will accept all of the values
above and set the instance variables. The second version of the constructor will
allow the user to specify just the book title, price and number of pages. It
should initialize the other instance variables to default values of your choosing
(make the values logical).
o A method called addInventory that will take the number of books being added
as input and update the “number of copies in stock” instance variable.
o A method called sellInventory that will take the number of books being sold as
input and update the “number of copies in stock” instance variable.
o A method called updateRating that will take a customer rating as input and
calculate the new average rating before updating the “average rating” instance
variable. Be careful with your math!
o “Getter” (aka accessor) methods that will return the values of the instance
variables. This is analogous to the getBalance and getAccountNumber methods
from our bank account example. For this exercise, you only need to make these
accessor methods for the book title, price, number of copies in stock, and
average rating instance variables.
o “Setter” (aka mutator) methods that will update the title, number of pages and
price instance variables. For the setter method that updates the price, if the
change in price is more than 25% in either direction, it will print a warning to the
user but still implement the change without requiring confirmation.
o A method called “toString” that returns a string containing the values of all
instance variables (separated by tabs). Use the bank account example as a
template.

● 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 class BookInventory {

private NumberFormat fmt = NumberFormat.getCurrencyInstance();

private String bookTitle;


private double bookPrice;
private int numPages;
private int numCopies;
private int userRatings;
private double starRating;

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;
}

public BookInventory (String title, double price, int pages) {


bookTitle = title;
bookPrice = price;
numPages = pages;
numCopies = 0;
userRatings = 0;
starRating = 0;
}

public void addInventory (int addedCopies) {


numCopies += addedCopies;
}

public void sellInventory (int soldCopies) {


numCopies -= soldCopies;
}

public void updateRating (double newInput) {


double newRating = (starRating * userRatings + newInput) / (userRatings + 1);
starRating = newRating;
}

public String getTitle() {


return bookTitle;
}

public double getPrice() {


return bookPrice;
}

public int getPages() {


return numPages;
}

public int getStock() {


return numCopies;
}

public double getAverageRating() {


return starRating;
}

public void setTitle (String newTitle) {


bookTitle = newTitle;
}

public void setPages (int newPages) {


numPages = newPages;
}

public void setPrice (double newPrice) {


if (newPrice > bookPrice*1.25) {
System.out.println("Warning: The new price is 25% higher than the old price.");
}
bookPrice = newPrice;

public String toString ()


{
return (bookTitle + "\t" + numPages + "\t" + fmt.format(bookPrice) + "\t" + numPages + "\t"
+ numCopies + "\t" + userRatings + "\t" + starRating);
}

BookInventoryDriver.java
import java.text.DecimalFormat;

public class BookInventoryDriver {


public static void main(String[] args) {

DecimalFormat df = new DecimalFormat("#.#");


BookInventory book1 = new BookInventory ("The Great Gatsby", 8.99, 218, 2400, 67, 4.3);
BookInventory book2 = new BookInventory ("Animal Farm", 7.99, 112, 3767, 48, 3.9);
BookInventory book3 = new BookInventory ("The Catcher in the Rye", 9.99, 277);

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

You might also like