Question: Questions on building a parking lot program in Java. Hi, I'm very new to Java, need help on a parking lot program. file [Car.java] file[ParkingLot.java]
Questions on building a parking lot program in Java.
Hi, I'm very new to Java, need help on a parking lot program.
file [Car.java]
file[ParkingLot.java] -- It's an interface (2 pictures upload)
![new to Java, need help on a parking lot program. file [Car.java]](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f5c1682d152_86366f5c167973b9.jpg)
![file[ParkingLot.java] -- It's an interface (2 pictures upload) File[ParkingLotFactory.java] -- I've handled](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f5c168d78e8_86466f5c16861223.jpg)
File[ParkingLotFactory.java] -- I've handled the exceptions in this class already, the return needs to be modified

file[ParkingLotImp.java]: -- the class needs to be implemented
![file[ParkingLotImp.java]: -- the class needs to be implemented file[ParkingTicket.java] public class ParkingTicket](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f5c16a2a6aa_86566f5c169bad02.jpg)
file[ParkingTicket.java]
public class ParkingTicket { }
Following are the tests:
file[ParkingLotFactoryTest.java]
![{ } Following are the tests: file[ParkingLotFactoryTest.java] file[ParkingLotFetchingTest.java] -- a bit long,](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f5c16ad9fd7_86666f5c16a5bcd8.jpg)
file[ParkingLotFetchingTest.java] -- a bit long, so I give the code text below.
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; class ParkingLotFetchingTest { private ParkingLot createParkingLotWithPlentyOfCapacity() { return ParkingLotFactory.create(1000); } @Test void should_return_the_parked_car_using_ticket() { // Given final ParkingLot parkingLot = createParkingLotWithPlentyOfCapacity(); final Car car = new Car(); final ParkingTicket ticket = parkingLot.park(car); // When final Car fetched = parkingLot.fetch(ticket); // Then assertSame(car, fetched); } @Test void should_return_the_parked_car_correspond_to_the_ticket() { // Given final ParkingLot parkingLot = createParkingLotWithPlentyOfCapacity(); final Car firstCar = new Car(); final Car secondCar = new Car(); final ParkingTicket firstTicket = parkingLot.park(firstCar); final ParkingTicket secondTicket = parkingLot.park(secondCar); // When final Car fetchedSecond = parkingLot.fetch(secondTicket); final Car fetchedFirst = parkingLot.fetch(firstTicket); // Then assertSame(secondCar, fetchedSecond); assertSame(firstCar, fetchedFirst); } @Test void should_fail_if_ticket_is_null() { // Given final ParkingLot parkingLot = createParkingLotWithPlentyOfCapacity(); parkingLot.park(new Car()); // When final Car fetched = parkingLot.fetch(null); // Then assertNull(fetched); assertEquals("No ticket is provided.", parkingLot.getLastErrorMessage()); } @Test void should_fail_if_ticket_has_been_used() { // Given final ParkingLot parkingLot = createParkingLotWithPlentyOfCapacity(); final ParkingTicket ticket = parkingLot.park(new Car()); parkingLot.fetch(ticket); // When final Car fetched = parkingLot.fetch(ticket); // Then assertNull(fetched); assertEquals("Invalid ticket.", parkingLot.getLastErrorMessage()); } @Test void should_fail_if_ticket_matches_no_car() { // Given final ParkingLot parkingLot = createParkingLotWithPlentyOfCapacity(); parkingLot.park(new Car()); // When final Car fetched = parkingLot.fetch(new ParkingTicket()); // Then assertNull(fetched); assertEquals("Invalid ticket.", parkingLot.getLastErrorMessage()); } @Test void should_update_position_on_fetching_success() { // Given final ParkingLot parkingLot = createParkingLotWithPlentyOfCapacity(); final ParkingTicket ticket = parkingLot.park(new Car()); final int positions = parkingLot.getAvailableParkingPosition(); // When parkingLot.fetch(ticket); // Then assertEquals(positions, parkingLot.getAvailableParkingPosition() - 1); } }
file[ParkingLotParkingTest.java] -- give the code text as well.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; class ParkingLotParkingTest { private ParkingLot createParkingLotWithPlentyOfCapacity() { return ParkingLotFactory.create(1000); } private ParkingLot createFullParkingLot() { final int capacity = 10; final ParkingLot parkingLot = ParkingLotFactory.create(capacity); for (int i = 0; i /** * Represents a car. */ public class Car { //NOSONAR } public interface Parking Lot { * This method will park the given car to this {@zink Parking Lot }.
* * Each car will use only one space in the parking lot. If the parking lot is not fully * filled, then you should always be able to park a car into it.
* * If the parking operation is successful. Then the method should return a non-nuzz * {@link ParkingTicket). And there should not be any error message. But if the parking * operation failed, the method should return a null {@link ParkingTicket} and set the * error message. Please note that it should completely cancel the operation when the * operation failed.
* The following operations will fail the parking process:
* - When the car is nuzz. The error message is: {@code "You are parking nothing."}
- When the car is parked twice. The error message is: {@code "The car has been parked."}
- When the parking lot has no empty space. The error message is: {@code "The parking lot is fuzz."} *
* * * @param car The car to park. * @return the parking ticket object. Or null if parking process failed. */ ParkingTicket park(Car car); /** *
This method fetches car from the parking lot, using the ticket provided. * After fetching the car, the car will no longer exist in the parking lot.
* *
If the fetching process is successful, the return value should be the car * correspond to the {@link ParkingTicket}. And there should be no error message.
* *
If the fetching process failed. The returned car is {@code null}. You can get * error message using {@link Parking Lot#getLastError Message()} method. The fetching * process should be completely cancelled if it is failed.
*
If the fetching process failed. The returned car is {@code nuzz}. You can get * error message using {@link Parking Lot#getLastErrorMessage()} method. The fetching * process should be completely cancelled if it is failed.
*
The following operations will fail the fetching process:
*
- If the ticket is {@code null). The error message should be {@code "No ticket is provided."}
- If the ticket has been used. The error message should be {@code "Invalid ticket."}
- If the ticket matches no car. The error message should be {@code "Invalid ticket."} *
* * @param ticket The ticket you get when parking the car. * @return the car correspond to the ticket. Or null if the process failed. */ Car fetch(ParkingTicket ticket); /** * This method returns the error message to the last operation. If the last operation * is successful, it should return {@code null}. * @return the error message of the last operation. Or {@code null} when there is no * error. */ String getLastErrorMessage(); /** * This method returns the available parking position count. This number will always greater * than or equal to zero. If it returns zero, it means no parking position left. * @return the available parking position count. */ int getAvailableParkingPosition(); } public class ParkingLotFactory { private static final int MAX_CAPACITY = 2000; /** * Create a parking lot with specified capacity (the initial parking position * count). * @param capacity The capacity of the parking lot. The minimum capacity is 1 and the maximum capacity is 2000. * @return The {@link ParkingLot} object. * @throws IllegalArgumentException The capacity is out of range. */ public static Parkinglot create(int capacity) { // TODO: implement the method. // MAX_CAPACITY) { throw new IllegalArgumentException("Capacity should be less than or equal to 2000: + capacity); } return null; // needs to be modified // --end-> } } class Parking Lot Impl implements Parking Lot { // TODO: Please implement the class // } import org.junit.jupiter.api. Test; import static org.junit.jupiter.api. Assertions.assertEquals; import static org. junit.jupiter.api. Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; class Parking LotFactoryTest { @Test void should_throw_if_capacity_is_less_than_or_equal_to_zero() { assert InvalidCapacity( capacity: 0, expectedMessage: "Capacity should be greater than 0: 0"); assertInvalidCapacity( capacity: -1, expected Message: "Capacity should be greater than 0: -1"); } @Test void should_throw_if_capacity_is_greater_than_maximum() { assertInvalidCapacity( capacity: 2001, expected Message: "Capacity should be less than or equal to 2000: 2001"); } @Test void should_create_parking lot() { assertNotNull(Parking LotFactory.create(1)); assertNotNull(Parking LotFactory.create(2000)); } private void assertInvalidCapacity(int capacity, String expectedMessage) { final IllegalArgumentException exception = assertThrows (IllegalArgumentException.class, () -> Parking LotFactory.create(capacity)); assertEquals(expectedMessage, exception.getMessage()); } } /** * Represents a car. */ public class Car { //NOSONAR } public interface Parking Lot { *
This method will park the given car to this {@zink Parking Lot }.
* *
Each car will use only one space in the parking lot. If the parking lot is not fully * filled, then you should always be able to park a car into it.
* *
If the parking operation is successful. Then the method should return a non-nuzz * {@link ParkingTicket). And there should not be any error message. But if the parking * operation failed, the method should return a null {@link ParkingTicket} and set the * error message. Please note that it should completely cancel the operation when the * operation failed.
*
The following operations will fail the parking process:
*
- When the car is nuzz. The error message is: {@code "You are parking nothing."}
- When the car is parked twice. The error message is: {@code "The car has been parked."}
- When the parking lot has no empty space. The error message is: {@code "The parking lot is fuzz."} *
* * * @param car The car to park. * @return the parking ticket object. Or null if parking process failed. */ ParkingTicket park(Car car); /** *
This method fetches car from the parking lot, using the ticket provided. * After fetching the car, the car will no longer exist in the parking lot.
* *
If the fetching process is successful, the return value should be the car * correspond to the {@link ParkingTicket}. And there should be no error message.
* *
If the fetching process failed. The returned car is {@code null}. You can get * error message using {@link Parking Lot#getLastError Message()} method. The fetching * process should be completely cancelled if it is failed.
*
If the fetching process failed. The returned car is {@code nuzz}. You can get * error message using {@link Parking Lot#getLastErrorMessage()} method. The fetching * process should be completely cancelled if it is failed.
*
The following operations will fail the fetching process:
*
- If the ticket is {@code null). The error message should be {@code "No ticket is provided."}
- If the ticket has been used. The error message should be {@code "Invalid ticket."}
- If the ticket matches no car. The error message should be {@code "Invalid ticket."} *
* * @param ticket The ticket you get when parking the car. * @return the car correspond to the ticket. Or null if the process failed. */ Car fetch(ParkingTicket ticket); /** * This method returns the error message to the last operation. If the last operation * is successful, it should return {@code null}. * @return the error message of the last operation. Or {@code null} when there is no * error. */ String getLastErrorMessage(); /** * This method returns the available parking position count. This number will always greater * than or equal to zero. If it returns zero, it means no parking position left. * @return the available parking position count. */ int getAvailableParkingPosition(); } public class ParkingLotFactory { private static final int MAX_CAPACITY = 2000; /** * Create a parking lot with specified capacity (the initial parking position * count). * @param capacity The capacity of the parking lot. The minimum capacity is 1 and the maximum capacity is 2000. * @return The {@link ParkingLot} object. * @throws IllegalArgumentException The capacity is out of range. */ public static Parkinglot create(int capacity) { // TODO: implement the method. // MAX_CAPACITY) { throw new IllegalArgumentException("Capacity should be less than or equal to 2000: + capacity); } return null; // needs to be modified // --end-> } } class Parking Lot Impl implements Parking Lot { // TODO: Please implement the class // } import org.junit.jupiter.api. Test; import static org.junit.jupiter.api. Assertions.assertEquals; import static org. junit.jupiter.api. Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; class Parking LotFactoryTest { @Test void should_throw_if_capacity_is_less_than_or_equal_to_zero() { assert InvalidCapacity( capacity: 0, expectedMessage: "Capacity should be greater than 0: 0"); assertInvalidCapacity( capacity: -1, expected Message: "Capacity should be greater than 0: -1"); } @Test void should_throw_if_capacity_is_greater_than_maximum() { assertInvalidCapacity( capacity: 2001, expected Message: "Capacity should be less than or equal to 2000: 2001"); } @Test void should_create_parking lot() { assertNotNull(Parking LotFactory.create(1)); assertNotNull(Parking LotFactory.create(2000)); } private void assertInvalidCapacity(int capacity, String expectedMessage) { final IllegalArgumentException exception = assertThrows (IllegalArgumentException.class, () -> Parking LotFactory.create(capacity)); assertEquals(expectedMessage, exception.getMessage()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts

