-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsedCarDBTest_1Perrott.java
132 lines (112 loc) · 4.07 KB
/
UsedCarDBTest_1Perrott.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
import java.util.ArrayList;
public class UsedCarDBTest_1Perrott {
public static void main(String[] args) {
UsedCarDB db = new UsedCarDB();
db.addCarListing(new UsedCar("SUV", "fair", 12999.99));
db.addCarListing(new UsedCar("SUV", "good", 13000));
db.addCarListing(new UsedCar("SUV", "good", 14000));
db.addCarListing(new UsedCar("SUV", "fair", 19000));
db.addCarListing(new UsedCar("sedan", "fair", 7000));
db.addCarListing(new UsedCar("sedan", "fair", 9000));
db.addCarListing(new UsedCar("sedan", "good", 8000));
db.addCarListing(new UsedCar("sedan", "good", 11000));
db.addCarListing(new UsedCar("sedan", "excellent", 10000));
db.addCarListing(new UsedCar("sedan", "good", 14000));
db.addCarListing(new UsedCar("sedan", "excellent", 21000));
db.addCarListing(new UsedCar("sedan", "excellent", 20000));
db.addCarListing(new UsedCar("convertible", "fair", 13000));
db.addCarListing(new UsedCar("coupe", "excellent", 80000));
db.print();
System.out.println(db.cheapest("SUV", "good"));
System.out.println(db.cheapest("sedan", "excellent"));
System.out.println(db.choices(14000, 22000, "SUV", "good"));
System.out.println(db.choices(8000, 12000, "sedan", "good"));
System.out.println(db.choices(8000, 20000, "sedan", "excellent"));
}
}
class UsedCarDB
{
ArrayList<UsedCar> carList; // contains UsedCars in increasing order of price
public UsedCarDB() {
carList = new ArrayList<>();
}
/**
Adds car to carList maintaining the nondecreasing order of price
*/
public void addCarListing(UsedCar car) {
int i = 0;
if (carList.size() != 0) {
while (i < carList.size() && carList.get(i).price() <= car.price()) {
i++;
}
}
carList.add(i, car);
}
/*
public void addCarListing(UsedCar car) {
carList.add(car);
int mindex = 1;
for (int i = 0; i < carList.size() - 1; i++) {
mindex = i;
for (int j = i + 1; j < carList.size(); j++)
if (carList.get(j).price() < carList.get(mindex).price()) mindex = j;
carList.set(i, carList.set(mindex, carList.get(i)));
}
}*/
/**
@return the car listing that is the cheapest of those with matching
type and condition or null if no cars with given type & cond
*/
public UsedCar cheapest(String type, String condition) {
for (UsedCar car : carList) {
if (car.carType().equals(type) && car.condition().equals(condition)) return car;
}
return null;
}
/**
@return list of all cars with the given condition and type that are priced
within the range [low, high]
*/
public ArrayList choices(double low, double high, String type, String condition) {
ArrayList<UsedCar> options = new ArrayList<>();
int lowIndex = 0;
while (carList.get(lowIndex).price() < low) {
lowIndex++;
}
int highIndex = carList.size() - 1;
while (carList.get(highIndex).price() > high) {
highIndex--;
}
for (int i = lowIndex; i <= highIndex; i++) {
if (carList.get(i).carType().equals(type) && carList.get(i).condition().equals(condition)) options.add(carList.get(i));
}
return options;
}
public void print() {
for(int index = 0; index < carList.size(); index++)
System.out.println(carList.get(index));
System.out.println();
}
}
class UsedCar {
private String myType;
private String myCondition;
private double myPrice;
public UsedCar(String type, String condition, double price) {
myType = type;
myCondition = condition;
myPrice = price;
}
public String carType() {
return myType; // SUV, Sedan, Coupe, or Convertible
}
public String condition() {
return myCondition; // fair, good, or excellent
}
public double price() {
return myPrice;
}
public String toString() {
return carType() + " " + condition() + " " + price();
}
}