-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDealership.java
More file actions
72 lines (63 loc) · 2.13 KB
/
Copy pathDealership.java
File metadata and controls
72 lines (63 loc) · 2.13 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
import java.util.Scanner;
public class Dealership {
private Car[] cars;
public Dealership(Car[] cars) {
this.cars = new Car[cars.length];
for (int i = 0; i < cars.length; i++) {
this.cars[i] = new Car(cars[i]);
}
}
public void setCar(Car car, int index) {
this.cars[index] = new Car(car);
}
public Car getCar(int index) {
return new Car(this.cars[index]);
}
public void sell(int index) {
this.cars[index].drive();
this.cars[index] = null;
}
/** Task 2 - Re-write the search action.
* Function name: search
*
* @param make (String)
* @param budget (int)
* @return (int)
*
* Inside the function:
* 1. Loops through every element in the cars field.
* 2. Skips the run if the element is null.
* 3. If it finds a car the user can afford:
* • println: \nWe found a car in spot <i> \n\n <toString>
* • print: If you're interested, type 'yes':
* • returns the index
* 4. If the loop runs and it didn't find a car
* • println: \nYour search didn't match any results.\n
* • returns 404
*/
public int search(String make, int budget) {
for (int i = 0; i < this.cars.length; i++) {
if (this.cars[i] == null) {
continue;
} else if (this.cars[i].getMake().equalsIgnoreCase(make) && this.cars[i].getPrice() <= budget) {
System.out.println("\nWe found a car in spot " + i + "\n\n" + this.cars[i].toString());
System.out.print("If you're interested, type 'yes': ");
return i;
}
}
System.out.println("\nYour search didn't match any results.\n");
return 404;
}
public String toString() {
String temp = "";
for (int i = 0; i < this.cars.length; i++) {
temp += "Parking Spot: " + i + "\n";
if (this.cars[i] == null) {
temp += "Empty\n";
} else {
temp += this.cars[i].toString() + "\n";
}
}
return temp;
}
}