Collection Framework - Guided Exercise - CustomerRewardsSystem – Mastering Map, Set, and Stream Ops in Java #111
Replies: 4 comments
-
package com.practice.problems.java;
import java.util.*;
import java.util.stream.*;
class Customer {
private int id;
private String name;
private String mobile;
private int points;
public Customer(int id, String name, String mobile, int points) {
this.id = id;
this.name = name;
this.mobile = mobile;
this.points = points;
}
public int getId() { return id; }
public String getName() { return name; }
public String getMobile() { return mobile; }
public int getPoints() { return points; }
public void setPoints(int points) { this.points = points; }
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", points=" + points +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Customer)) return false;
Customer c = (Customer) o;
return this.mobile.equals(c.getMobile());
}
@Override
public int hashCode() {
return mobile.hashCode();
}
}
class RewardsManager {
private Map<Integer, Customer> customerMap = new TreeMap<>();
private Set<String> mobileSet = new HashSet<>();
public void registerCustomerWithValidation(Customer c) {
if (!mobileSet.contains(c.getMobile())) {
customerMap.put(c.getId(), c);
mobileSet.add(c.getMobile());
System.out.println("Registered: " + c);
} else {
System.out.println("Duplicate mobile found: " + c.getMobile());
}
}
public void addPoints(int id, int points) {
customerMap.computeIfPresent(id, (key, customer) -> {
customer.setPoints(customer.getPoints() + points);
return customer;
});
}
public void boostHighValueCustomers() {
customerMap.replaceAll((id, c) -> {
if (c.getPoints() >= 500) {
c.setPoints(c.getPoints() * 2);
}
return c;
});
}
public void removeInactiveCustomers() {
customerMap.entrySet().removeIf(entry -> entry.getValue().getPoints() < 50);
}
public void showPremiumCustomers() {
System.out.println("Premium Customers:");
customerMap.values().stream()
.filter(c -> c.getPoints() > 1000)
.forEach(System.out::println);
}
public void printSorted() {
System.out.println("Sorted Customers:");
customerMap.forEach((id, customer) ->
System.out.println(id + " => " + customer.getName() + ", Points: " + customer.getPoints()));
}
public void displayAll() {
customerMap.forEach((id, customer) -> System.out.println(id + " -> " + customer));
}
}
public class Main {
public static void main(String[] args) {
RewardsManager manager = new RewardsManager();
manager.registerCustomerWithValidation(new Customer(101, "Ravi", "9876543210", 100));
manager.registerCustomerWithValidation(new Customer(102, "Seema", "9123456789", 550));
manager.registerCustomerWithValidation(new Customer(103, "Tina", "9876543210", 70));
manager.addPoints(101, 50);
manager.boostHighValueCustomers();
System.out.println("\nBefore Removing Inactives:");
manager.displayAll();
manager.removeInactiveCustomers();
System.out.println("\nAfter Removing Inactives:");
manager.displayAll();
manager.showPremiumCustomers();
manager.printSorted();
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Lohith R package org.example; import java.text.ParseException; public class Practice { } class Customer { // Getters and Setters } class RewardsManager { // private Map<Integer,Customer> customerMap = new HashMap<>(); } |
Beta Was this translation helpful? Give feedback.
-
**
** |
Beta Was this translation helpful? Give feedback.
-
|
Bhavana import java.util.; class Customer { } class RewardsManager { } public class Main { } |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
🧪 Exercise:
CustomerRewardsSystem – Mastering Map, Set, and Stream Ops in Java🏢 Context: Welcome to LambdaMart CRM Team
LambdaMart has launched a loyalty program for its repeat customers. You're building the RewardsManager system that uses
HashMap,HashSet, andStream API enhancementsto manage:Customer registrations
Unique mobile numbers
Reward points management
Duplicate detection
Fraud cleanup
High-value customer segmentation
🎯 Key Concepts Covered
HashMap<K,V>,LinkedHashMap,TreeMapHashSet<T>,LinkedHashSet,TreeSetMethods like
computeIfAbsent,putIfAbsent,replaceAll,removeIf,forEachStreamson maps/setsClean coding + real-world use cases
📦 Files to Create
Customer.java– modelRewardsManager.java– logic layerMain.java– test file🧩 Part 1: Define the
CustomerClassCustomer.java✅ Task A: Fill in the blanks
🧩 Part 2: Customer Management with Map
RewardsManager.java✅ Task B: Predict the output
✍️ What will the final points for Ravi be?
Answer:
__________Why?
__________✅ Task C: Write a method to increase reward points
✍️ Rewrite this using lambda block style and comment each step inline.
🧩 Part 3: Mobile Validation Using Set
✅ Task D: Ensure mobile numbers are unique
Create a
Set<String>to track already-used mobile numbers.🧠 Challenge:
What if the same number is used but with a different customer ID?
How would you prevent duplicates without relying on IDs?
✍️ Write a plan in 2 lines:
🧩 Part 4: Removing Suspicious or Inactive Entries
✅ Task E: Use
removeIf()onSetorMap.entrySet()✅ Task F: Use
replaceAll()to boost high-value customers🧩 Part 5: Sorting and Segmentation
✅ Task G: Migrate to TreeMap for sorted display
✅ Add this method:
✅ Task H: Filter high-reward customers using stream
🧩 Part 6: Main Class for Testing
Main.java✍️ Add 2 more customers manually in main, one of them having exactly 1000 points.
📘 Clean Code Practices Recap
🧠 Final Reflection
What’s the difference between
replace()andreplaceAll()?Why does
HashSetrequireequals()andhashCode()to be overridden?When would you use a
TreeSetorTreeMapin an application?🏁 Deliverables
Your submission must include:
✅ Completed
Customer.javawith overriddenequals,hashCode, andtoString✅ Fully working
RewardsManager.java:Safe customer registration
Validation using
SetPoint management using
MapStream filters
removeIf,replaceAll,computeIfPresent✅ Test logic in
Main.javaSubmit screenshots of:
Output before and after
removeIf()Output of sorted map
showPremiumCustomers()Beta Was this translation helpful? Give feedback.
All reactions