Vous êtes sur la page 1sur 4

Ecole Supérieure d’economie numérique

---------------------------------------------------------------------------------------------------------------------------------

Module : Programmation Orientée Objet

Correction TD5

Remarque : Chaque classe et chaque interface est un fichier séparé.

public interface VendableKilo {


// cette méthode retourne le revenu de la vente
public abstract double vendre(double qte);

}
public interface vendablePiece {
// cette méthode retourne le revenu de la vente
public abstract double vendre(int qte);

}
public class Article {
private double prixAchat;
private double prixVente;
private String Nom;
private String Fournisseur;
//Constructeur
public Article(double prixAchat, double prixVente, String Nom, String
Fournisseur) {
this.prixAchat = prixAchat;
this.prixVente = prixVente;
this.Nom = Nom;
this.Fournisseur = Fournisseur;
}
//les methodes Getter
public double GetprixAchat()
{
return prixAchat;
}
public double GetprixVente()
{
return prixVente;
}
//methodes
public double rendement() {
return (prixVente - prixAchat) / prixAchat;
}

public void info() {


System.out.println("Nom du produit:" + Nom);
System.out.println("Fournisseur: " + Fournisseur);
System.out.println("Prix d'achat: " + prixAchat);
1
System.out.println("Prix de vente: " + prixVente);
}

}
public class ArticleElectromenager extends Article implements vendablePiece
{
public int stock;
// constructeur
public ArticleElectromenager(double pA, double pV, String n, String f)
{
super(pA,pV,n,f);
stock = 0;
}
// nouvelles méthodes
public double RemplirStock(int qte) {
stock = stock + qte;
return GetprixAchat() * qte;
}
// méthode surécrite
public void info() {
super.info();
System.out.println("Le stock du produit est " + stock + " pièce(s).");
}
// implementation de l'interface vendablePiece
public double vendre(int qte) {
if (qte<stock) {
stock = stock - qte;
return qte * GetprixVente();
}
else {
System.out.println(" Stock insuffisant!!! ");
return 0;
}
}

}
public class ArticlePrimeur extends Article implements VendableKilo
{
public double stock;
// constructeur
public ArticlePrimeur(double pA, double pV, String n, String f) {
super(pA,pV,n,f);
stock = 0.0;
}
// nouvelles méthodes
public double RemplirStock(double qte) {
stock = stock + qte;
return GetprixAchat() * qte;
}
// méthode surécrite
public void info() {
super.info();
System.out.println("Le stock du produit est " + stock + " kilogramme(s).");
}
// implementation de l'interface vendableKilo
public double vendre(double qte) {
if (qte<stock) {
2
stock = stock - qte;
return qte * GetprixVente();
}
else {
System.out.println(" Stock insuffisant!!! ");
return 0;
}
}
}
public class Magasin
{
public static int choixE = 2; // choixE est la taille du tableau Ae
public static int choixP = 2; // choixP est la taille du tableau Ap

public double Revenu;


public double Depense;
public ArticleElectromenager[] Ae;
public ArticlePrimeur[] Ap;
// constructeur
public Magasin() {
Ae = new ArticleElectromenager[choixE];
Ap = new ArticlePrimeur[choixP];
Revenu = 0;
Depense = 0;
}
// méthodes
public double rendement() {
return (Revenu - Depense) / Depense;
}
public void info() {
System.out.println("=======Informations Générales ======");
System.out.println("Les depenses du magasin: " + Depense);
System.out.println("Les revenus du magasin: " + Revenu);
System.out.println("Le rendement" + rendement());
System.out.println("====================================");
System.out.println("======= Liste des produits ======");
System.out.println("====================================");
for (int i=0; i<choixE; i++)
{
System.out.println("***Produit Electromenager n°"+(i+1)+"***");
Ae[i].info();
System.out.println("****************************************");
}
for (int i=0; i<choixP; i++)
{
System.out.println("***Produit Primeur n°"+(i+1)+"***");
Ap[i].info();
System.out.println("****************************************");
}
}
}

public class testMagasin {


public static void main (String[] args){
Magasin M1 = new Magasin();
// les produits a vendre
M1.Ae[0] = new ArticleElectromenager(27000, 35000, "télé", "Samsung");
M1.Ae[1] = new ArticleElectromenager(5000, 15000, "radio", "Sony");
3
M1.Ap[0] = new ArticlePrimeur(100, 200, "tomate", "Abdi, Tunisie");
M1.Ap[1] = new ArticlePrimeur(300, 500, "Poivre", "Abdi, Tunisie");
// remplir les stocks
for (int i=0; i<M1.choixE; i++)
{ M1.Depense += M1.Ae[i].RemplirStock(50);}

for (int i=0; i<M1.choixP; i++)


{ M1.Depense += M1.Ap[i].RemplirStock(100);}
// avoir les info
M1.info();
}
}

Vous aimerez peut-être aussi