Java Shopping Cart Implementation
Java Shopping Cart Implementation
To properly handle overloaded methods in the "FreshItem" class, the "addFresh" method that accepts an "Item" type should be removed or the UnsupportedOperationException thrown should be handled by implementing logic to convert the "Item" to a "FreshItem" if applicable. Alternatively, the second "addFresh" method that accepts "FreshItem" already handles the array operations and can be used for specific type handling .
I would redesign the "Shoppingcart" class to use an "ArrayList" instead of a fixed array. "ArrayList" provides dynamic sizing, meaning it can automatically expand as items are added, removing the fixed capacity constraint. This not only improves scalability but also allows for intuitive operations like adding and removing items without manual size management .
Using a fixed-size array for storing "Item" and "FreshItem" objects limits scalability since it can only hold a predefined number of elements (5 in this case). This approach can quickly run out of capacity, leading to inability to add more items. From a performance perspective, iterating over all array elements to aggregate data can become inefficient as the array size grows, which could be addressed by using a more scalable collection such as ArrayList .
In the "test" class's main method, "FreshItem" objects are being added using "Item" objects instead of "FreshItem" objects which causes a type mismatch and ignores the necessity of "BestBeforeDate". This could be resolved by directly instantiating "FreshItem" with appropriate parameters including its "BestBeforeDate" or adapting the addFresh method logic to handle "Item" to "FreshItem" conversion appropriately .
The constructor of the "Item" class has a logical error; it does not assign the parameters to the class fields correctly. Instead of assigning the parameters to class variables, it assigns them to the parameters themselves, rendering the constructor ineffective for field initialization. The fields "name", "price", and "quantity" remain uninitialized after instantiation .
The "totalPrice" method sums up the prices of all items stored in the "Shoppingcart" array to calculate the total price. An improvement could be to multiply each item's price by its quantity to reflect the true cost of each item rather than using the unit price, which is currently not accounted for and may lead to incorrect total values .
The "FreshItem" class inherits from the "Item" class, meaning it takes on the properties and methods of the "Item" class. "FreshItem" introduces an additional property called "BestBeforeDate" to store the expiration date associated with fresh items, as well as respective getter and setter methods for this property .
The "Shoppingcart" class determines it is full when the variable "in" is equal to or greater than 5, the size of the Item array. When this condition is met, the message "Shoppingcart is full" is displayed .
The "affichItem" method in the "Shoppingcart" class is used to display information about each item in the shopping cart. It iterates over the "Shoppingcart" array and prints the name, price, and quantity of each item using their respective getter methods .
The methods "Itemcount" and "Itemquantity" provide aggregate information about the shopping cart. "Itemcount" returns the number of items added to the cart, while "Itemquantity" returns the total quantity of all items. These methods offer a quick overview of the cart's contents, contributing to functionalities like checkout processes and inventory management .