Question

In: Computer Science

PREVIOUS CODE: InventroryItems class InventoryItem implements Cloneable{ // instance variables protected String description; protected double price;...

PREVIOUS CODE:

InventroryItems

class InventoryItem implements Cloneable{
        // instance variables
        protected String description;
        protected double price;
        protected int howMany;

        // constructor
        public InventoryItem(String description, double price, int howMany) {
                this.description = description;
                this.price = price;
                this.howMany = howMany;
        }

        // copy constructor
        public InventoryItem(InventoryItem obj) {
                this.description = obj.description;
                this.price = obj.price;
                howMany = 1;
        }

        // clone method
        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }

        // toString method
        @Override
        public String toString() {
                return description + " ($" + price + ")";
        }

        // equals method
        @Override
        public boolean equals(Object obj) {
                InventoryItem other = (InventoryItem) obj;
                if (description.equals(other.description) && price == other.price)
                        return true;
                return false;
        }

        // view method
        public void view() {
                System.out.println("Viewing: " + description);
        }
}

Book

class Book extends InventoryItem {
        private String author;

        public Book(String description, double price, int howMany, String author) {
                super(description, price, howMany);
                this.author = author;
        }

        public Book(Book obj) {
//              super(new InventoryItem(obj.description, obj.price, obj.howMany));
                super(obj);
                this.author = obj.author;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }

        @Override
        public String toString() {
                return "Book: " + description + " by " + author + " ($" + price + ")";
        }

        @Override
        public void view() {
                System.out.println("Openeing Book: " + description);
        }

        @Override
        public boolean equals(Object obj) {
                Book other = (Book) obj;
                if (super.equals(obj) && author.equals(other.author))
                        return true;
                return false;
        }
}

MusicCD

class MusicCD extends InventoryItem{
        private String performer;

        public MusicCD(String description, double price, int howMany, String performer) {
                super(description, price, howMany);
                this.performer = performer;
        }
        
        public MusicCD(MusicCD obj) {
                super(obj);
                this.performer = obj.performer;
        }
        
        @Override
        public String toString() {
                return "CD: "+performer+": "+super.toString();
        }
        
        @Override
        public void view() {
                System.out.println("Now Playing Sample: "+description);
        }
        
        @Override
        protected Object clone() throws CloneNotSupportedException {
                return super.clone();
        }
        
        @Override
        public boolean equals(Object obj) {
                MusicCD other = (MusicCD) obj;
                if(super.equals(obj) && performer.equals(other.performer))
                        return true;
                return false;
        }
}

Test

public class Test{
        public static void main(String[] args) throws CloneNotSupportedException {
                //testing the classes
                InventoryItem book1 = new Book("Somthing Nice", 2.56, 5, "John Wick");
                InventoryItem book2 = (InventoryItem) book1.clone();
                InventoryItem book3 = new Book((Book)book2);
                
                InventoryItem cd1 = new MusicCD("Classic Hits", 3.26, 6, "Elvis Presley");
                InventoryItem cd2 = (InventoryItem) cd1.clone();
                InventoryItem cd3 = new MusicCD((MusicCD)cd2);
                
                System.out.println("book2.equals(book3)? "+book2.equals(book3));
                System.out.println("cd1.equals(cd3)? "+cd1.equals(cd3));
        }
}

JAVA programming- please respond to all prompts as apart of one java project. All information has been provided.

Part C

Create two more classes, TextBook and Novel, which inherit from Book. A TextBook has a String subject, and a Novel has a String genre.

Override equals in both these classes. A TextBook can be equal to another TextBook with the same price, description, author, and subject, or to a Novel if the price, description, and author are the same and the genre of the Novel is the same as the subject of the textbook. The same goes for the Novel class.  

Part D

Create a class ItemStorage which contains an array of inventory items as an instance variable. It should start out empty but with room for at least 10 items. Keep an instance variable like firstEmptyInstance which holds the number of the first empty location in the array (these should NOT be accessible directly from outside classes, but you may find it easier to use protected for the sake of your subclasses).

Add a method add(newitem) to add InventoryItems to the list. If the item it is adding is equal (think about this) to an item already in the list, instead of taking up another spot in the array, increase the howmany number for the item already there (so if the one in the array has 3 and the one I am adding has 2, we end up with 5), otherwise add it to the array in the first empty spot as usual. If we have added successfully, return true. If the item is new to the list and the array is full, return false.

The toString method should return a String that lists all the items that are in the list, numbered from 1, including how many of each there are.

Add a method viewAll that calls view for each of the items in the list.

Create a class Cart which inherits from ItemStorage

Add a method totalPrice which should return the current total price of all elements in the cart, taking in to account how many of each there are (so if there is an item with price $3 and there are 4 of them, they add $12 to the total price.

The toString should look like the one for ItemStorage, but add a total price at the bottom.

Create a Class Warehouse which inherits from ItemStorage.

Add a method buy(int index) (Assume a human started counting at 1, and adjust accordingly.)

  • If index is not a valid, occupied index in the list, return null.
  • If it is valid and there is more than one of that item in stock, decrease the number in stock, and return a copy of the item.
  • If there is only one left, before returning it, remove it entirely from the list by first replacing it by the last item in the list, and then moving the first empty index variable up by one, and then return the item. (so, if the cart formerly contained items (A, B, C, D, E) and we removed the last C, it would now contain (A, B, E, D), and the first empty index would become 4 instead of 5, while we return C.)  

[Do not remove items by looping through and moving everything else up, so that ABCDE with C removed becomes ABDE, notice how much more inefficient this is. It is only worth doing in an array if we care about the order.]

Part E

In a main, create a Warehouse and fill it with various Books, MusicCD's, and other items for sale. Make sure there are multiples of some items in the warehouse. (Make items up in your code, don't read them in from a user). Also create a Cart.

In a loop, repeatedly show the Warehouse and ask the user whether to 1) buy a new item, 2) view cart, 3) preview items 4) check out. If they choose to buy a new item, ask for the number of the item, buy that item from the warehouse, and add the chosen item to the cart. If they choose to view cart, print the cart. If they choose to preview items, use viewAll() from the Cart class to view all items in the cart. If they choose to check out, show the total cost again and make them confirm that they want to buy and if they do, print a message saying their credit card was charged, if not just say goodbye, then end the program.

(hint: The main class shouldn't even need to be aware that there are arrays in the Cart and Warehouse classes. Classes other than main shouldn't talk to the user for this program. As always, you may add extra methods to any class to help you break down tasks or to make coding easier. )

Solutions

Expert Solution

C

//TextBook.java

public class TextBook extends Book{
        private String subject;
        @Override
    public boolean equals(TextBook textBook2) {
            return this.price==textBook2.price&&this.description==textBook2.description&&this.author==textBook2.author&&this.subject==textBook2.subject;
    }
        @Override
    public boolean equals(Novel novel2) {
            return this.price==novel2.price&&this.description==novel2.description&&this.author==novel2.author&&this.subject==novel2.subject;
    }
}

//Novel.java

public class Novel extends Book{
        private String genre;
        @Override
    public boolean equals(TextBook textBook2) {
            return this.price==textBook2.price&&this.description==textBook2.description&&this.author==textBook2.author&&this.subject==textBook2.subject;
    }
        @Override
    public boolean equals(Novel novel2) {
            return this.price==novel2.price&&this.description==novel2.description&&this.author==novel2.author&&this.genre==novel2.genre;
    }
}

Related Solutions

Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance);...
Base Class class TransportationLink { protected: string _name; double _distance; public: TransportationLink(const string &name, double distance); const string & getName() const; double getDistance() const; void setDistance(double); // Passes in the departure time (as minute) and returns arrival time (as minute) // For example: // 8 am will be passed in as 480 minutes (8 * 60) // 2:30 pm will be passed in as 870 minutes (14.5 * 60) virtual unsigned computeArrivalTime(unsigned minute) const = 0; }; #endif Derived Classes...
The Hole Class Description: For the Hole class, there are three instance variables: the par for...
The Hole Class Description: For the Hole class, there are three instance variables: the par for the hole, the length of the hole in yards, and a Boolean valued indicator of whether the hole is one in which the players’ full drives will be measured for distance. John clarifies: The length of the hole in yards determines par for the hole. The current standard is: No more than 250 yards Par 3 251 to 470 yards Par 4 471 to...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Create a Java class named Trivia that contains three instance variables, question of type String that...
Create a Java class named Trivia that contains three instance variables, question of type String that stores the question of the trivia, answer of type String that stores the answer to the question, and points of type integer that stores the points’ value between 1 and 3 based on the difficulty of the question. Also create the following methods: getQuestion( ) – it will return the question. getAnswer( ) – it will return the answer. getPoints( ) – it will...
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second;...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second; public: Temporary(string = "", double = 0.0, double = 0.0); void set(string, double, double); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); }; Write the definition of the member function set() so that the instance variables are set according to the parameters. Write the definition of the constructor so that it initializes the instance variables using the function set() Write...
with PHP Create a class called Employee that includes three instance variables—a first name (type String),...
with PHP Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary int). Provide a constructor that initializes the three instance data member. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its 0. Write a test app named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary....
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT