JAVA
Objective :
• Learning how to write a simple class.
• Learning how to use a prewritten class.
• Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application.
Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java
• I’ve provided a screenshot of how your project should look like. • Be sure to document your code (add comments on top of your java file).
• In the comments add your name, date, course, homework number, and statement of problem. • Make sure your output matches mine (I attached a demo file for your convenience).
• Once you are done, Zip Lab13 project and upload through Blackboard.
Description: In this assignment, you will essentially write a console application that runs a simple bookstore. Three classes will be used in this homework: Book , Bookstore and TempleBookstore .
1. I’ve provided half - way written class called Bookstore that you need to co mplete ( it is posted in Blackboard next to the assignment description). You can add it to your project. You will just use the methods in Bookstore class only . The methods of Bookstore class are given below.
2. You have to implement a class called Book as described below. The name of this class MUST be Book and it MUST contain the instance methods below. Do not change the names of the methods, because they are accessed from the Bookstore class.
3. You also have to implement another class called TempleBooks tore . This class will contain only a main method. In this main method, you will create a Bookstore object first, and then you will perform certain menu - driven operations using this Bookstore object and the methods in the Bookstore class.
Book
The Book class needs to written by you. A Book object keeps track of information for one particular book that could potentially be stored in a bookstore. Here are the instance variables/fields for this class:
private String title;
private int numOfPages;
private double price;
private int quantity;
// Constructor: Takes in the title of the book, the number of pages in the book, the cost of the book and
// the number of copies (quantity) of books and initializes each of the appropriate instances variables in
//the object.
public Book(String thetitle, int pages, double cost, int num)
// Returns the title of the Book object the method is called on.
public
String getTitle()
// Returns the price of the Book object the method is called on.
public double getPrice()
// Returns the quantity of the Book object the method is called on.
public int getQuantity()
// Returns all the information about a Book object
as a String. (Add spaces or tabs to make it readable!)
public String toString()
// Decrements the number of copies, by the given amount, for the Book object the method is called on.
public void subtractQuantity(int amount)
//Increments the number of copi
es, with the given amount, for the Book object the method is called on.
public void addQuantity(int amount)
Bookstore
You need to use the methods in Bookstore class in order to create your console application that will "run" a bookstore. Here are the instance methods in that class:
// Constructor that creates a new, empty Bookstore object.
public Bookstore()
// Adds a new Book b to the stock of the Bookstore object.
public void addNewBook(Book b)
// 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.
public void addBookQuantity(String title, int quantity)
// Returns true if quantity or more copies of a book with the title title are co
ntained in the Bookstore object.
public boolean inStock(String title, int quantity)
//
Executes
selling quantity number of books from the Bookstore object with the title title to the
// buyer. (Note: there is no I/O done in this method, the Bookstore obje
ct is changed to reflect
// the sale. The method returns true is the sale was executed successfully, false otherwise.
public boolean sellBook(String title, int quantity);
// Lists all of the titles of the books in the Bookstore object.
public void
listTitles()
// Lists all of the information about the books in the Bookstore object.
public void listBooks()
// Returns the total gross income of the Bookstore object.
public double getIncome()
The TempleBookstore class should contain only a main method. In this main method, you will create a Bookstore object first, and then you will perform certain menu
-
driven operations using this
Bookstore
object and the methods in this class. The main method of your
TempleBookstore
class should do the
following:
•
Create a new Bookstore
object.
•Prompt the user with the following menu choice
1) Add a book to the stock
2) Sell a book in stock
3) List the titles of all the books in stock (in the Bookstore object)
4)
List all the information about the books in stock (in the Bookstore object)
5) Print out the gross income of the bookstore
6) Quit
Execute the choice the user chooses and then continue until they quit.
•What each menu choice should do:
1.Adding a book
:
Ask the user for the title of the book. If it is already in stock, simply ask the user to enter how many extra books to stock, and then do so. If not, ask the user for the rest of the information (number of pages, price, and quantity) and add that book into the stock.
2.Selling a book: Ask the user for the title of the book they want to buy. If it is not in stock, print a message to that effect. Otherwise,ask the user how many copies of that book they want to buy. If there are enough copies of the book, make the sale. If not, print out a message explaining why the transaction could not be completed.
3.List the titles of all the books in stock(in the Bookstore object)
4.List all the information about the books in stock(in the Bookstore object)
5.Print out the gross income of the bookstore
6.Quit

public class Book {
private String title;
private int numOfPages;
private double price;
private int quantity;
public Book(String title, int numOfPages, double price, int quantity) {
this.title = title;
this.numOfPages = numOfPages;
this.price = price;
this.quantity = quantity;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void subtractQuantity(int amount){
if(amount > 0 && quantity >= amount)
quantity -= amount;
}
public void addQuantity(int amount){
if(amount > 0)
quantity += amount;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", numOfPages=" + numOfPages +
", price=" + price +
", quantity=" + quantity +
'}';
}
}

import java.util.ArrayList;
import java.util.List;
public class BookStore {
private List<Book> books;
private double income;
public BookStore() {
books = new ArrayList<>();
}
public void addNewBook(Book b){
books.add(b);
}
private Book findBook(String title){
for(int i = 0; i < books.size(); ++i){
if(books.get(i).getTitle().equalsIgnoreCase(title))
return books.get(i);
}
return null;
}
public void addBookQuantity(String title, int quantity){
Book book = findBook(title);
if(book != null){
book.addQuantity(quantity);
}
}
public boolean inStock(String title, int quantity){
Book book = findBook(title);
if(book != null){
return book.getQuantity() > 0;
}
return false;
}
public boolean sellBook(String title, int quantity){
Book book = findBook(title);
if(book != null){
if(book.getQuantity() >= quantity){
book.subtractQuantity(quantity);
income += book.getPrice() * quantity;
return true;
}
}
return false;
}
public void listTitles(){
for(int i = 0; i < books.size(); ++i){
System.out.println(books.get(i).getTitle());
}
}
public void listBooks(){
for(int i = 0; i < books.size(); ++i){
System.out.println(books.get(i).toString());
}
}
public double getIncome(){
return income;
}
}

JAVA Objective : • Learning how to write a simple class. • Learning how to use...
I need code in java
The Student class:
CODE IN JAVA:
Student.java file:
public class Student {
private String name;
private double gpa;
private int idNumber;
public Student() {
this.name = "";
this.gpa = 0;
this.idNumber = 0;
}
public Student(String name, double gpa, int
idNumber) {
this.name = name;
this.gpa = gpa;
this.idNumber = idNumber;
}
public Student(Student s)...
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...
Programming language: Java
Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...
in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....
Write a class called Book. Here are the relevant attributes: title author yearPublished bookPriceInCAD Provide a constructor that takes parameters to initialize all the instance variables. The constructor calls the mutator (set) methods to initialize the instance variables. Provide an accessor (get) and mutator (set) for each instance variable. The mutators all validate their parameters appropriately and use them only if valid. If the passed parameter was invalid an IllegalArgumentException will be thrown with a proper error message A...
re-implement the Bookshelf class using a linkedlist and a Node class. Implement the above add, remove, and search methods again. here is the code for that: public class Book { private String title; private double price; public Book() { title=""; price=0; } public Book(String title, double price) { this.title = title; this.price = price; } public void setTitle(String title) { this.title = title; } public void setPrice(double price) { this.price = price; } public...
JAVA This PoD, builds off the Book class that you created on Monday (you may copy your previously used code). For today’s problem, you will add a new method to the class called lastName() that will print the last name of the author (you can assume there are only two names in the author name – first and last). You can use the String spilt (“\s”) method that will split the String by the space and will return an array...
Question set 1 (Java) object oriented: 1.Write a class called Student. The class should be able to store information regarding the name, age, gpa, and phone number of a Student object. Please write all the setter and getter methods. Finally, write a Demo class to demonstrate the use of the class by creating two different objects. 2.Use the same Student class from the previous problem. This time,you will write a different Demo class, in which you will create three Student...
cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...