You are on page 1of 7

Department of Computer Science CSC-210:Object-Oriented Programming

Bahria University Karachi Campus Semester 02 (SPRING 2021)

ASSIGNMENT 03
Solution
Marks: 5

Q1. Write down a java program for a Super Market which stores the details of their daily
customer.
A required system will be able to store name, address, phone number and bill amount for each
customer. We have 2 special categories of customers in supermarket. Gold customer and silver
customer. Criteria for special category customer is as follows:
 If customer billing amount is of more than or equal to 10000 then it is a silver customer.
 If customer billing amount is of more than or equal to 20000 then it is a Gold customer.
 If a customer is not from the gold and silver categories, then customer will be
considered as general customer.
Bill generation for these special type of customers is based on points of loyalty card and fixed
discount value.
 Discount is 15% for Gold customers and 8% for Silver customers as fixed values.
 If a customer have more than 100 points in their loyalty cards then provide 5% more
discounts.
 If a customer have more than 200 points in their loyalty cards then provide 8% more
discount.
Select appropriate access modifiers for the variables of customer class as subclasses cannot
access base class variables directly and write accessor and mutator methods where
necessary. Create default and parameterized constructors for customer class. Each child class
will have their appropriate constructors.
a) Make a UML class diagram for above scenario with proper notations [Marks 0.5]
b) Write a code for each class mentioning their attributes and methods. [Marks 2]
c) Override getBill method that returns the bill_amount based on discount and points
criteria. [Marks 1]
d) Write an updatePoints method which is used to update points by subtracting the used
points during bill computation.
[Marks 0.5]
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)

e) Override toString method for each class to display the type of customer and other
relevant information. [Marks 1]
f) Create a test class where you write a menu driven program to insert data for 10
customers. Use object array for creating the objects (with polymorphic reference). Insert data
for 10 customers (Insert data for all 3 categories of customer. General, Gold and Silver) and
display details of each customer using appropriate method.
Source Code:

Customer Class:
package Mid;
public class Customer
{
private String name, address, phoneno;
private double billamount;
public Customer(String name, String address, String phoneno, double
billamount)
{
this.name = name;
this.address = address;
this.phoneno = phoneno;
this.billamount = billamount;
}
public Customer()
{
name = "";
address = "";
phoneno = "";
billamount = 0;
}
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)

public double getBillamount()


{
return billamount;
}
public void setBillamount(double billamount)
{
this.billamount = billamount;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getPhoneno()
{
return phoneno;
}
}
Gold Class:
package Mid;
class Gold extends Customer
{
double discount = 0, bill_amount;
int points, updatedpoints;
Gold(String name, String address, String phoneno, double billamount)
{
super(name, address, phoneno, billamount);
}

public double getBillamount()


{
if(100 > 200)
{
discount = super.getBillamount() * 20 / 100;
}
else
{
discount = super.getBillamount() * 23 / 100;
}
bill_amount = super.getBillamount()- discount;
setBillamount(bill_amount);
return super.getBillamount(); Zoha Sabih(BSIT-2A) 02-235192-014

}
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)

public int getPoints()


{
points = (int) (super.getBillamount() / 150);
return points;
}

public int updatePoints()


{
if(points > 200)
{
updatedpoints = getPoints() - 200;
}
else
{
updatedpoints = getPoints() - 100;
}
return updatedpoints;
}
public String toString()
{
return "\t************ GOLD CUSTOMER ************\nName: " +
getName() + "\nAddress: " + getAddress() + "\nPhone
No.: " + getPhoneno() + "\nCalculated Bill: " + super.getBillamount() +
"\nYour loyalty points are: " + getPoints() + "\nCongratualtions! On the
purchase of " + super.getBillamount() + " you have recieved a discount!" +
"\nUpdated
Points: " + updatePoints() + "\nTotal Bill: " + getBillamount();
}
}

Silver Class:
package Mid;
class Silver extends Customer
{
double discount, bill_amount;
int points, updatedpoints;
Silver(String name, String address, String phoneno, double billamount)
{
super(name, address, phoneno, billamount);
}

@Override
public double getBillamount()
{
if(getPoints() > 200)
{
discount = super.getBillamount() * 16 / 100;
}
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)

else if(getPoints() > 100)


{
discount = super.getBillamount() * 13 / 100;
}
else
{ discount = super.getBillamount() * 8 / 100;
}
bill_amount = super.getBillamount()- discount;
setBillamount(bill_amount);
return super.getBillamount();
}

public int getPoints()


{
points = (int) (super.getBillamount() / 150);
return points;
}

public int updatePoints()


{
if(points > 200)
{
updatedpoints = getPoints() - 200;
}
else if(points > 100)
{
updatedpoints = getPoints() - 100;
}
else
{
updatedpoints = points;
}
return updatedpoints;
}
public String toString()
{
return "\t************ SILVER CUSTOMER ************\nName: " +
getName() + "\nAddress: "
+ getAddress() + "\nPhone No.: " + getPhoneno() + "\nCalculated Bill: "
+ super.getBillamount() + "\nYour loyalty points are: " + getPoints() +
"\nCongratualtions! On the purchase of " + super.getBillamount() + "
you have recieved a discount!" + "\nUpdated Points: " + updatePoints()
+ "\nTotal Bill: " + getBillamount();
}
}

General Class:
package Mid;
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)

class General extends Customer

{
General(String name, String address, String phoneno, double
billamount)
{
super(name, address, phoneno,billamount);
}

@Override
public String toString()
{
return "\t************ GENERAL CUSTOMER ************\nName: " +
getName() + "\nAddress: " + getAddress() + "\nPhone No.: " +
getPhoneno() + "\nTotal Bill: " + getBillamount() + "\nThank you for
Shopping!";
}
}
Application Class:
package Mid;
import java.util.Scanner;
public class Application
{
public static void main(String[] args)
{
String name, address, phoneno;
double billamount;
int count = 1;
Scanner input = new Scanner(System.in);
Customer GoldData[] = new Gold[3];
Customer SilverData[] = new Silver[3];
Customer GeneralData[] = new General[4];
GoldData[0] = new Gold("Ahmed", "Johar, Block 18",
"03318397063", 48237);
GoldData[1] = new Gold("Zahid Ahmed", "North Nazimabad",
"0333819101", 76005);
GoldData[2] = new Gold("Ali Waqar", "Johar, Block 16",
"0302877372", 20500);
SilverData[0] = new Silver("sara Rehman", "Sharah-e-Faisal",
"036379929", 17822);
SilverData[1] = new Silver("Waleed Akbar", "Malir",
"090078601", 12300);
SilverData[2] = new Silver("Sarim", "Malir", "0331739021",
11761);
GeneralData[0] = new General("Suleman", "Nazimabad",
"031273782", 721);
GeneralData[1] = new General("Hasan khan", "North Karachi",
"0322761661", 50);
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)

GeneralData[2] = new General("Lamiya Zohair", "Clifton",


"0333898972", 9870);
GeneralData[3] = new General("Mahnoor Mahboob", "DHA",
"03007010943", 4010);
System.out.println("**********GOLD OUTPUTS**********");
for(int i = 0; i < 3; i++)
{
System.out.println(GoldData[i]);
}
System.out.println("**********SILVER OUTPUTS**********");
for(int i = 0; i < 3; i++)
{
System.out.println(SilverData[i]);
} Zoha Sabih(BSIT-2A) 02-235192-014

System.out.println("**********GENERAL OUTPUTS**********");
for(int i = 0; i < 4; i++)
{
System.out.println(GeneralData[i]);
}
}
}

You might also like