Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

TP2 Java

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 can add items, count items, display items, get total quantity and total price. The FreshItem class extends Item to also store a best before date. A test class creates sample items, adds them to shopping carts, and displays totals.

Uploaded by

Aymen Douibi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

TP2 Java

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 can add items, count items, display items, get total quantity and total price. The FreshItem class extends Item to also store a best before date. A test class creates sample items, adds them to shopping carts, and displays totals.

Uploaded by

Aymen Douibi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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)
{ System.out.println("Shoppingcart is full "); }
else {
Shoppingcart [in]=it;
in++ ;}}
public int Itemcount()
{return in ;}
public void affichItem (){
for (int i=0;i<=4;i++)
{ System.out.println((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);
this.BestBeforeDate=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)
{ System.out.println(" FreshItem is full "); }
else {
cartitem [in]= fit ;
in++ ;}}
public int FreshItemcount()
{return in ;}
public void affichFreshItem (){
for (int i=0;i<=4;i++)
{ System.out.println((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();

cart1.addItem(item1);
cart1.addItem(item2);
cart1.addItem(item3);
cart1.addItem(item4);
cart1.addItem(item5);

cart1.affichItem();

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();

cart2.addFresh (fresh1);
cart2.addFresh (fresh2);
cart2.addFresh (fresh3);
cart2.addFresh (fresh4);
cart2.addFresh (fresh5);

System.out.println("le nombre total des articles est: "+


cart1.Itemcount());
System.out.println("la Quantite total des articles est: "+
cart1.Itemquantity ());
System.out.println("le prix total de tous les articles est: "+
cart1.totalPrice()+"Da");

}
}

You might also like