Skip to content
Snippets Groups Projects
Commit 4641b4d7 authored by Baptiste Royer's avatar Baptiste Royer
Browse files

Ajout des précédents TP

parent 29cee14d
No related branches found
No related tags found
No related merge requests found
Showing
with 451 additions and 0 deletions
tp06 @ a3a413dd
Subproject commit a3a413ddd7a02a4055d86a2a8452438bba4ab392
{
"java.debug.settings.onBuildFailureProceed": true
}
\ No newline at end of file
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
package tpOO.tp08;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class BreedingGroundTest {
public Participant p1,p2,p3,p4,p5,p6,p7,p8,p9,p10;
public BreedingGround v1,v2,v3;
public List<Participant> partList;
public Set<Participant> partSet;
@BeforeEach
public void initTest() {
partList = new ArrayList<Participant>();
partSet = new HashSet<Participant>();
p1 = new Participant("alice");
p2 = new Participant("bruno"); // the cheater registered in v1 and v3
p3 = new Participant("clement");
p4 = new Participant("dominique");
p5 = new Participant("elia");
p6 = new Participant("flore");
p7 = new Participant("gudul");
p8 = new Participant("hercule");
p9 = new Participant("isidor");
p10 = new Participant("jules");
// appariement des participants
p1.matchWith(p2);
p5.matchWith(p6);
p8.matchWith(p9);
v1 = new BreedingGround();
v1.registration(p1);
v1.registration(p2);
v1.registration(p3);
v1.registration(p4);
v2 = new BreedingGround();
v2.registration(p5);
v2.registration(p6);
v3 = new BreedingGround();
v3.registration(p8);
v3.registration(p9);
v3.registration(p10);
v3.registration(p2);
}
@Test
public void testRegistration() {
assertEquals(4, v1.getApplicants().size());
assertTrue(v1.getApplicants().contains(p1) && v1.getApplicants().contains(p2)
&& v1.getApplicants().contains(p3) && v1.getApplicants().contains(p4));
v1.clear();
assertEquals(Collections.emptySet(), v1.getApplicants());
assertTrue(v1.registration(p1));
assertEquals(1, v1.getApplicants().size());
assertTrue(v1.getApplicants().contains(p1));
assertFalse(v1.registration(p1));
}
@Test
public void testLoners() {
assertEquals(0, v2.loners().size());
assertEquals(Collections.emptyList(), v2.loners());
partList = v1.loners();
assertEquals(2, partList.size());
assertTrue(partList.contains(p3) && partList.contains(p4));
}
@Test
public void testLonersCleansing() {
v1.lonersCleansing();
assertEquals(2, v1.getApplicants().size());
assertTrue(v1.getApplicants().contains(p1) && v1.getApplicants().contains(p2));
v2.lonersCleansing();
assertEquals(2, v2.getApplicants().size());
assertTrue(v2.getApplicants().contains(p5) && v2.getApplicants().contains(p6));
}
@Test
public void testCheaters() {
assertEquals(Collections.emptyList(), v1.cheaters(v2));
partList.add(p2);
assertEquals(partList, v1.cheaters(v3));
}
@Test
public void testCheatersBan() {
assertTrue(p1.isMatchedWith(p2));
assertTrue(p2.isMatchedWith(p1));
v1.isolateCheater(p2);
assertFalse(p1.isMatchedWith(p2));
assertFalse(p2.isMatchedWith(p1));
}
@Test
public void testCheatersCleansing() {
partSet = v1.getApplicants();
v1.cheatersCleansing(v2);
assertEquals(partSet, v1.getApplicants());
partSet.clear();
partSet.add(p1); partSet.add(p3); partSet.add(p4);
v1.cheatersCleansing(v3);
assertEquals(partSet, v1.getApplicants());
}
@Test
public void testPossibleMerging() {
assertFalse(v1.possibleMerging(v1));
assertTrue(v1.possibleMerging(v2));
assertFalse(v1.possibleMerging(v3));
}
@Test
public void testMerge() {
partSet = v1.getApplicants();
partSet.addAll(v2.getApplicants());
v1.merging(v2);
assertEquals(partSet, v1.getApplicants());
partSet.clear();
partSet.addAll(v1.getApplicants());
partSet.addAll(v2.getApplicants());
partSet.addAll(v3.getApplicants());
v1.merging(v3);
assertEquals(partSet, v1.getApplicants());
}
@Test
public void testSecuredMerging() {
partSet = v1.getApplicants();
partSet.add(p8); partSet.add(p9); partSet.add(p10);
v1.securedMerging(v3);
assertEquals(partSet, v1.getApplicants());
partSet.addAll(v2.getApplicants());
v1.securedMerging(v2);
assertEquals(partSet, v1.getApplicants());
}
}
package tp08;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Food implements IProduct, Comparable<Food> {
private String label;
private double price;
private LocalDate bestBeforDate;
private static int refIconnue = 0;
public Food(String label, double price, LocalDate bestBeforDate) {
if(label == null) {
this.label = "refUnknown" + refIconnue;
refIconnue++;
} else {
this.label = label;
}
this.price = price;
this.bestBeforDate = bestBeforDate;
}
public Food(String label, double price) {
if(label == null) {
this.label = "refUnknown" + refIconnue;
refIconnue++;
} else {
this.label = label;
}
this.price = price;
this.bestBeforDate = LocalDate.now().plus(10, ChronoUnit.DAYS);
}
public String getLabel() {
return this.label;
}
public double getPrice() {
return this.price;
}
public LocalDate getBestBeforDate() {
return this.bestBeforDate;
}
public boolean isPerishable() {
return true;
}
public boolean isBestBefore(LocalDate date) {
return this.bestBeforDate.isBefore(date);
}
@Override
public int compareTo(Food arg0) {
if(this.isBestBefore(arg0.getBestBeforDate())) {
return 1;
} else {
return -1;
}
}
public String toString() {
return "[" + this.label + "=" + this.price + " -> before " + this.bestBeforDate + "]";
}
}
\ No newline at end of file
package tp08;
public class Furniture implements IProduct {
private String label;
private double price;
private static int refInconnue = 0;
public Furniture(String label, double price) {
if(label == null) {
this.label = "refUnknown" + refInconnue;
refInconnue++;
} else {
this.label = label;
}
this.price = price;
}
public String getLabel() {
return this.label;
}
public double getPrice() {
return this.price;
}
public boolean isPerishable() {
return false;
}
public String toString() {
return "[" + this.label + "=" + this.price + "]";
}
}
\ No newline at end of file
package tp08;
public interface IProduct {
public double getPrice();
public boolean isPerishable();
}
\ No newline at end of file
package tp08;
import java.util.ArrayList;
public class Shelf{
private boolean refregirated;
private int capacityMax;
private ArrayList<IProduct> products;
public Shelf(boolean refregirated, int capacityMax){
this.refregirated = refregirated;
this.capacityMax = capacityMax;
this.products = new ArrayList<IProduct>();
}
public ArrayList<IProduct> getShelves(){
return this.products;
}
public int getCapacityMax(){
return this.capacityMax;
}
public boolean isFull(){
return this.products.size() == this.capacityMax;
}
public boolean isEmpty(){
return this.products.size() == 0;
}
public boolean isRefregirated(){
return this.refregirated;
}
public String toString() {
return "[" + isRefregirated() + ": " + getCapacityMax() + " -> " + getShelves().toString() + "]";
}
public boolean add(IProduct product){
if(isFull()){
return false;
}
this.products.add(product);
return true;
}
}
\ No newline at end of file
package tp08;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
public class Shop {
private ArrayList<Shelf> shelves;
public Shop(){
this.shelves = new ArrayList<Shelf>();
}
public Shop(ArrayList<Shelf> shelves){
this.shelves = shelves;
}
public ArrayList<Shelf> getShelving(){
return this.shelves;
}
public String toString() {
String retour = "";
for(Shelf shelf : this.shelves){
retour += shelf.toString() + "\n";
}
return retour;
}
public void tidy(ArrayList<IProduct> products){
for(int i = 0; i<products.size(); i++) {
if(products.get(i).isPerishable()){
for(Shelf shelf : this.shelves){
if(shelf.isRefregirated() && shelf.add(products.get(i))){
products.remove(i);
i--;
break;
}
}
} else {
for(Shelf shelf : this.shelves){
if(!shelf.isRefregirated() && shelf.add(products.get(i))){
products.remove(i);
i--;
break;
}
}
}
}
}
public ArrayList<IProduct> closeBestBeforeDate(int nbDays) {
ArrayList<IProduct> retour = new ArrayList<IProduct>();
for (Shelf shelf : this.shelves) {
if(shelf.isRefregirated()) {
for (IProduct product : shelf.getShelves()) {
if (((Food)product).isBestBefore(LocalDate.now().plus(nbDays, ChronoUnit.DAYS))) {
retour.add(product);
}
}
}
}
return retour;
}
}
\ No newline at end of file
package tp08;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.time.LocalDate;
import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ShopTest {
public IProduct p1, p2, p3, p4, p5;
public ArrayList<IProduct> aStock;
public Shelf e1, e2, e3, e4;
public ArrayList<Shelf> shelves;
public Shop m;
@BeforeEach
public void initialization() {
this.p1 = new Food("pasta", 3.25, LocalDate.of(2019, 1, 1));
this.p2 = new Furniture("chair", 50.0);
this.p3 = new Furniture("table", 100.0);
this.p4 = new Food("fish", 10.0);
this.p5 = new Food("meat", 15.0, LocalDate.of(2019, 1, 1));
this.aStock = new ArrayList<IProduct>();
this.aStock.add(p1); this.aStock.add(p2); this.aStock.add(p3); this.aStock.add(p4); this.aStock.add(p5);
this.e1 = new Shelf(false, 1);
this.e2 = new Shelf(false, 2);
this.e3 = new Shelf(true, 2);
this.e4 = new Shelf(true, 2);
this.shelves = new ArrayList<Shelf>();
this.shelves.add(e1); this.shelves.add(e2); this.shelves.add(e3); this.shelves.add(e4);
this.m = new Shop(shelves);
}
@Test
public void initTest() {
ArrayList<Shelf> tmp = m.getShelving();
assertEquals(4, tmp.size());
assertTrue(tmp.get(0).isEmpty());
assertFalse(tmp.get(0).isRefregirated());
assertTrue(tmp.get(1).isEmpty());
assertFalse(tmp.get(1).isRefregirated());
assertTrue(tmp.get(2).isEmpty());
assertTrue(tmp.get(2).isRefregirated());
assertTrue(tmp.get(3).isEmpty());
assertTrue(tmp.get(3).isRefregirated());
}
@Test
public void tidyTest() {
ArrayList<Shelf> tmp = m.getShelving();
m.tidy(this.aStock);
assertTrue(tmp.get(0).isFull());
assertEquals(this.p2, tmp.get(0).getShelves().get(0));
assertFalse(tmp.get(1).isFull());
assertFalse(tmp.get(1).isEmpty());
assertEquals(this.p3, tmp.get(1).getShelves().get(0));
assertTrue(tmp.get(2).isFull());
assertEquals(this.p1, tmp.get(2).getShelves().get(0));
assertEquals(this.p4, tmp.get(2).getShelves().get(1));
assertFalse(tmp.get(3).isFull());
assertFalse(tmp.get(3).isEmpty());
assertEquals(this.p5, tmp.get(3).getShelves().get(0));
}
@Test
public void closeBeforeDateTest() {
m.tidy(this.aStock);
ArrayList<IProduct> res = m.closeBestBeforeDate(-3);
assertEquals(2, res.size());
assertEquals(this.p1, res.get(0));
assertEquals(this.p5, res.get(1));
}
}
package tp08;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UseComparable{
public static void main(String[] args) {
Food f1 = new Food("pasta", 3.25, LocalDate.of(2019, 1, 1));
Food f2 = new Food("fish", 10.0, LocalDate.of(2019, 1, 10));
Food f3 = new Food("meat", 15.0, LocalDate.of(2019, 1, 3));
List<Food> storage = new ArrayList<Food>();
storage.add(f1);storage.add(f2);storage.add(f3);
System.out.println(storage);
Collections.sort(storage);
System.out.println(storage);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment