Skip to content

Commit 8c4c449

Browse files
committed
Cab CustomerService program
1 parent 278b087 commit 8c4c449

File tree

4 files changed

+336
-0
lines changed

4 files changed

+336
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.lab.jan_01;
2+
3+
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.ObjectInputStream;
8+
import java.io.ObjectOutputStream;
9+
import java.io.Serializable;
10+
import java.util.ArrayList;
11+
12+
record Employee(Integer employeeId, String employeeName) implements Serializable
13+
{
14+
}
15+
16+
17+
public class ArrayListSerialization
18+
{
19+
public static void main(String[] args) throws IOException
20+
{
21+
ArrayList<Employee> listOfEmployees = new ArrayList<>();
22+
listOfEmployees.add(new Employee(111, "A"));
23+
listOfEmployees.add(new Employee(222, "B"));
24+
listOfEmployees.add(new Employee(333, "C"));
25+
listOfEmployees.add(new Employee(444, "D"));
26+
listOfEmployees.add(new Employee(555, "E"));
27+
28+
String filePath = "C:\\Users\\vikra\\OneDrive\\Desktop\\Lap-Programs\\Java_Problem_solving\\src\\com\\lab\\jan_01";
29+
//Serialization
30+
var fos = new FileOutputStream(filePath);
31+
var oos = new ObjectOutputStream(fos);
32+
33+
try(fos; oos)
34+
{
35+
oos.writeObject(listOfEmployees);
36+
System.out.println("Object data stored successfully");
37+
}
38+
catch(Exception e)
39+
{
40+
e.printStackTrace();
41+
}
42+
43+
//De-Serialization
44+
45+
var fin = new FileInputStream(filePath);
46+
var ois = new ObjectInputStream(fin);
47+
48+
try(fin; ois)
49+
{
50+
51+
ArrayList<Employee> empList = (ArrayList<Employee>) ois.readObject();
52+
empList.forEach(System.out::println);
53+
54+
}
55+
catch(Exception e)
56+
{
57+
e.printStackTrace();
58+
}
59+
60+
61+
}
62+
63+
}

src/com/lab/jan_03/CabCustomer.java

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.lab.jan_03;
2+
3+
public class CabCustomer
4+
{
5+
6+
private int custId;
7+
private String customerName;
8+
private String pickupLocation;
9+
private String dropLocation;
10+
private int distance;
11+
private String phone;
12+
13+
14+
public CabCustomer() {
15+
super();
16+
17+
}
18+
19+
20+
public CabCustomer(int custId, String customerName, String pickupLocation, String dropLocation, int distance,
21+
String phone) {
22+
super();
23+
this.custId = custId;
24+
this.customerName = customerName;
25+
this.pickupLocation = pickupLocation;
26+
this.dropLocation = dropLocation;
27+
this.distance = distance;
28+
this.phone = phone;
29+
}
30+
31+
32+
public int getCustId() {
33+
return custId;
34+
}
35+
36+
37+
public void setCustId(int custId) {
38+
this.custId = custId;
39+
}
40+
41+
42+
public String getCustomerName() {
43+
return customerName;
44+
}
45+
46+
47+
public void setCustomerName(String customerName) {
48+
this.customerName = customerName;
49+
}
50+
51+
52+
public String getPickupLocation() {
53+
return pickupLocation;
54+
}
55+
56+
57+
public void setPickupLocation(String pickupLocation) {
58+
this.pickupLocation = pickupLocation;
59+
}
60+
61+
62+
public String getDropLocation() {
63+
return dropLocation;
64+
}
65+
66+
67+
public void setDropLocation(String dropLocation) {
68+
this.dropLocation = dropLocation;
69+
}
70+
71+
72+
public int getDistance() {
73+
return distance;
74+
}
75+
76+
77+
public void setDistance(int distance) {
78+
this.distance = distance;
79+
}
80+
81+
82+
public String getPhone() {
83+
return phone;
84+
}
85+
86+
87+
public void setPhone(String phone) {
88+
this.phone = phone;
89+
}
90+
91+
92+
93+
94+
}
95+
/*
96+
Three classes are given to you,
97+
CabCustomer
98+
CabCustomerService
99+
CabCustomerServiceTester
100+
101+
102+
Define the following in the class CabCustomer
103+
private : custId int, customerName String, pickupLocation String,dropLocation String, distance int,phone String
104+
Generate Getter/Setter for the all fields.
105+
Implement the No Argument constructor.
106+
Implement/Generate the parameterized constructor in the order as defined above, i.e., custId,Name,Pickup,Drop,Distance,Phone
107+
108+
Define the following in the class CabCustomerService and write logics in the following methods:
109+
private : Generic ArrayList to represent list of CabCustomers.
110+
public : addCabCustomer() : Add the customer object parameter to the ArrayList
111+
isFirstCustomer(): Check whether the customer object parameter is already existing in arrayList.
112+
113+
Note : If phone number of a customer matches with any of the phone numbers of the array list, then consider it as a existing customer, otherwise consider the customer as new customer.
114+
115+
calculateBill() should calculate and return the customer bill based on following rules
116+
1) if the customer is new return 0;
117+
2) if the customer's travel distance is below or equal
118+
to 4 kms then return 80 (Rs).
119+
3) if the customer travel distance is above 4 kms calculate bill 80 + 6 per each km.
120+
Ex: Any distance for new customer return 0;
121+
distance 4 return 80
122+
distance 6 return 80 + 6 * 6;
123+
printBill() which should return the bill of the customer object parameter in the following format:
124+
125+
output : JOHN Please pay your bill of Rs.0.0
126+
SMITH Please pay your bill of Rs.180.0
127+
128+
Note :
129+
Assume one customer books only one cab at a time.
130+
No charge for customer booking the cab for the first time.
131+
Customer's phone number is key to test a new customer or old customer.
132+
Distance should be treated as kilometers
133+
134+
A CabCustomerServiceTester is given with main() where you can create various objects and test them.
135+
*/
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.lab.jan_03;
2+
3+
import java.util.ArrayList;
4+
5+
public class CabCustomerService
6+
{
7+
8+
private ArrayList<CabCustomer> CabCustomers;
9+
10+
public CabCustomerService() {
11+
super();
12+
CabCustomers = new ArrayList<CabCustomer>();
13+
}
14+
15+
16+
17+
public void addCabCustomer(CabCustomer cbc)
18+
{
19+
CabCustomers.add(cbc);
20+
}
21+
22+
public boolean isFirstCustomer(CabCustomer cbc)
23+
{
24+
int count=0;
25+
for(CabCustomer cc : CabCustomers)
26+
{
27+
if(cc.getPhone().equals(cbc.getPhone()))
28+
count++;
29+
}
30+
return count==1;
31+
}
32+
33+
public double calculateBill(CabCustomer cbc)
34+
{
35+
if(isFirstCustomer(cbc)==true)
36+
return 0.0;
37+
if(cbc.getDistance()<4)
38+
return 80;
39+
if(cbc.getDistance()>4)
40+
return 80 + (cbc.getDistance()-4)*6;
41+
return 0.0;
42+
}
43+
44+
public void printBill(CabCustomer cbc)
45+
{
46+
System.out.println(cbc.getCustomerName()+" Please pay your bill of Rs."+calculateBill(cbc));
47+
}
48+
}
49+
/*
50+
51+
Define the following in the
52+
class CabCustomerService and write logics in the following methods:
53+
private : Generic ArrayList to represent list of CabCustomers.
54+
public : addCabCustomer() : Add the customer object parameter to the ArrayList
55+
isFirstCustomer(): Check whether the customer object parameter is already existing in arrayList.
56+
57+
58+
Note : If phone number of a customer matches with any of the phone
59+
numbers of the array list, then consider it as a existing customer,
60+
otherwise consider the customer as new customer.
61+
62+
calculateBill() should calculate and return the customer
63+
bill based on following rules
64+
1) if the customer is new return 0;
65+
2) if the customer's travel distance is below or equal
66+
to 4 kms then return 80 (Rs).
67+
3) if the customer travel distance is above 4 kms calculate bill 80 + 6 per each km.
68+
69+
70+
Ex: Any distance for new customer return 0;
71+
distance 4 return 80
72+
distance 6 return 80 + 6 * 6;
73+
printBill() which should return the bill of
74+
the customer object parameter in the following format:
75+
76+
output : JOHN Please pay your bill of Rs.0.0
77+
SMITH Please pay your bill of Rs.180.0
78+
79+
Note :
80+
Assume one customer books only one cab at a time.
81+
No charge for customer booking the cab for the first time.
82+
Customer's phone number is key to test a new customer or old customer.
83+
Distance should be treated as kilometers
84+
85+
86+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.lab.jan_03;
2+
3+
public class CabCustomerServiceMain
4+
{
5+
6+
public static void main(String[] args)
7+
{
8+
CabCustomer cbc1 = new CabCustomer(111, "Vijay", "Ameerpet", "Begampet", 3, "98745 32728");
9+
CabCustomer cbc2 = new CabCustomer(111, "Harun", "Ameerpet", "Aurangabad", 578, "81745 32428");
10+
11+
CabCustomerService cbcs = new CabCustomerService();
12+
cbcs.addCabCustomer(cbc1);
13+
cbcs.addCabCustomer(cbc2);
14+
cbcs.printBill(cbc1);
15+
cbcs.printBill(cbc2);
16+
17+
System.out.println();
18+
CabCustomer cbc3 = new CabCustomer(111, "Harun", "Ameerpet", "Aurangabad", 10, "81745 32428");
19+
cbcs.addCabCustomer(cbc3);
20+
cbcs.printBill(cbc3);
21+
22+
}
23+
24+
}
25+
/*
26+
27+
Note : If phone number of a customer matches with any of the phone
28+
numbers of the array list, then consider it as a existing customer,
29+
otherwise consider the customer as new customer.
30+
31+
calculateBill() should calculate and return the customer
32+
bill based on following rules
33+
1) if the customer is new return 0;
34+
2) if the customer's travel distance is below or equal
35+
to 4 kms then return 80 (Rs).
36+
3) if the customer travel distance is above 4 kms calculate bill 80 + 6 per each km.
37+
Ex: Any distance for new customer return 0;
38+
distance 4 return 80
39+
distance 6 return 80 + 6 * 6;
40+
printBill() which should return the bill of the customer object parameter in the following format:
41+
42+
output : JOHN Please pay your bill of Rs.0.0
43+
SMITH Please pay your bill of Rs.180.0
44+
45+
Note :
46+
Assume one customer books only one cab at a time.
47+
No charge for customer booking the cab for the first time.
48+
Customer's phone number is key to test a new customer or old customer.
49+
Distance should be treated as kilometers
50+
51+
A CabCustomerServiceTester is given with main() where you can create various objects and test them.
52+
*/

0 commit comments

Comments
 (0)