JAVA The source code below defines a Customer class. Examines the code and answe
ID: 3875286 • Letter: J
Question
JAVA
The source code below defines a Customer class. Examines the code and answer the questions below;
public class Customer extends Person implements Comparable<Customer>{
private float totalSpent;
public Customer(String id, String name, float totalSpent){
super(id, name);
this.totalSpent = totalSpent;
}
public int compareTo(Customer customer) {
int x = this.name.compareTo(customer.name);
if (x == 0){
return 0;
}else if(x < 0){
return 1;
}else{
return -1;
}
}
public float getExpenditure(){
return totalSpent;
}
}
Using ArrayList, write a simple program to create five customer objects. The details of the objects should be entered through a console by prompting the user. (5 marks)
The current implementation of the compareTo() method enables the program to order customers’ details in descending order, based on their names. Rewrite the method to enable the program to order the details in ascending order, based on the total amount a customer spend.
Write a method that calculates a discount of 20% from the amount of the customer who spent most.
Explanation / Answer
Person Class
public class Person {
protected String id ;
protected String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Customer Class
public class Customer extends Person implements Comparable<Customer> {
private float totalSpent;
public Customer(String id, String name, float totalSpent) {
super(id, name);
this.totalSpent = totalSpent;
}
public int compareTo(Customer customer) {
int x = this.name.compareTo(customer.name);
if (x == 0) {
return 0;
} else if (x > 0) {
return 1;
} else {
return -1;
}
}
public float getExpenditure() {
return totalSpent;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", totalSpent=" + totalSpent + "]";
}
}
//Test Class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class CustomerTester {
public static void main(String[] args) {
//Declaring a Customer ArrayList
ArrayList<Customer> customerList=new ArrayList<Customer>();
InputStreamReader ir=new InputStreamReader(System.in); //To read from console.
BufferedReader br=new BufferedReader(ir);
try {
//Looping 5 times to read customer objects
for(int i=1;i<=5;i++)
{
System.out.println("Please enter the details of Customer "+ i);
System.out.println("Id : ");
String id=br.readLine();
System.out.println("Name : ");
String name=br.readLine();
System.out.println("Total Spend : ");
float totalSpent=Float.parseFloat(br.readLine()); //Parsing from String to Float (br.readLine() gives a String)
//Creating a new Customer Object
Customer customer=new Customer(id, name, totalSpent);
//Adding customer object to customerList
customerList.add(customer);
}
//Sorting based on comparator
Collections.sort(customerList);
System.out.println("Customer Details are :");
//Using foreach loop to iterate and print customer details
for (Customer customer : customerList) {
System.out.println(customer.toString());
//toString method is created in Customer Class , It is used to print object as a whole.
}
//For 20 % case.
//First Change the compareTo method to sort based on amount spend .Now data is sorted so last customer spend most.
System.out.println("20 % Discount is " +getDiscount(customerList.get(4)));
} catch (IOException e) {
//Exception, if there is some problem in reading the input data
}
}
public static double getDiscount(Customer c)
{
return 0.02*c.getExpenditure();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.