Skip to content
Snippets Groups Projects
Commit 1d7eee6d authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

Lab3-Task3 fait!

parent 46fd75b1
No related branches found
No related tags found
No related merge requests found
public interface Calories {
public static int caloriesValueOf(Fruits f){
return f.getCalories();
}
public void connect(Fruit f);
public void setQuotient(int q);
public int getQuotient();
public int value(Fruits f);
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.ArrayList;
public class CaloriesEnrichi implements Calories{
private String[] fruits = {"Avocat","Banane","Mangue"};
private int quotient = 100;
private ArrayList<Fruit> observer = new ArrayList<>();
public int getQuotient(){
return this.quotient;
}
public void setQuotient(int q){
this.quotient = q;
notifyAll(observer);
}
public void connect(Fruit f){
observer.add(f);
}
public void notifyAll(ArrayList<Fruit> list){
for(Fruit f: list){
f.update();
}
}
public int value(Fruits f){
for(int i = 0; i<fruits.length; i++){
if(fruits[i].equals(f.getName())){
return quotient/(i+1);
}
}
return 0;
}
}
\ No newline at end of file
import java.util.ArrayList;
public class CaloriesNormal implements Calories{
private String[] fruits = {"Pomme","Fraise","Myrtille"};
private int quotient = 50;
private ArrayList<Fruit> observer = new ArrayList<>();
public int getQuotient(){
return this.quotient;
}
public void setQuotient(int q){
this.quotient = q;
notifyAll(observer);
}
public void connect(Fruit f){
observer.add(f);
}
public void notifyAll(ArrayList<Fruit> list){
for(Fruit f: list){
f.update();
}
}
public int value(Fruits f){
for(int i = 0; i<fruits.length; i++){
if(fruits[i].equalsIgnoreCase(f.getName())){
return quotient/(i+1);
}
}
return 0;
}
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.ArrayList;
public class CaloriesPauvre implements Calories{
private String[] fruits = {"Poire","Melon","Pastèque"};
private int quotient = 25;
private ArrayList<Fruit> observer = new ArrayList<>();
public int getQuotient(){
return this.quotient;
}
public void setQuotient(int q){
this.quotient = q;
notifyAll(observer);
}
public void connect(Fruit f){
observer.add(f);
}
public void notifyAll(ArrayList<Fruit> list){
for(Fruit f: list){
f.update();
}
}
public int value(Fruits f){
for(int i = 0; i<fruits.length; i++){
if(fruits[i].equals(f.getName())){
return quotient/(i+1);
}
}
return 0;
}
}
\ No newline at end of file
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Calories normal = new CaloriesNormal();
Calories pauvre = new CaloriesPauvre();
Calories riche = new CaloriesEnrichi();
Fruit fraise = new Fruit(normal,"Fraise",false);
Fruit melon = new Fruit(pauvre,"Melon",false);
Fruit avocat = new Fruit(riche,"Avocat",true);
Fruit banane = new Fruit(riche,"Banane",false);
Fruit banane2 = new Fruit(riche,"Banane",false);
normal.setQuotient(30);
riche.setQuotient(120);
}
}
\ No newline at end of file
public class Fruit implements Fruits{
private String name;
private boolean pepin;
private Calories calories;
public Fruit(Calories cal,String n, boolean p){
this.calories = cal;
this.name = n;
this.pepin = p;
cal.connect(this);
}
public void setName(String n){
this.name = n;
}
public void setPepin(boolean p){
this.pepin = p;
}
public String getName(){
return this.name;
}
public int getCalories(){
return this.calories.value(this);
}
public boolean getPepin(){
return this.pepin;
}
public void update(){
System.out.println("Les calories on été modifié pour " + this.name + ": " + this.getCalories());
}
public void afficherFruit(){
System.out.println("");
System.out.println(this.name + " - " + this.pepin);
System.out.println("");
}
public void afficherCalories(){
System.out.println("Calories: " + getCalories());
}
}
\ No newline at end of file
public interface Fruits {
public String getName();
public int getCalories();
public boolean getPepin();
public void afficherFruit();
public void afficherCalories();
}
\ No newline at end of file
import java.util.ArrayList;
public class PanierFruit implements Fruits{
private ArrayList<Fruits> panier = new ArrayList<>();
private String name = "Panier(";
private boolean pepin = false;
public PanierFruit(Fruits... fruits){
this.addAll(fruits);
}
public String getName(){
return this.name;
}
public int getCalories(){
int res = 0;
for(Fruits f: panier){
res += Calories.caloriesValueOf(f);
}
return res;
}
public void add(Fruits f){
panier.add(f);
name += " " + f.getName() + " ";
if(f.getPepin()) pepin = true;
}
public void addAll(Fruits... fruits){
for(Fruits f: fruits){
this.add(f);
}
}
public boolean getPepin(){
return pepin;
}
public void afficherFruit(){
System.out.println("");
System.out.print(name);
if(pepin){
System.out.print(") Avec Pépin");
}
else{
System.out.print(") Sans Pépin");
}
System.out.println("");
System.out.println("");
}
public void afficherCalories(){
System.out.println("Calories: " + this.getCalories());
}
}
\ No newline at end of file
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment