Skip to content

Commit 7454dbf

Browse files
author
amigoscode
committed
switch from arrays to lists
1 parent 09fc1e8 commit 7454dbf

9 files changed

Lines changed: 69 additions & 141 deletions

File tree

src/com/amigoscode/Main.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.amigoscode.user.UserFileDataAccessService;
1212
import com.amigoscode.user.UserService;
1313

14+
import java.util.List;
1415
import java.util.Scanner;
1516
import java.util.UUID;
1617

@@ -52,8 +53,8 @@ public static void main(String[] args) {
5253
}
5354

5455
private static void allBookings(CarBookingService carBookingService) {
55-
CarBooking[] bookings = carBookingService.getBookings();
56-
if (bookings.length == 0) {
56+
List<CarBooking> bookings = carBookingService.getBookings();
57+
if (bookings.isEmpty()) {
5758
System.out.println("No bookings available 😕");
5859
return;
5960
}
@@ -63,8 +64,8 @@ private static void allBookings(CarBookingService carBookingService) {
6364
}
6465

6566
private static void displayAllUsers(UserService userService) {
66-
User[] users = userService.getUsers();
67-
if (users.length == 0) {
67+
List<User> users = userService.getUsers();
68+
if (users.isEmpty()) {
6869
System.out.println("❌ No users in the system");
6970
return;
7071
}
@@ -74,8 +75,8 @@ private static void displayAllUsers(UserService userService) {
7475
}
7576

7677
private static void displayAvailableCars(CarBookingService carBookingService, boolean isElectric) {
77-
Car[] availableCars = isElectric ? carBookingService.getAvailableElectricCars() : carBookingService.getAvailableCars();
78-
if (availableCars.length == 0) {
78+
List<Car> availableCars = isElectric ? carBookingService.getAvailableElectricCars() : carBookingService.getAvailableCars();
79+
if (availableCars.isEmpty()) {
7980
System.out.println("❌ No cars available for renting");
8081
return;
8182
}
@@ -98,8 +99,8 @@ private static void displayAllUserBookedCars(UserService userService,
9899
return;
99100
}
100101

101-
Car[] userBookedCars = carBookingService.getUserBookedCars(user.getId());
102-
if (userBookedCars.length == 0) {
102+
List<Car> userBookedCars = carBookingService.getUserBookedCars(user.getId());
103+
if (userBookedCars.isEmpty()) {
103104
System.out.printf("❌ user %s has no cars booked", user);
104105
return;
105106
}

src/com/amigoscode/booking/CarBookingDao.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
11
package com.amigoscode.booking;
22

3+
import java.sql.Array;
4+
import java.util.ArrayList;
5+
import java.util.List;
36
import java.util.UUID;
47

58
public class CarBookingDao {
69

7-
private final static CarBooking[] carBookings;
10+
private final static List<CarBooking> carBookings;
811

912
static {
10-
carBookings = new CarBooking[10];
13+
carBookings = new ArrayList<CarBooking>();
1114
}
1215

13-
public CarBooking[] getCarBookings() {
16+
public List<CarBooking> getCarBookings() {
1417
return carBookings;
1518
}
1619

1720
public void book(CarBooking carBooking) {
18-
int nextFreeIndex = -1;
19-
20-
for (int i = 0; i < carBookings.length; i++) {
21-
if (carBookings[i] == null) {
22-
nextFreeIndex = i;
23-
}
24-
}
25-
26-
if (nextFreeIndex > -1) {
27-
carBookings[nextFreeIndex] = carBooking;
28-
return;
29-
}
30-
31-
// full array
32-
// copy all bookings to new array with bigger space
33-
CarBooking[] biggerCarBookings = new CarBooking[carBookings.length + 10];
34-
35-
for (int i = 0; i < carBookings.length; i++) {
36-
biggerCarBookings[i] = carBookings[i];
37-
}
38-
39-
// finally add new booking
40-
biggerCarBookings[carBookings.length] = carBooking;
21+
carBookings.add(carBooking);
4122

4223
}
4324

src/com/amigoscode/booking/CarBookingService.java

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import com.amigoscode.user.User;
66

77
import java.time.LocalDateTime;
8+
import java.util.ArrayList;
9+
import java.util.Collections;
10+
import java.util.List;
811
import java.util.UUID;
912

1013
public class CarBookingService {
@@ -18,9 +21,9 @@ public CarBookingService(CarBookingDao carBookingDao, CarService carService) {
1821
}
1922

2023
public UUID bookCar(User user, String regNumber) {
21-
Car[] availableCars = getAvailableCars();
24+
List<Car> availableCars = getAvailableCars();
2225

23-
if (availableCars.length == 0) {
26+
if (availableCars.isEmpty()) {
2427
throw new IllegalStateException("No car available for renting");
2528
}
2629

@@ -39,74 +42,43 @@ public UUID bookCar(User user, String regNumber) {
3942
throw new IllegalStateException("Already booked. car with regNumber " + regNumber);
4043
}
4144

42-
public Car[] getUserBookedCars(UUID userId) {
43-
CarBooking[] carBookings = carBookingDao.getCarBookings();
45+
public List<Car> getUserBookedCars(UUID userId) {
46+
List<CarBooking> carBookings = carBookingDao.getCarBookings();
47+
List<Car> userCars = new ArrayList<>();
4448

45-
int numberOfBookingsForUser = 0;
46-
47-
for (CarBooking cb : carBookings) {
48-
if (cb != null && cb.getUser().getId().equals(userId)) {
49-
++numberOfBookingsForUser;
50-
}
51-
}
52-
53-
if (numberOfBookingsForUser == 0) {
54-
return new Car[0];
55-
}
56-
57-
Car[] userCars = new Car[numberOfBookingsForUser];
58-
59-
int index = 0;
6049
for (CarBooking carBooking : carBookings) {
6150
if (carBooking != null && carBooking.getUser().getId().equals(userId)) {
62-
userCars[index++] = carBooking.getCar();
51+
userCars.add(carBooking.getCar());
6352
}
6453
}
6554
return userCars;
6655
}
6756

6857

69-
public Car[] getAvailableCars() {
58+
public List<Car> getAvailableCars() {
7059
return getCars(carService.getAllCars());
7160
}
7261

73-
public Car[] getAvailableElectricCars() {
62+
public List<Car> getAvailableElectricCars() {
7463
return getCars(carService.getAllElectricCars());
7564
}
7665

77-
private Car[] getCars(Car[] cars) {
66+
private List<Car> getCars(List<Car> cars) {
7867

7968
// no cars in the system yet
80-
if (cars.length == 0) {
81-
return new Car[0];
69+
if (cars.isEmpty()) {
70+
return Collections.emptyList();
8271
}
8372

84-
CarBooking[] carBookings = carBookingDao.getCarBookings();
73+
List<CarBooking> carBookings = carBookingDao.getCarBookings();
8574

8675
// no bookings yet therefore all cars are available
87-
if (carBookings.length == 0) {
76+
if (carBookings.isEmpty()) {
8877
return cars;
8978
}
9079

91-
// this variable is used to create new array for availableCars since we need the size
92-
int availableCarsCount = 0;
9380

94-
for (Car car : cars) {
95-
// lets check if car part of any booking. if not then its available
96-
boolean booked = false;
97-
for (CarBooking carBooking : carBookings) {
98-
if (carBooking == null || !carBooking.getCar().equals(car)) {
99-
continue;
100-
}
101-
booked = true;
102-
}
103-
if (!booked) {
104-
++availableCarsCount;
105-
}
106-
}
107-
108-
Car[] availableCars = new Car[availableCarsCount];
109-
int index = 0;
81+
List<Car> availableCars = new ArrayList<>();
11082

11183
// populate available cars
11284
for (Car car : cars) {
@@ -120,36 +92,15 @@ private Car[] getCars(Car[] cars) {
12092
booked = true;
12193
}
12294
if (!booked) {
123-
availableCars[index++] = car;
95+
availableCars.add(car);
12496
}
12597
}
12698

12799
return availableCars;
128100
}
129101

130-
public CarBooking[] getBookings() {
131-
CarBooking[] carBookings = carBookingDao.getCarBookings();
132-
133-
int numberOfBookings = 0;
134-
135-
for (CarBooking cb : carBookings) {
136-
if (cb != null) {
137-
++numberOfBookings;
138-
}
139-
}
140-
141-
if (numberOfBookings == 0) {
142-
return new CarBooking[0];
143-
}
144-
145-
CarBooking[] bookings = new CarBooking[numberOfBookings];
102+
public List<CarBooking> getBookings() {
103+
return carBookingDao.getCarBookings();
146104

147-
int index = 0;
148-
for (CarBooking carBooking : carBookings) {
149-
if (carBooking != null) {
150-
bookings[index++] = carBooking;
151-
}
152-
}
153-
return bookings;
154105
}
155106
}

src/com/amigoscode/car/CarDAO.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.amigoscode.car;
22

33
import java.math.BigDecimal;
4+
import java.util.Arrays;
5+
import java.util.List;
46

57
public class CarDAO {
68

7-
private static final Car[] CARS = {
9+
private static final List<Car> CARS = Arrays.asList(
810
new Car("1234", new BigDecimal("89.00"), Brand.TESLA, true),
911
new Car("5678", new BigDecimal("50.00"), Brand.AUDI, false),
10-
new Car("5678", new BigDecimal("77.00"), Brand.MERCEDES, false),
11-
};
12+
new Car("5678", new BigDecimal("77.00"), Brand.MERCEDES, false)
13+
);
1214

13-
public Car[] getAllCars() {
15+
public List<Car> getAllCars() {
1416
return CARS;
1517
}
1618
}

src/com/amigoscode/car/CarService.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.amigoscode.car;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
37
public class CarService {
48

59
private final CarDAO carDAO;
@@ -8,7 +12,7 @@ public CarService(CarDAO carDAO) {
812
this.carDAO = carDAO;
913
}
1014

11-
public Car[] getAllCars() {
15+
public List<Car> getAllCars() {
1216
return carDAO.getAllCars();
1317
}
1418

@@ -21,34 +25,22 @@ public Car getCar(String regNumber) {
2125
throw new IllegalStateException(String.format("Car with reg %s not found", regNumber));
2226
}
2327

24-
public Car[] getAllElectricCars() {
25-
int electricCarsCount = 0;
26-
27-
Car[] cars = getAllCars();
28+
public List<Car> getAllElectricCars() {
29+
List<Car> cars = getAllCars();
2830

29-
if (cars.length == 0) {
30-
return new Car[0];
31+
if (cars.size() == 0) {
32+
return Collections.emptyList();
3133
}
3234

35+
List<Car> electricCars = new ArrayList<>();
36+
3337
for (Car car : cars) {
3438
if (car.isElectric()) {
35-
electricCarsCount++;
39+
electricCars.add(car);
3640
}
3741
}
3842

39-
if (electricCarsCount == 0) {
40-
return new Car[0];
41-
}
42-
43-
Car[] electricCars = new Car[electricCarsCount];
44-
45-
int index = 0;
4643

47-
for (int i = 0; i < cars.length; i++) {
48-
if (cars[i].isElectric()) {
49-
electricCars[index++] = cars[i];
50-
}
51-
}
5244

5345
return electricCars;
5446
}

src/com/amigoscode/user/UserArrayDataAccessService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.amigoscode.user;
22

3+
import java.lang.reflect.Array;
4+
import java.util.Arrays;
5+
import java.util.List;
36
import java.util.UUID;
47

58
public class UserArrayDataAccessService implements UserDao {
@@ -15,7 +18,7 @@ public class UserArrayDataAccessService implements UserDao {
1518

1619

1720
@Override
18-
public User[] getUsers() {
19-
return users;
21+
public List<User> getUsers() {
22+
return Arrays.asList(users);
2023
}
2124
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.amigoscode.user;
22

3+
import java.util.List;
4+
35
public interface UserDao {
46

5-
User[] getUsers();
7+
List<User> getUsers();
68
}

src/com/amigoscode/user/UserFileDataAccessService.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
57
import java.util.Scanner;
68
import java.util.UUID;
79

810
public class UserFileDataAccessService implements UserDao {
911

1012
@Override
11-
public User[] getUsers() {
13+
public List<User> getUsers() {
1214
File file = new File("src/com/amigoscode/users.csv");
1315

14-
/*
15-
Size 4 because I know there are 4 entries in src/users.csv
16-
If you add more rows in the file update the size of the initial array too
17-
*/
18-
User[] users = new User[4];
16+
List<User> users = new ArrayList<>();
1917

20-
// read example
2118
try {
22-
int index = 0;
2319
Scanner scanner = new Scanner(file);
2420
while (scanner.hasNext()) {
2521
String[] split = scanner.nextLine().split(",");
26-
users[index] = new User(UUID.fromString(split[0]), split[1]);
27-
index++;
22+
users.add(new User(UUID.fromString(split[0]), split[1]));
2823
}
2924
return users;
3025
} catch (IOException e) {

0 commit comments

Comments
 (0)