By using Java: Design a Customer class to handle a customer loyalty marketing ca
ID: 3733221 • Letter: B
Question
By using Java:
Design a Customer class to handle a customer loyalty marketing campaign. After accumulating $100 in purchases, the customer receives a $10 discount on the next purchase.
Record customer’s name and id.
Provide constructors, mutators, accessors, and methods:
- void makePurchase (double amount).
- boolean discountReached ().
-string toString ().
- boolean equals ().
Provide a demo program and test a scenario in which a costomer has earned a discount and then made over $90, but less than $100 in purchases. This should not result in a second discount. Then add another purchases that results in the second discount.
Explanation / Answer
public class Customer { private String name; private int id; private double amount; private boolean discount; public Customer(String name, int id) { this.name = name; this.id = id; this.amount = 0; discount = false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public void makePurchase (double amount) { if(this.discount) { amount -= 10; this.discount = false; } this.amount += amount; if(this.amount > 100) { this.amount -= 100; this.discount = true; } } public boolean discountReached() { return discount; } @Override public String toString() { return "Customer{" + "name='" + name + ''' + ", id=" + id + ", amount=" + amount + ", discount=" + discount + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Customer customer = (Customer) o; if (id != customer.id) return false; if (Double.compare(customer.amount, amount) != 0) return false; if (discount != customer.discount) return false; return name != null ? name.equals(customer.name) : customer.name == null; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.