Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please use java thanks! [10 points] Write an abstract class Market Product which has the following private field: A String name The class must also
please use java thanks!
[10 points] Write an abstract class Market Product which has the following private field: A String name The class must also have the following public methods . A constructor that takes a String as input indicating the name of product and uses it to initialize the corresponding attribute. A final getName() method to retrieve the name of this MarketProduct. An abstract method getCost() which takes no input and returns an int. This method should be abstract (thus, not implemented) because how to determine the cost depends on the type of product. . An abstract method equals() which takes an Object as an input and retums a boolean. This method should be abstract as well, since depending on the type of product different conditions should be met in order for two products to be considered equal. [25 points] All of the followings must be subclasses of the Market Product: Write a class Egg derived from the Market Product class. The Egg class has the following private fields - An int indicating the number of eggs. - An int indicating the price per dozen of these eggs. Note that the all the prices (throughout the assignment) are indicated in cents. For instance, 450 represents the amount $4.50. The Egg class has also the following public methods: - A constructor that takes as input a String with the name of the product, an int indicating the number required, and an int indicating the price of such product by the dozen. The constructor uses the inputs to create a Market Product and initialize the corresponding fields. - A getCost () method that takes no input and returns the cost of the product in cents. The cost is computed base on the number required and the cost per dozen. For instance, 4 large brown eggs at 380 cents/dozen cost 126 cents (the cost should be rounded down to the nearest cent). You may assume that cost of all MarketProducts fits within an int and therefore doesn't cause overflow. - An equals() method which takes as input an Object and return true if input matches this in type, name, cost and number of eggs. Otherwise the method returns false. [40 points] Write a class Basket representing a list of market products. Note that the instructions on how to implement this class are not always very specific. This is intentional, since your assignment will not be tested on the missing details of the implementation. Note though, that your choices will make a difference in terms of how efficient your code will be. We will not be deducting point for inefficient code in Assignment 1. Note once again that, you are NOT allowed to import any other class (including ArrayList or LinkedList). The class has (at least) the following private field: An array of Market Products. The class must also have the following public methods A constructor that takes no inputs and initialize the field with an empty array. A getProducts() method which takes no inputs and returns a shallow copy of the array (NOT a copy of the reference!) of Market Products of this basket (with the elements in the same order). An add() method which takes as input a Market Product and does not return any value. The method adds the MarketProduct at the end of the list of products of this basket. A remove () method which takes as input a Market Product and returns a boolean. The method removes the first occurrence of the specified element from the array of products of this basket. If no such product exists, then the method returns false, otherwise, after removing it, the method returns true. Note that this method removes a product from the list if and only if such product is equal to the input received. For example, it is not possible to remove 0.25 Kg of McIntosh apples for a 0.5 Kg McIntosh apples MarketProduct. After the product has been remove from the array, the subsequent elements should be shifted down by one position, leaving no unutilized slots in the middle array. A clear() method which takes no inputs, retums no values, and empties the array of products of this basket. A get NumOf Products() method that takes no inputs and retums the number of products present in this basket. A getSubTotal() method that takes no inputs and returns the cost (in cents) of all the products in this basket. A getTotalTax() method that takes no inputs and returns the tax amount in cents) to be paid based on the product in this basket. Since we are in Quebec, you can use a tax rate of 15%. The tax amount should be rounded down to the nearest cent. Note that Egg and Fruit are tax free, so taxes should be paid only for Jam. A getTotalCost() method that takes no inputs and returns the total cost (in cents and after tax) of the products in this basket. AtoString() method that returns a String representing a receipt for this basket. The receipt should contain a product per line. On each line the name of the product should appear, followed by its cost separated by a tab character. After all the products have been listed, the following information should appear on each line: - An empty line - The subtotal cost - The total tax - An empty line - The total cost Note that all the integer number of cents should be transformed into a String formatted in dollars and cents (you can write a helper method to do so if you'd like). If the number of cents is represented by an int that is less than or equal to 0, then it should be transformed into a String containing only the hyphen character ("-"). An example of a receipt is as follows: Quail eggs 4.00 McIntosh apples 6.12 Asian Pears 4.24 Blueberry Jam 4.75 Blueberry Jam 4.75 Subtotal Total Tax 23.86 1.42 Total Cost 25.28 points] Write a class Customer which has the following private fields: A String name An int representing the balance in cents) of the customer A Basket containing the products the customer would like to buy. The class must also have the following public methods: A constructor that takes as input a string indicating the name of the customer, and an int representing their initial balance. The constructor uses its inputs and creates an empty Basket to initialize the corresponding fields. A get Name () and a getBalance() method which return the name and balance (in cents) of the customer respectively. A get Basket() method which returns the reference to the Basket of the customer (no copy of the Basket is needed). . An addFunds () method which takes an int as input representing the amount of cents to be added to the balance of the customer. If the input received is negative, the method should throw an Illegal ArgumentException with an appropriate message. Otherwise, the method will simply update the balance and return the new balance in cents. An addToBasket() method which takes a Market Product as input and adds it to the basket of the customer. This method should not return anything. A removeFromBasket() method which takes a MarketProduct as input and removes it from the basket of the customer. The method returns a boolean indicating whether of not the operation was successful. A checkOut() method which takes no input and returns the receipt for the customer as a String. If the customer's balance is not enough to cover the total cost of their basket, then the method throws an IllegalStateException. Otherwise, the customer is charged the total cost of the basket, the basket is cleared, and a receipt is returned. [10 points] Write an abstract class Market Product which has the following private field: A String name The class must also have the following public methods . A constructor that takes a String as input indicating the name of product and uses it to initialize the corresponding attribute. A final getName() method to retrieve the name of this MarketProduct. An abstract method getCost() which takes no input and returns an int. This method should be abstract (thus, not implemented) because how to determine the cost depends on the type of product. . An abstract method equals() which takes an Object as an input and retums a boolean. This method should be abstract as well, since depending on the type of product different conditions should be met in order for two products to be considered equal. [25 points] All of the followings must be subclasses of the Market Product: Write a class Egg derived from the Market Product class. The Egg class has the following private fields - An int indicating the number of eggs. - An int indicating the price per dozen of these eggs. Note that the all the prices (throughout the assignment) are indicated in cents. For instance, 450 represents the amount $4.50. The Egg class has also the following public methods: - A constructor that takes as input a String with the name of the product, an int indicating the number required, and an int indicating the price of such product by the dozen. The constructor uses the inputs to create a Market Product and initialize the corresponding fields. - A getCost () method that takes no input and returns the cost of the product in cents. The cost is computed base on the number required and the cost per dozen. For instance, 4 large brown eggs at 380 cents/dozen cost 126 cents (the cost should be rounded down to the nearest cent). You may assume that cost of all MarketProducts fits within an int and therefore doesn't cause overflow. - An equals() method which takes as input an Object and return true if input matches this in type, name, cost and number of eggs. Otherwise the method returns false. [40 points] Write a class Basket representing a list of market products. Note that the instructions on how to implement this class are not always very specific. This is intentional, since your assignment will not be tested on the missing details of the implementation. Note though, that your choices will make a difference in terms of how efficient your code will be. We will not be deducting point for inefficient code in Assignment 1. Note once again that, you are NOT allowed to import any other class (including ArrayList or LinkedList). The class has (at least) the following private field: An array of Market Products. The class must also have the following public methods A constructor that takes no inputs and initialize the field with an empty array. A getProducts() method which takes no inputs and returns a shallow copy of the array (NOT a copy of the reference!) of Market Products of this basket (with the elements in the same order). An add() method which takes as input a Market Product and does not return any value. The method adds the MarketProduct at the end of the list of products of this basket. A remove () method which takes as input a Market Product and returns a boolean. The method removes the first occurrence of the specified element from the array of products of this basket. If no such product exists, then the method returns false, otherwise, after removing it, the method returns true. Note that this method removes a product from the list if and only if such product is equal to the input received. For example, it is not possible to remove 0.25 Kg of McIntosh apples for a 0.5 Kg McIntosh apples MarketProduct. After the product has been remove from the array, the subsequent elements should be shifted down by one position, leaving no unutilized slots in the middle array. A clear() method which takes no inputs, retums no values, and empties the array of products of this basket. A get NumOf Products() method that takes no inputs and retums the number of products present in this basket. A getSubTotal() method that takes no inputs and returns the cost (in cents) of all the products in this basket. A getTotalTax() method that takes no inputs and returns the tax amount in cents) to be paid based on the product in this basket. Since we are in Quebec, you can use a tax rate of 15%. The tax amount should be rounded down to the nearest cent. Note that Egg and Fruit are tax free, so taxes should be paid only for Jam. A getTotalCost() method that takes no inputs and returns the total cost (in cents and after tax) of the products in this basket. AtoString() method that returns a String representing a receipt for this basket. The receipt should contain a product per line. On each line the name of the product should appear, followed by its cost separated by a tab character. After all the products have been listed, the following information should appear on each line: - An empty line - The subtotal cost - The total tax - An empty line - The total cost Note that all the integer number of cents should be transformed into a String formatted in dollars and cents (you can write a helper method to do so if you'd like). If the number of cents is represented by an int that is less than or equal to 0, then it should be transformed into a String containing only the hyphen character ("-"). An example of a receipt is as follows: Quail eggs 4.00 McIntosh apples 6.12 Asian Pears 4.24 Blueberry Jam 4.75 Blueberry Jam 4.75 Subtotal Total Tax 23.86 1.42 Total Cost 25.28 points] Write a class Customer which has the following private fields: A String name An int representing the balance in cents) of the customer A Basket containing the products the customer would like to buy. The class must also have the following public methods: A constructor that takes as input a string indicating the name of the customer, and an int representing their initial balance. The constructor uses its inputs and creates an empty Basket to initialize the corresponding fields. A get Name () and a getBalance() method which return the name and balance (in cents) of the customer respectively. A get Basket() method which returns the reference to the Basket of the customer (no copy of the Basket is needed). . An addFunds () method which takes an int as input representing the amount of cents to be added to the balance of the customer. If the input received is negative, the method should throw an Illegal ArgumentException with an appropriate message. Otherwise, the method will simply update the balance and return the new balance in cents. An addToBasket() method which takes a Market Product as input and adds it to the basket of the customer. This method should not return anything. A removeFromBasket() method which takes a MarketProduct as input and removes it from the basket of the customer. The method returns a boolean indicating whether of not the operation was successful. A checkOut() method which takes no input and returns the receipt for the customer as a String. If the customer's balance is not enough to cover the total cost of their basket, then the method throws an IllegalStateException. Otherwise, the customer is charged the total cost of the basket, the basket is cleared, and a receipt is returnedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started