0% found this document useful (0 votes)
34 views25 pages

Part 9 - Collections

Uploaded by

abdellahlotfi05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views25 pages

Part 9 - Collections

Uploaded by

abdellahlotfi05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

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
public class ProductManager { ProductManager
//private Product product; ProductManager
//private Review review;
public void printProductReport(Product product){
private Map<Product, List<Review>> products=new HashMap<>();
//private Review[] reviews=new Review[5]; List<Review> reviews = products.get(product);
StringBuilder txt = new StringBuilder();
public Product createProduct(int id, String name, BigDecimal price, Rating rating, LocalDate bestBefore){ txt.append(MessageFormat.format(
Product product= new Food(id,name, price, rating, bestBefore); resources.getString("product")
products.putIfAbsent(product,new ArrayList<>()); , product.getName()
return product; , moneyFormat.format(product.getPrice())
} , product.getRating().getStars()
public Product createProduct(int id, String name, BigDecimal price, Rating rating){ , dateFormat.format(product.getBestBefore())
Product product= new Drink(id,name, price, rating); ));
products.putIfAbsent(product,new ArrayList<>()); txt.append("\n");
return product; for (Review review : reviews){
} ProductManager if(review==null){
break;
}
public Product reviewProduct(Product product, Rating rating, String comments){ txt.append(MessageFormat.format(
ProductManager resources.getString("review")
List<Review> reviews = products.get(product);
products.remove(product, reviews); , review.getRating().getStars()
reviews.add(new Review(rating, comments)); , review.getComments()
));
int sum = 0; ٍShop
for(Review review : reviews){ if(review!=null){
sum += review.getRating().ordinal();
} }
product = product.applyRating(Rateable.convert(Math.round((float) sum/reviews.size()))); txt.append("\n");
products.put(product, reviews); }
return product; if(reviews.isEmpty()){
} txt.append(resources.getString("no.reviews"));
txt.append("\n");
}

System.out.println(txt);
}
ProductManager productManager=new ProductManager(Locale.getDefault()); ٍShop
Product p1= productManager.createProduct(101,"Tea",BigDecimal.valueOf(13), Rating.TWO_STAR);
productManager.printProductReport(p1);
System.out.println("--------------------------------");
productManager.reviewProduct(p1,Rating.FIVE_STAR,"Very nice");
p1 = productManager.reviewProduct(p1, Rating.FOUR_STAR, "Good Product");
p1 = productManager.reviewProduct(p1, Rating.ONE_STAR, "Poor Product");
p1 = productManager.reviewProduct(p1, Rating.FIVE_STAR, "Excellent Product");
p1 = productManager.reviewProduct(p1, Rating.FIVE_STAR, "Fine Product");
p1 = productManager.reviewProduct(p1, Rating.ONE_STAR, "Poor Product");
productManager.printProductReport(p1);
Product p2= productManager.createProduct(101,"Coffee",BigDecimal.valueOf(1.6),Rating.NOT_RATED);
productManager.printProductReport(p2); Tea, price : 13,00 MAD, Rating : ★★☆☆☆, Best Before : 20/09/2023
System.out.println("--------------------------------"); Not Reviewed
productManager.reviewProduct(p2,Rating.FIVE_STAR,"Very nice"); --------------------------------
p2 = productManager.reviewProduct(p2, Rating.FOUR_STAR, "Good Product"); Tea, price : 13,00 MAD, Rating : ★★★★☆, Best Before : 20/09/2023
p2 = productManager.reviewProduct(p2, Rating.ONE_STAR, "Poor Product"); Review : ★★★★★ Very nice
p2 = productManager.reviewProduct(p2, Rating.THREE_STAR, "Satisfied"); Review : ★★★★☆ Good Product
productManager.printProductReport(p2); Review : ★☆☆☆☆ Poor Product
Review : ★★★★★ Excellent Product
Review : ★★★★★ Fine Product
Review : ★☆☆☆☆ Poor Product

Coffee, price : 1,60 MAD, Rating : ☆☆☆☆☆, Best Before : 20/09/2023


Not Reviewed
--------------------------------
Coffee, price : 1,60 MAD, Rating : ★★★☆☆, Best Before : 20/09/2023
Review : ★★★★★ Very nice
Review : ★★★★☆ Good Product
Review : ★☆☆☆☆ Poor Product
Review : ★★★☆☆ Satisfied
Review
public class Review implements Comparable<Review> { Tea, price : 13,00 MAD, Rating : ★★☆☆☆, Best Before : 21/09/2023
Not Reviewed
--------------------------------
@Override Tea, price : 13,00 MAD, Rating : ★★★★☆, Best Before : 21/09/2023
public int compareTo(Review otherReview) { Review : ★★★★★ Very nice
Review : ★★★★★ Excellent Product
return otherReview.rating.ordinal() - this.rating.ordinal(); Review : ★★★★★ Fine Product
} Review : ★★★★☆ Good Product
Review : ★☆☆☆☆ Poor Product
Review : ★☆☆☆☆ Poor Product

public void printProductReport(Product product){ Coffee, price : 1,60 MAD, Rating : ☆☆☆☆☆, Best Before : 21/09/2023
List<Review> reviews = products.get(product); Not Reviewed
--------------------------------
Collections.sort(reviews); Coffee, price : 1,60 MAD, Rating : ★★★☆☆, Best Before : 21/09/2023
…. Review : ★★★★★ Very nice
} Review : ★★★★☆ Good Product
Review : ★★★☆☆ Satisfied
} Review : ★☆☆☆☆ Poor Product
public abstract sealed class Product implements Rateable<Product> permits Food,Drink {
Review @Override
public class Review implements Comparable<Review> { public boolean equals(Object o) {
if (this == o) return true;
public Product findProduct(int id){ if (o == null ) return false;
Product result = null; if(o instanceof Product product){
for (Product product : products.keySet()){ //Product product = (Product) o;
if(product.getId() == id){ return id == product.id && Objects.equals(this.name,product.name);
result = product; } ProductManager pm = new ProductManager(Locale.getDefault());
break; return false; pm.createProduct(101, "Tea", BigDecimal.valueOf(1.99), Rating.NOT_RATED);
} } pm.reviewProduct(101, Rating.FOUR_STAR, "Good Product");
} pm.reviewProduct(101, Rating.ONE_STAR, "Poor Product");
}
pm.reviewProduct(101, Rating.FIVE_STAR, "Excellent Product");
return result; pm.reviewProduct(101, Rating.FIVE_STAR, "Fine Product");
} pm.reviewProduct(101, Rating.ONE_STAR, "Poor Product");
pm.printProductReport(101);
public Product reviewProduct(int productId, Rating rating, String comments){
Product product = findProduct(productId); pm.createProduct(103, "Cake", BigDecimal.valueOf(1.99), Rating.NOT_RATED,
LocalDate.now().plusDays(2));
return this.reviewProduct(product, rating, comments);
pm.reviewProduct(103, Rating.FOUR_STAR, "Good Product");
} pm.reviewProduct(103, Rating.ONE_STAR, "Poor Product");
pm.reviewProduct(103, Rating.FIVE_STAR, "Excellent Product");
public void printProductReport(int productId){ pm.printProductReport(103);
Product product = findProduct(productId);
Tea, price : 1,99 MAD, Rating : ★★★☆☆, Best Before : 21/09/2023
printProductReport(product);
Review : ★★★★★ Excellent Product
} Review : ★★★★★ Fine Product
} Review : ★★★★☆ Good Product
Review : ★☆☆☆☆ Poor Product
Review : ★☆☆☆☆ Poor Product

Cake, price : 1,99 MAD, Rating : ★★★☆☆, Best Before : 22/09/2023


Review : ★★★★★ Excellent Product
Review : ★★★★☆ Good Product
Review : ★☆☆☆☆ Poor Product

You might also like