Professional Documents
Culture Documents
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)
}
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)
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)
General Class:
package Mid;
Department of Computer Science CSC-210:Object-Oriented Programming
Bahria University Karachi Campus Semester 02 (SPRING 2021)
{
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)
System.out.println("**********GENERAL OUTPUTS**********");
for(int i = 0; i < 4; i++)
{
System.out.println(GeneralData[i]);
}
}
}