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

Assignment 2 Solution

The document provides code for a salon service application with classes for Customer, PremiumCustomer and Visit to track customer visits and charges, with PremiumCustomer getting a 20% discount on services and able to view visit history; it includes a test class to demonstrate creating customers, adding visits for a premium customer, and printing bills with and without discounts.

Uploaded by

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

Assignment 2 Solution

The document provides code for a salon service application with classes for Customer, PremiumCustomer and Visit to track customer visits and charges, with PremiumCustomer getting a 20% discount on services and able to view visit history; it includes a test class to demonstrate creating customers, adding visits for a premium customer, and printing bills with and without discounts.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE

UW
Assigned Date: 30-10 – 19
ASSIGNMENT 2

Discipline/Semester: BSCS 3RD


Course Code: CS-212 Due Date: 08- 11 - 19
Course : Advanced Object Oriented Programming
Total Marks: 10
Note: Plagiarized assignments will be marked zero. Continuous plagiarism will result in negative
marking.

You need to write code for a Salon Service. For better customer management the Salon has divided
the customers into two classes; Customer and PremiumCustomer. The track of ordinary customers in
not maintained whereas the premium customers have a maintained history. Premium customer are
also able to get a discount of 20% in each purchase.

Code for class Visit.

/** @author Aslam */

public class Visit {


Date date;
double serviceCharges;
double productCharges;

Visit(Date date,double serviceCharges,double productCharges){


setDate(date);
setServiceCharges( serviceCharges);
setProductCharges(productCharges);
}

public String toString(){


LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();

return "Date:"+day+"\\"+month+"\\"+year+
"\nService Charges: "+serviceCharges+"\nProduct Charges: "+productCharges;
}

void setDate(Date date){


this.date=date;}
void setServiceCharges(double serviceCharges){
this.serviceCharges=serviceCharges;}
void setProductCharges(double productCharges){
this.productCharges=productCharges;}
Date getDate(){return date;}
double getServiceCharges(){return serviceCharges;}
double getProductCharges(){return productCharges;}

Code for class Customer.

/**@author Aslam*/

public class Customer {


private String name;
protected ArrayList<Visit> visit=new ArrayList<>();

Customer(String name,Visit v){


setName(name);
visit.add(v);
}

double getTotalExpense(){
return((visit.get(0).serviceCharges)+(visit.get(0).productCharges));
}

public String toString(){


String s=visit.get(visit.size()-1).toString().replace('[',' ');s=s.replace(']',' ');s=s.trim();
return "Customer Name:"+this.name+"\nMemberShip:"+this.getClass().getSimpleName()+"\tNo of
visit:"+visit.size()
+"\n"+s ;
}
public void printBill(){
System.out.println(this);
System.out.println("Total:"+getTotalExpense()+"\n----------------------------------");
}

public void setName(String name){


this.name=name;
}

public String getName(){


return name;
}

Code for class PremiumCustomer.

public interface Discountable {


static final double premiumDiscountRate=0.20;
double discount(double amount);
}
/**@author Aslam */

public class PremiumCustomer extends Customer implements Discountable {


double discountRate=Discountable.premiumDiscountRate;

PremiumCustomer(String name,Visit v){


super(name,v);
}

public void addVisit(Date d1,double serviceCharges,double productCharges){


super.visit.add(new Visit(d1,serviceCharges,productCharges));
}

public void getCustomerHistory(){


int k=1;
for(Visit i : super.visit){

System.out.println("\nVisit No:"+k);
System.out.println(i.toString());
k++;
}
// System.out.println("No of visits: "+getTotalNoOfVisit());
}

public int getTotalNoOfVisit(){


return super.visit.size();
}
public void printBill(){
System.out.println(super.toString());
System.out.println("\nTotal:"+getTotalExpense()+" with "+ discountRate*100+"%
discount\n----------------------------------");
}
public Customer convertCustomerToPremium(Customer c){
c=new PremiumCustomer(c.getName(),c.visit.get(0));
return c;
}

double getTotalExpense(){
int lastVisit=visit.size()-1;
System.out.println("sc"+super.visit.get(lastVisit).serviceCharges+"pc"+super.visit.get(lastVisit).pr
oductCharges+"v"+(double)getTotalNoOfVisit()/100);
double total= (super.visit.get(lastVisit).serviceCharges+super.visit.get(lastVisit).productCharges) ;
return discount(total);
}

public void setDiscountRate(double discountRate){


this.discountRate=discountRate;
}
public double getDiscountRate(){
return discountRate;}

@Override
public double discount(double amount) {
return amount-amount*discountRate;
}
}

Write code for the testClass

/**@author Aslam */

public class Assignment2 {

public static void main(String[] args) {


// TODO code application logic here
Date d1=new Date("11/10/2011");
Visit v1c1=new Visit(d1,200.0,300.0);
Customer c1=new Customer("Farrah",v1c1);
c1.printBill();

Date d2=new Date("10/21/2019");


Visit v1c2=new Visit(d2,500.0,500.0);
PremiumCustomer p1=new PremiumCustomer("Aslam",v1c2);
System.out.println("BILL");
p1.printBill();

p1.addVisit(d1,400,200);

System.out.println("BILL");
p1.printBill();
System.out.println("CUSTOMER HISTORY");
p1.getCustomerHistory();

}
}

Output :

You might also like