I would like to know what I should do to make the array list be returned properly. Currently I am getting this error on each line of the testing method where get(o), get(1) etc: cannot find symbol symbol: method get(int) location: interface Collection
see here: error image
I have also tried other methods such as assertThat which did not work.
I have a testing method that looks like this:
@Test
public void testSaveCar() {
dao.saveCar(car3);
assertEquals(dao.getCars().get(0), car1.getCarId());
assertEquals(dao.getCars().get(1).getCarId(), car2.getCarId());
assertEquals(dao.getCars().get(2).getCarId(), car3.getCarId());
dao.removeCar(car3);
}
and a jdbcdao class that looks like this:
/**
*
*
*/
public class CarJdbcDAO implements CarDAO {
private String url = JdbcConnection.getDefaultConnectionUri();
public CarJdbcDAO() {
}
public CarJdbcDAO(String uri) {
this.url = uri;
}
@Override
public Collection<Car> getCars() {
String sql = "select * from Car";
try (
Connection dbCon = JdbcConnection.getConnection(url); //get connection to db
PreparedStatement stmt = dbCon.prepareStatement(sql); //create stmt
) {
ResultSet rs = stmt.executeQuery();
List<Car> carsList = new ArrayList<>();
//iterate through query results
while (rs.next()) {
String carId = rs.getString("Car_Id");
String carName = rs.getString("Car_Name");
String carType = rs.getString("Car_Type");
String seatNumber = rs.getString("Seat_Number");
BigDecimal hourlyCharge = rs.getBigDecimal("Hourly_Charge");
String location = rs.getString("Location");
Car car = new Car(carId, carName, carType, seatNumber, hourlyCharge, location);
carsList.add(car);
}
return carsList;
} catch (SQLException ex) {
throw new DAOException(ex.getMessage(), ex);
}
}