0% found this document useful (0 votes)
18 views

Part 7-Interface and Generics

Uploaded by

abdellahlotfi05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Part 7-Interface and Generics

Uploaded by

abdellahlotfi05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Oracle Java 17 Developer Certification

Collections Arrays, Loops


Records

Modules
Inheritance Exception Handling
Generics
JDBC API
Class, Object Annotations
Interfaces Java Security

Java IO API
Java Concurrency & Multi Threading
Lambda Expressions Java Streams API
Mohamed Youssfi, Enseignant Chercheur ENSET Mohammedia, Université Hassan II de Casablanca Consultant R&D Ingénierie Logicielle
Rateable
package labs.pm.data;
@FunctionalInterface
public interface Rateable<T> {
public static final Rating DEFAULT_RATING = Rating.NOT_RATED;
public abstract T applyRating(Rating rating);
public default T applyRating(int stars){
return applyRating(convert(stars));
}
public default Rating getRating(){
return DEFAULT_RATING;
}
public static Rating convert(int stars){
return (stars>=0 && stars<=5)?Rating.values()[stars]:DEFAULT_RATING;
}
}
Product @Override
public String toString() {
public abstract sealed class Product implements Rateable<Product> permits Food,Drink { return "id=" + id +
protected final int id; ", name='" + name + '\'' +
protected final String name; ", price=" + price + ", Discount="+getDiscount()+
protected final BigDecimal price; ", rating=" + rating.getStars() ;
protected final Rating rating; }
public static int counter=0;
public static final BigDecimal DISCOUNT_RATE= BigDecimal.valueOf(0.1); @Override
public boolean equals(Object o) {
public Product(){ if (this == o) return true;
this(++counter,"Unknown",BigDecimal.ZERO,Rating.NOT_RATED); if (o == null ) return false;
} if(o instanceof Product product){
public Product(int id,String name, BigDecimal price,Rating rating) { //Product product = (Product) o;
this.id=id; return id == product.id && Objects.equals(this.name,product.name);
this.name = name; }
this.price = price; return false;
this.rating = rating; }
} @Override
public Product(int id,String name, BigDecimal price) { public int hashCode() {
this(id,name, price, Rating.NOT_RATED); return Objects.hash(id);
} }
}

public abstract LocalDate getBestBefore();


package labs.pm.data; Drink package labs.pm.data; Food
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalTime;
public final class Drink extends Product { public final class Food extends Product {
Drink(int id, String name, BigDecimal price, Rating rating) { private LocalDate bestBefore;
super(id, name, price, rating);
} Food(int id, String name, BigDecimal price, Rating rating, LocalDate bestBefore) {
@Override super(id,name, price, rating);
public String toString() { this.bestBefore = bestBefore;
return "Drink {"+super.toString()+"}"; }
}
@Override public LocalDate getBestBefore() {
public boolean equals(Object o) { return bestBefore;
return super.equals(o); }
}
@Override @Override
public BigDecimal getDiscount() { public BigDecimal getDiscount() {
LocalTime now = LocalTime.now(); return (bestBefore.isEqual(LocalDate.now()))?super.getDiscount():BigDecimal.ZERO;
return (now.isAfter(LocalTime.of(17,30)) && now.isBefore(LocalTime.of(22,30)))? }
super.getDiscount():BigDecimal.ZERO;
@Override
} public String toString() {
@Override return "Food {" + super.toString()+","+
public LocalDate getBestBefore() { "bestBefore=" + bestBefore +
return LocalDate.now().plusDays(1); '}';
} }

@Override @Override
public Product applyRating(Rating rating) { public Product applyRating(Rating rating) {
return new Drink(id, name, price, rating); return new Food(id,name,price, rating,bestBefore);
} }
} }
package labs.pm.data; Review

public class Review {


private Rating rating;
private String comments;

public Review(Rating rating, String comments) {


this.rating = rating;
this.comments = comments;
}

public Rating getRating() {


return rating;
}

public String getComments() {


return comments;
}

@Override
public String toString() {
return "Review{" +
"rating=" + rating +
", comments='" + comments + '\'' +
'}';
}
}
product = {0}, price : {1}, Rating : {2}, Best Before : {3} resources.prpperties
ProductManager review = Review : {0}\t{1}
no.reviews = Not Reviewed
public class ProductManager { public Product reviewProduct(Product product, Rating rating, String comments){
private Product product; this.review =new Review(rating, comments);
private Review review; this.product=product.applyRating(rating);
return this.product;
private Locale locale; }
private ResourceBundle resources;
private DateTimeFormatter dateFormat; public void printProductReport(){
private NumberFormat moneyFormat; StringBuilder txt = new StringBuilder();
txt.append(MessageFormat.format(
public ProductManager(Locale locale) { resources.getString("product")
this.locale = locale; , product.getName()
resources = ResourceBundle.getBundle("resources", locale); , moneyFormat.format(product.getPrice())
dateFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).localizedBy(locale); , product.getRating().getStars()
moneyFormat = NumberFormat.getCurrencyInstance(locale); , dateFormat.format(product.getBestBefore())
} ));
txt.append("\n");
public Product createProduct(int id, String name, BigDecimal price, Rating rating, LocalDate if(review!=null){
bestBefore){ txt.append(MessageFormat.format(
product= new Food(id,name, price, rating, bestBefore); resources.getString("review")
return product; , review.getRating().getStars()
} , review.getComments()
public Product createProduct(int id, String name, BigDecimal price, Rating rating){ ));
product= new Drink(id,name, price, rating); } else {
return product; txt.append(resources.getString("no.reviews"));
} }
txt.append("\n");
System.out.println(txt);
}
}
package labs.shop; Shop
import labs.pm.data.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Locale;

public class Shop {

public static void main(String[] args) {

ProductManager productManager=new ProductManager(Locale.getDefault());


Product p1= productManager.createProduct(101,"Soda",BigDecimal.valueOf(13), Rating.TWO_STAR);
productManager.printProductReport();
productManager.reviewProduct(p1,Rating.FIVE_STAR,"Very nice");
productManager.printProductReport();
}

} Soda, price : 13,00 MAD, Rating : ★★☆☆☆, Best Before : 04/09/2023


Not Reviewed

Soda, price : 13,00 MAD, Rating : ★★★★★, Best Before : 04/09/2023


Review : ★★★★★ Very nice

You might also like