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

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,...
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...
Create a class called Sphere. The class will contain the following    Instance data double radius...
Create a class called Sphere. The class will contain the following    Instance data double radius Methods Constructor with one parameter which will be used to set the radius instance data Getter and Setter for radius             Area - calculate the area of the sphere (4 * PI * radius * radius)             Volume - calculate and return the volume of the sphere (4/3 * PIE * radius * radius * radius) toString - returns a string with the...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType>...
Author code /** * LinkedList class implements a doubly-linked list. */ public class MyLinkedList<AnyType> implements Iterable<AnyType> { /** * Construct an empty LinkedList. */ public MyLinkedList( ) { doClear( ); } private void clear( ) { doClear( ); } /** * Change the size of this collection to zero. */ public void doClear( ) { beginMarker = new Node<>( null, null, null ); endMarker = new Node<>( null, beginMarker, null ); beginMarker.next = endMarker; theSize = 0; modCount++; } /**...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
Previous Lab Code files: PersonType.h: #include <string> using namespace std; class personType { public: void print()...
Previous Lab Code files: PersonType.h: #include <string> using namespace std; class personType { public: void print() const; void setName(string first, string middle, string last);    void setLastName(string last);    void setFirstName(string first);    void setMiddleName(string middle);    bool isLastName(string last) const;    bool isFirstName(string first) const; string getFirstName() const;    string getMiddleName() const;    string getLastName() const; personType(string first = "", string middle = "", string last = ""); private: string firstName; string middleName; string lastName; }; PersonTypeImp: #include <iostream>...
1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory.
  1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory. 2. The Item class will require one constructor that takes a name and price to initialize the fields. Since the name and price will be provided through TextFields, the parameters can be two Strings (be sure to convert the price to a double before storing this into the field). 3. Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT