0% ont trouvé ce document utile (0 vote)
38 vues4 pages

TP3

Le document présente une implémentation d'un système de gestion de panier d'achats en Java, incluant des classes pour les articles, le panier, les articles frais et les factures. Il décrit les méthodes pour ajouter, supprimer et compter les articles ainsi que pour calculer le prix total. Les classes sont organisées en différents fichiers et utilisent des accesseurs pour manipuler les données des articles.

Transféré par

Chayma Baklouti
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
38 vues4 pages

TP3

Le document présente une implémentation d'un système de gestion de panier d'achats en Java, incluant des classes pour les articles, le panier, les articles frais et les factures. Il décrit les méthodes pour ajouter, supprimer et compter les articles ainsi que pour calculer le prix total. Les classes sont organisées en différents fichiers et utilisent des accesseurs pour manipuler les données des articles.

Transféré par

Chayma Baklouti
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd

objectifs de tp:

***connaitre la notion d'accesseurs et comment définir des classes de méme packets.


difficultés:
implémentation du tableau item et l'id

***pour le fichier item.java:


package chimou;
public class item{
private String nom;
private long price ;
private int poids ;
public item(String nom, long price ,int poids) {
this.nom=nom;
this.price=price;
this.poids=poids;
}
public String getname() {
return nom;}
public long getprice() {
return price;
}
public long getpoids() {
return poids;
}
public String tostring() {
long p=price/100;
long r=price%100;
return String.format("%s:%d.%02d$",nom,p,r);
}
}

***pour le fichier shoppingcart.java:

package chayma;

public class shoppingcart {


private item t[];
private int id;
private static int nextid = 1;
private int c;
public shoppingcart() {
this.t=new item[100];
this.id = nextid++;
this.c=0;

}
public void additem(item el) {
t[c]=el;
c++;
}
public Boolean removeitem(item el) {
for( int i=0;i<c;i++) {
if(t[i].equals(el)) {
for (int j = i; j < c-1; j++) {
t[j] = t[j + 1];
}
t[c-1] = null;
c--;
return true;
}
}
return false;
}
public int countitem() {
return c;
}
public int getid() {
return id;
}
public long tprice() {
long s=0;
for (int i = 0; i < c; i++) {
s+=t[i].getprice();
}
return s;
}
public String tostring() {
StringBuilder sb =new StringBuilder();

sb.append("panier").append(id).append("]").append(countitem()).append("article(s)]\
n");
for (item item:t) {
sb.append("").append(item).append("\n");
}
return sb.toString();
}
}

***pour le fichier shoppingcart.java:

package chayma;
import java.time.LocalDate;
public class FreshItem extends item {
private String d;
private String da;
public FreshItem ( String d,int price,int poids,String nom) {
super(nom,price,poids);
this.d=d;
this.da= LocalDate.now().toString();
}
public String tostring() {
long p=getprice()/100;
long r=getprice()%100;
return String.format("%s:%s %d.%02d$",d,getname(),p,r);
}
public String isExpired(String currentDate) {
String p1[]=d.split("-");
String p2[]=da.split("-");
if (Integer.parseInt(p1[0])>Integer.parseInt(p2[0])) {
return "true";}
else {
if (Integer.parseInt(p1[1])>Integer.parseInt(p2[1])) {
return "true";}
else {
if (Integer.parseInt(p1[2])>Integer.parseInt(p2[2])) {
return "true";}
else {
return "false";}}}
}
}

***pour le fichier invoice.java:

package chayma;
public class invoice {
private item titem [];
private int c;
public invoice() {
this.titem=new item[100];
this.c=0;
}
public void additem (item el) {
if(c<titem.length) {
titem[c]=el;
c++;
}}
public double toprice() {
double t=0;
for(int i=0;i<c;i++) {
t+=titem[i].getprice()/100.0;
}
return t;

}
public String tostring() {
StringBuilder sb =new StringBuilder();
sb.append("facture");
if(c==0) {
sb.append("aucun article ");
}
else {
for (int i=0; i<c; i++) {
sb.append("- ").append(titem[i].toString());
}
sb.append("Total: ").append(String.format("%.2f", toprice())).append("
€");
}
return sb.toString();
}
}

***pour le fichier main.java:

package chayma;
public class main {
public static void main(String[] args) {
item item1=new item("corn flakes",500,1000);
shoppingcart cart =new shoppingcart();
cart.additem(item1);
System.out.println(item1.getprice());
System.out.println(item1.getname());
System.out.println(item1.tostring());
item item2=new item("caviar",50000,100);
cart.additem(item2);
System.out.println(cart.getid());
System.out.println(cart.countitem());
System.out.println(cart.tprice());
System.out.println(cart.countitem());
shoppingcart cart2=new shoppingcart();
shoppingcart cart3=new shoppingcart();
item item3=new item("water",101,120);
cart3.additem(item3);
System.out.println(cart2.getid());
System.out.println(cart3.getid());
cart3.removeitem(item3);
item tin =new item("sardine",500,500);
FreshItem fresh =new FreshItem("Salmon",1450,800,"2012-04-11");
cart.additem(fresh);
cart.additem(tin);
System.out.println(fresh.tostring());
}
}

Vous aimerez peut-être aussi