Skip to content
Snippets Groups Projects
Commit 14a7af1d authored by Lilith Camplin's avatar Lilith Camplin :speech_balloon:
Browse files

tpOO-08::exo-miseEnRayon (Unfinished)

parent 9509668e
Branches
No related tags found
No related merge requests found
Pipeline #25208 passed
Showing
with 695 additions and 310 deletions
package td.td07;
public class Fish implements IAnimal{
private static final String SHOUT = "Blob!";
private static final int QTY = 1;
private static final boolean PET = false;
@Override
public String shout() {
return SHOUT;
}
@Override
public int foodAmount() {
return QTY;
}
@Override
public boolean isPet() {
return PET;
}
}
package td.td07;
public interface IAnimal {
public String shout ();
public int foodAmount ();
public boolean isPet();
}
package td.td07;
import java.util.HashMap;
public class PhoneBook {
private HashMap<String, PhoneNumber> directory;
public PhoneBook() {
this.directory = new HashMap<>();
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("PhoneBook{");
sb.append("directory=").append(directory);
sb.append('}');
return sb.toString();
}
}
package td.td07;
public class PhoneNumber {
private int countryCode;
private int areaCode;
private int secteurCode;
private int one;
private int two;
private int three;
public PhoneNumber(int countryCode, int areaCode, int secteurCode, int one, int two, int three) {
this.countryCode = countryCode;
this.areaCode = areaCode;
this.secteurCode = secteurCode;
this.one = one;
this.two = two;
this.three = three;
}
public String standartFormat () {
return "" + this.secteurCode + this.areaCode + this.one + this.two + this.three;
}
public String internationalFormat () {
return "+" + this.countryCode + this.standartFormat();
}
@Override
public String toString() {
return "Standard Format : " + this.standartFormat() + "\nInternational Format : " + this.internationalFormat();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PhoneNumber that)) return false;
if (countryCode != that.countryCode) return false;
if (areaCode != that.areaCode) return false;
if (secteurCode != that.secteurCode) return false;
if (one != that.one) return false;
if (two != that.two) return false;
return three == that.three;
}
@Override
public int hashCode() {
int result = countryCode;
result = 31 * result + areaCode;
result = 31 * result + secteurCode;
result = 31 * result + one;
result = 31 * result + two;
result = 31 * result + three;
return result;
}
}
package td.td07;
public class Pig implements IAnimal{
private final static String SHOUT = "Groink!";
private final static boolean PET = true;
private int weight;
private int mealSize;
public Pig (int weight) {
this.weight = weight;
this.mealSize = 20 + weight / 2;
}
@Override
public String shout() {
return SHOUT;
}
@Override
public int foodAmount() {
return mealSize;
}
@Override
public boolean isPet() {
return PET;
}
public void rollingInMud () {
System.out.println("Pig is rolling in mud");
}
}
package td.td07;
import java.util.Random;
public class Tiger implements IAnimal{
private static final String SHOUT = "Purr";
private static final int QTYMAX = 20;
private static final Random RAND = new Random();
private static final boolean PET = false;
@Override
public String shout() {
return SHOUT;
}
@Override
public int foodAmount() {
if (RAND.nextInt(2) == 0) return QTYMAX;
else return 0;
}
@Override
public boolean isPet() {
return PET;
}
}
package td.td07;
import java.util.ArrayList;
public class UseAnimal {
private static ArrayList<IAnimal> zoo = new ArrayList<>();
public static void main(String[] args) {
zoo.add(new Pig(20));
zoo.add(new Fish());
zoo.add(new Tiger());
for (IAnimal animal:zoo) {
System.out.println(animal.shout() + animal.foodAmount() + animal.isPet());
}
}
}
package td.td09;
public enum Mat {
ALGO, BDD, SYS, COM, EN, PPP, MATH, GESTION
}
package td.td09;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Person {
private String name;
private LocalDate birth;
public Person (String name, LocalDate birth) {
this.name = name;
this.birth = birth;
}
public String getName() {
return name;
}
public LocalDate getBirth() {
return birth;
}
public long getAge () {
return this.birth.until(LocalDate.now(), ChronoUnit.YEARS);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", birth=" + birth +
'}';
}
}
package td.td09;
import org.jetbrains.annotations.NotNull;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Spliterator;
import java.util.function.Consumer;
public class Student implements Iterable<Double>, Comparable<Student>{
private Person person;
private String INE;
private static int cptStudent = 0;
public static boolean civilianDisplay = true;
private Map<Mat, Double> grades;
public Student(String name, LocalDate birth, Map<Mat, Double> grades) {
this.INE = String.valueOf(Student.cptStudent++);
this.person = new Person(name, birth);
this.grades = grades;
}
public Student (String name, LocalDate birth) {
this(name,birth,new HashMap<>());
}
public void addGrade (Mat mat, double grade) {
this.grades.put(mat, grade);
}
public void addGrade (String mat, double grade) {
this.addGrade(Mat.valueOf(mat.toUpperCase()), grade);
}
public double computeOverallGrade () {
double sum = 0;
if (this.grades.isEmpty()) return -1;
else {
for (Double grade: this.grades.values()) {
sum += grade;
}
return sum/this.grades.size();
}
}
private String toStringCivilian () {
return this.person.toString();
}
private String toStringPedagogic () {
return this.INE + ":" + this.person.getName() + ":" + this.computeOverallGrade() + "-->" + this.grades;
}
@Override
public String toString() {
if (Student.civilianDisplay) return this.toStringCivilian();
return this.toStringPedagogic();
}
@NotNull
@Override
public Iterator<Double> iterator() {
return this.grades.values().iterator();
}
@Override
public void forEach(Consumer<? super Double> action) {
Iterable.super.forEach(action);
}
@Override
public Spliterator<Double> spliterator() {
return Iterable.super.spliterator();
}
@Override
public int compareTo(@NotNull Student other) {
return Double.compare(this.computeOverallGrade(), other.computeOverallGrade());
}
}
package tp.tp08;
import org.jetbrains.annotations.NotNull;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Food implements IProduct, Comparable<Food>{
private String label;
private double price;
private LocalDate bestBeforeDate;
private static int cptUnknown;
public Food(String label, double price, LocalDate bestBeforeDate) {
if (label == null) this.label = "refUnknown" + cptUnknown++;
else this.label = label;
this.price = price;
this.bestBeforeDate = bestBeforeDate;
}
public Food(String label, double price) {
this(label, price, LocalDate.now().plusDays(10));
}
public String getLabel () {
return this.label;
}
@Override
public double getPrice() {
return this.price;
}
public LocalDate getBestBeforeDate() {
return bestBeforeDate;
}
@Override
public boolean isPerishable() {
return true;
}
public boolean isBestBefore(LocalDate aDate) {
return aDate.isBefore(this.bestBeforeDate);
}
@Override
public String toString () {
return "[" + this.label + "=" + this.price + " -> before " + this.bestBeforeDate + "]";
}
@Override
public int compareTo(@NotNull Food o) {
return (int)o.bestBeforeDate.until(this.bestBeforeDate, ChronoUnit.DAYS);
}
}
package tp.tp08;
public class Furniture implements IProduct{
private String label;
private double price;
public Furniture(String label, double price) {
this.label = label;
this.price = price;
}
public String getLabel() {
return label;
}
@Override
public double getPrice() {
return price;
}
@Override
public boolean isPerishable() {
return false;
}
@Override
public String toString () {
return "[" + this.label + "=" + this.price + "]";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment