-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.java
More file actions
137 lines (130 loc) · 4.63 KB
/
Vehicle.java
File metadata and controls
137 lines (130 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Class to save all the vehicle details
* This class contains all the vehicle attributes required for this application
* This is the data access layer and main logic for this application is written here
*/
public class Vehicle {
protected String Vehicle_id;
protected int Year;
protected String Make;
protected String Model;
protected int vehicleStatus;
protected VehicleType vehicleType;
protected RentalRecord records[]= new RentalRecord[10];
// Constructor to accept the details of a vehicle
Vehicle(String VehicleId,int Year,String Make,String Model,int status,VehicleType vehicleType){
this.Vehicle_id=VehicleId;
this.Year=Year;
this.Make=Make;
this.Model= Model;
this.vehicleStatus=status;
this.vehicleType=vehicleType;
}
/**
* Method to get vehicle ID
*/
public String getVehicleId(){
return this.Vehicle_id;
}
/**
* Used to rent either available car or available van
* @param customerId,rentDate,numOfRentDay accepts customeID, date of rent, no of renting days
* @return True or false as to vehicle is successfully rented or not
*/
public boolean rent(String customerId, DateTime rentDate, int numOfRentDay)
{
String typeOfVehicle;
if(this.Vehicle_id.contains("C_"))
typeOfVehicle="car";
else
typeOfVehicle="van";
if(this.vehicleStatus!=0 || numOfRentDay<this.vehicleType.canBeRentedForMinimumDays(rentDate,typeOfVehicle) || numOfRentDay>=14||numOfRentDay<2)
{
return false;
}
else if( typeOfVehicle.equals("van") )
{
if(this.vehicleStatus!=0 || this.vehicleType.IsUnderMaintenance(rentDate,typeOfVehicle,numOfRentDay) || numOfRentDay==0)
return false;
else
{
String rentId= this.Vehicle_id+"_"+customerId+"_"+rentDate. getEightDigitDate();
this.records[this.getLastElementIndex()+1]=new RentalRecord(rentId,rentDate,new DateTime(rentDate,numOfRentDay));
this.vehicleStatus=1;
return true;
}
}
else {
String rentId= this.Vehicle_id+"_"+customerId+"_"+rentDate. getEightDigitDate();
this.records[this.getLastElementIndex()+1]=new RentalRecord(rentId,rentDate,new DateTime(rentDate,numOfRentDay));
this.vehicleStatus=1;
return true;
}
}
/**
* sets the vehicle status to available after maintenance
* @return Returns true if returned else false
*/
public boolean performMaintenance()
{
if(this.vehicleStatus==1 || this.vehicleStatus==2)
return false;
this.vehicleStatus=2;
return true;
}
/**
* Method used to get details of vehicle
*/
public String toString()
{
String repository="";
if(this.Vehicle_id.contains("V_"))
{
repository=this.Vehicle_id+":"+String.valueOf(this.Year)+":"+this.Make+":"+this.Model+":15"+":";
}
if(this.Vehicle_id.contains("C_")) {
repository = this.Vehicle_id + ":" + String.valueOf(this.Year) + ":" + this.Make + ":" + this.Model + ":" + String.valueOf(this.vehicleType.getSeats("car")) + ":";
}
switch(this.vehicleStatus)
{
case 0:repository+="Available";
break;
case 1: repository+="Rented";
break;
case 2: repository+="Maintenance";
break;
}
return repository;
}
/**
* Method used to get details of car or van with their rental history
*/
public String getDetails()
{
String data = "Vehicle ID:\t"+this.Vehicle_id+"\n Year:\t "+String.valueOf(this.Year)+"\n Make:\t"+this.Make+"\n Model:\t"+this.Model+"\n Number of Seats:\t";
if(this.Vehicle_id.contains("C_"))
data+=String.valueOf(this.vehicleType.getSeats("car"))+"\n Status:\t";
else {
data += String.valueOf(this.vehicleType.getSeats("van")) + "\n Status:\t";
}
switch(this.vehicleStatus)
{
case 0:data+="Available";
break;
case 1: data+="Rented";
break;
case 2: data+="Maintenance";
break;
}
return data;
}
/**
* Method used to get last element index
*/
public int getLastElementIndex()
{
int index=0;
for(index=0;this.records[index]!=null;index++);
return index-1;
}
}