0% found this document useful (0 votes)
15 views3 pages

Java Shopping Cart Implementation

The document defines classes for items, shopping carts, and fresh items. The Item class stores name, price, and quantity of an item. The Shoppingcart class allows adding items to the cart and retrieving total price and quantity. The FreshItem class extends Item to also store an expiration date.

Uploaded by

Aymen Douibi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Java Shopping Cart Implementation

The document defines classes for items, shopping carts, and fresh items. The Item class stores name, price, and quantity of an item. The Shoppingcart class allows adding items to the cart and retrieving total price and quantity. The FreshItem class extends Item to also store an expiration date.

Uploaded by

Aymen Douibi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

public class Item

{
private String name ;
private long price ;
private int quantity ;

public Item (String N,long P,int Q)


{
N= name ;
P= price ;
Q= quantity ;
}

Item() {
throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}
public String getname()
{
return name;
}
public long getprice()
{
return price;
}
public int getquantity ()
{
return quantity;
}

public class Shoppingcart


{
Item [] Shoppingcart = new Item [5] ;

int in =0 ;
public Shoppingcart() {}

public void addItem (Item it) {


if (in>=5)
{ [Link]("Shoppingcart is full "); }
else {
Shoppingcart [in]=it;
in++ ;}}
public int Itemcount()
{return in ;}
public void affichItem (){
for (int i=0;i<=4;i++)
{ [Link]((i+1)+ " name "+ Shoppingcart[i].getname()+" price "
+ Shoppingcart[i].getprice()+ " quantity "+ Shoppingcart[i].getquantity());}
}
public int Itemquantity ()
{ int quntityTotal=0;
for(int i=0;i<=4;i++)
{quntityTotal=quntityTotal+Shoppingcart[i].getquantity();}
return quntityTotal ;

public long totalPrice()


{ long TotalPrice=0;
for(int i=0;i<=4;i++)
{TotalPrice=TotalPrice+Shoppingcart[i].getprice();}
return TotalPrice;
}

}
public class FreshItem extends Item {
FreshItem [] cartitem = new FreshItem [5] ;
int in =0 ;
private String BestBeforeDate;

public FreshItem(String Name, long Price, int Quantity,String BestBeforeDate)


{
super(Name, Price, Quantity);
[Link]=BestBeforeDate;
}
FreshItem() {}
public String getBestBeforeDate() {
return BestBeforeDate;
}
public void setBestBeforeDate(String bestBeforeDate) {
BestBeforeDate = bestBeforeDate;}

void addFresh(Item fresh1) {


throw new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}
public void addFresh (FreshItem fit) {
if (in>=5)
{ [Link](" FreshItem is full "); }
else {
cartitem [in]= fit ;
in++ ;}}
public int FreshItemcount()
{return in ;}
public void affichFreshItem (){
for (int i=0;i<=4;i++)
{ [Link]((i+1)+ " name "+ cartitem[i].getname()+" price "
+ cartitem[i].getprice()+ " quantity "+ cartitem[i].getquantity());}
}
public int FreshItemquantity ()
{ int quntityTotal=0;
for(int i=0;i<=4;i++)
{quntityTotal=quntityTotal+ cartitem[i].getquantity();}
return quntityTotal ;}
}

public class test


{
public static void main(String[] args) {
Item item1 = new Item("water",200,1);
Item item2 = new Item("bread",500,7);
Item item3 = new Item("Milk",800,4);
Item item4 = new Item("coffee",400,3);
Item item5 = new Item("Salmon",700,5);

Shoppingcart cart1= new Shoppingcart();

[Link](item1);
[Link](item2);
[Link](item3);
[Link](item4);
[Link](item5);

[Link]();

Item fresh1 = new Item("water",200,1);


Item fresh2 = new Item("bread",500,7);
Item fresh3 = new Item("Milk",800,4);
Item fresh4 = new Item("coffee",400,3);
Item fresh5 = new Item("Salmon",700,5);

FreshItem cart2=new FreshItem();

[Link] (fresh1);
[Link] (fresh2);
[Link] (fresh3);
[Link] (fresh4);
[Link] (fresh5);

[Link]("le nombre total des articles est: "+


[Link]());
[Link]("la Quantite total des articles est: "+
[Link] ());
[Link]("le prix total de tous les articles est: "+
[Link]()+"Da");

}
}

Common questions

Powered by AI

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 .

You might also like