Question

JAVA Objective : • Learning how to write a simple class. • Learning how to use...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

\color{blue}\underline{Book.java:}

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

\color{blue}\underline{BookStore.java:}

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

}

Make\;sure\;to\;test\;these\;classes

Add a comment
Know the answer?
Add Answer to:
JAVA Objective : • Learning how to write a simple class. • Learning how to use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    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 impl...

    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...

    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 "Ret...

    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....

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    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,...

  • Language : JAVA Part A Create a class CompanyDoc, representing an official document used by a...

    Language : JAVA Part A Create a class CompanyDoc, representing an official document used by a company. Give it a String title and an int length. Throughout the class, use the this. notation rather than bare instance variables and method calls.   Add a default constructor. Add a parameterized constructor that takes a value for title. Use this( appropriately to call one constructor from another. Add a toString(), which returns a string with the title, and the length in parentheses. So...

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT