-
Notifications
You must be signed in to change notification settings - Fork 9
/
Car.java
192 lines (190 loc) · 4.78 KB
/
Car.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//*******************************************************
// Class Car.java
// The class represent car
// Author: liron mizrahi
//*******************************************************
public class Car
{
private int _id;
private char _type;
private String _brand;
private boolean _isManual;
// Constant in the class
private static final int DEFAULT_ID = 9999999;
private static final int ID_VALID_LENGTH = 7;
/**
* Constructor for class Car
* @param id of the car, type of the car, brand of the car, and isManual
* @return None
*/
public Car(int id, char type, String brand, boolean isManual)
{
// Checking if id is valid
int lengthOfId = String.valueOf(id).length();
if(lengthOfId == ID_VALID_LENGTH && id > 0)
{
this._id = id;
}
else
{
this._id = DEFAULT_ID;
}
// Checking if type is valid
if(type == 'A' || type == 'B' || type == 'C' || type == 'D')
{
this._type = type;
}
else
{
this._type = 'A';
}
_brand = brand;
_isManual = isManual;
}// end of Car method
/**
* Constructor for class Car
* @param id of the car, type of the car, brand of the car, and isManual
* @return None
*/
public Car(Car other)
{
this._id = other._id;
this._type = other._type;
this._brand = other._brand;
this._isManual = other._isManual;
}// end of Car method
/**
* Method return the id of the car
* @param None
* @return the id of the car
*/
public int getId()
{
return this._id;
}// end of getId method
/**
* Method return the type of the car
* @param None
* @return the type of the car
*/
public char getType()
{
return this._type;
}// end of getType method
/**
* Method return the brand of the car
* @param None
* @return the brand of the car
*/
public String getBrand()
{
return this._brand;
}// end of getBrand method
/**
* Method return if the car is manual
* @param None
* @return if the car is manual
*/
public boolean isManual()
{
return this._isManual;
}// end of isManual method
/**
* Method set the car id
* @param id of the car
* @return None
*/
public void setId(int id)
{
int lengthOfId = String.valueOf(id).length();
if(lengthOfId == ID_VALID_LENGTH && id > 0)
{
this._id = id;
}
}// end of setId method
/**
* Method set the car type
* @param type of the car
* @return None
*/
public void setType(char type)
{
// Checking if type is valid
if(type == 'A' || type == 'B' || type == 'C' || type == 'D')
{
this._type = type;
}
}// end of setType method
/**
* Method set the car brand
* @param brand of the car
* @return None
*/
public void setBrand(String brand)
{
this._brand = brand;
}// end of setBrand method
/**
* Method set if the car is manual
* @param manual
* @return None
*/
public void setIsManual(boolean manual)
{
this._isManual = manual;
}// end of setIsManual method
/**
* Method returns the car data as a string
* @param None
* @return string
*/
public String toString()
{
if (this._isManual)
{
return "id:" + this._id + " type:" + this._type + " brand:" + this._brand + " gear:manual";
}
return "id:" + this._id + " type:" + this._type + " brand:" + this._brand + " gear:auto";
}// end of toString method
/**
* Check if two cars are the same
* Cars are considered the same if they have the same type, brand and gear
* @param other the car to compare this car to
* @return true if the cars are the same, otherwise false
*/
public boolean equals (Car other)
{
return this._type == other._type && this._brand.equals(other._brand) && this._isManual == other._isManual;
}// end of equals method
/**
* Check if this car is better than the other car
* A car is considered better than another car if its type is higher.
* If both cars have same type, an automated car is better than a manual car.
* @param other the car to compare this car to
* @return true if the car is better than the other car, otherwise false
*/
public boolean better (Car other)
{
if (this._type > other._type)
{
return true;
}
else if ( this._type == other._type)
{
if (this._isManual == false && other.isManual () == true)
{
return true;
}
}
return false;
}// end of better method
/**
* Check if this car is worse than the other car
* @param other the car to compare this car to
* @return true if the car is worse than the other car, otherwise false
*/
public boolean worse (Car other)
{
return (other.better(this));
}// end of worse method
}// end of class Car