代码
异常类
package exception;
public class ParkingLotFullException extends RuntimeException {
public ParkingLotFullException(String str) {
super(str);
}
}
package exception;
public class VoucherException extends RuntimeException{
public VoucherException(String str) {
super(str);
}
}
实体类
public class Car {
public Car() {
}
}
import exception.ParkingLotFullException;
import exception.VoucherException;
import java.util.HashMap;
public class ParkingLot implements SaveAndPickUp{
private int CAPACITY;
private HashMap<Voucher,Car> hashMap;
public int parkingLotCapacity(){
return CAPACITY-hashMap.size();
}
public ParkingLot(int capacity) {
CAPACITY = capacity;
hashMap=new HashMap<>(capacity);
}
public Voucher saveCar(Car car) {
Voucher voucher=new Voucher();
if(haveSpace()) {
hashMap.put(voucher, car);
}else {
throw new ParkingLotFullException("存取车位失败");
}
return voucher;
}
public boolean haveSpace() {
return hashMap.size() < CAPACITY;
}
public Car pickUpCar(Voucher voucher) {
if(haveCar(voucher)) {
return hashMap.remove(voucher);
}else {
throw new VoucherException("凭证失效");
}
}
public boolean haveCar(Voucher voucher) {
return hashMap.containsKey(voucher);
}
}
import exception.ParkingLotFullException;
import exception.VoucherException;
import java.util.*;
import java.util.stream.Stream;
public class ParkingLotBoy implements SaveAndPickUp{
private final ArrayList<ParkingLot> parkingLots;
public ParkingLotBoy(ParkingLot ... parkingLots) {
Objects.requireNonNull(parkingLots);
this.parkingLots =new ArrayList<>(Arrays.asList(parkingLots));
}
public boolean parkingLotsIsFull(ParkingLot[] parkingLots) {
return parkingLots.length == 0;
}
@Override
public Voucher saveCar(Car car) {
Optional<ParkingLot> optionalParkingLot = getParkingLotStream().filter(ParkingLot::haveSpace).findFirst();
if(optionalParkingLot.isPresent()){
return optionalParkingLot.get().saveCar(car);
}else {
throw new ParkingLotFullException("所有停车场已满,无法停车!");
}
}
public Stream<ParkingLot> getParkingLotStream() {
return parkingLots.stream();
}
@Override
public Car pickUpCar(Voucher voucher) {
Optional<ParkingLot> optionalParkingLot = parkingLots.stream().filter(parkingLot -> parkingLot.haveCar(voucher)).findFirst();
if(optionalParkingLot.isPresent()){
return optionalParkingLot.get().pickUpCar(voucher);
}else {
throw new VoucherException("凭据失效,无法停车");
}
}
}
import exception.ParkingLotFullException;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class SmartParkingLotBoy extends ParkingLotBoy{
public SmartParkingLotBoy(ParkingLot... parkingLots) {
super(parkingLots);
}
@Override
public Voucher saveCar(Car car) {
Optional<ParkingLot> parkingLotOptional = getParkingLotStream()
.sorted((parkingLotFirst, parkingLotSecond) -> -((parkingLotFirst.parkingLotCapacity())
- (parkingLotSecond.parkingLotCapacity())))
.filter(ParkingLot::haveSpace).findFirst();
if(parkingLotOptional.isPresent()){
return parkingLotOptional.get().saveCar(car);
}
throw new ParkingLotFullException("所有停车场已满,无法停车!");
}
}
public class Voucher {
public Voucher() {
}
}
接口
public interface SaveAndPickUp {
public Voucher saveCar(Car car);
public Car pickUpCar(Voucher voucher);
}
测试类
import exception.ParkingLotFullException;
import exception.VoucherException;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ParkingLotTest {
@Test
public void should_not_save_car_when_parkingLot_is_full() {
ParkingLot parkingLot = new ParkingLot(0);
Car car = new Car();
Car car1 = new Car();
assertThrows(ParkingLotFullException.class, ()->{
parkingLot.saveCar(car);
parkingLot.saveCar(car1);
});
}
@Test
public void should_save_car_when_the_parkingLot_has_place() {
ParkingLot parkingLot = new ParkingLot(2);
Car car = new Car();
Voucher voucher = parkingLot.saveCar(car);
assertEquals(car, parkingLot.pickUpCar(voucher));
}
@Test
public void should_pick_up_the_car_when_voucher_is_effective() {
ParkingLot parkingLot=new ParkingLot(5);
Car car=new Car();
Voucher voucher1 = parkingLot.saveCar(car);
assertEquals(car, parkingLot.pickUpCar(voucher1));
}
@Test
public void should_not_pick_up_the_car_when_voucher_is_invalid() {
ParkingLot parkingLot=new ParkingLot(3);
Car car=new Car();
Voucher voucher1 = parkingLot.saveCar(car);
Voucher voucher=new Voucher();
assertThrows(VoucherException.class, ()->{
parkingLot.pickUpCar(voucher);
});
}
}
import exception.ParkingLotFullException;
import exception.VoucherException;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ParingLotBoyTest extends ParkingLotTest{
@Test
public void should_save_car_when_parkingLot_is_not_full(){
ParkingLot parkingLotFirst = new ParkingLot(0);
ParkingLot parkingLotSecond = new ParkingLot(1);
ParkingLot parkingLotThird = new ParkingLot(2);
ParkingLotBoy parkingLotBoy =new ParkingLotBoy(parkingLotFirst,parkingLotSecond,parkingLotThird);
parkingLotBoy.saveCar(new Car());
parkingLotBoy.saveCar(new Car());
Assert.assertEquals(false,parkingLotSecond.haveSpace());
Assert.assertEquals(true,parkingLotThird.haveSpace());
}
@Test
public void should_not_save_car_when_parkingLot_is_full(){
ParkingLotBoy parkingLotBoy =new ParkingLotBoy(new ParkingLot(0),new ParkingLot(0),new ParkingLot(1));
assertThrows(ParkingLotFullException.class, ()->{
parkingLotBoy.saveCar(new Car());
parkingLotBoy.saveCar(new Car());
});
}
@Test
public void should_pick_up_car_when_voucher_is_effective() {
ParkingLot parkingLotFirst = new ParkingLot(2);
ParkingLot parkingLotSecond = new ParkingLot(2);
ParkingLotBoy parkingLotBoy = new ParkingLotBoy(parkingLotFirst,parkingLotSecond);
Car car = new Car();
Voucher voucher = parkingLotBoy.saveCar(car);
Assert.assertEquals(car, parkingLotBoy.pickUpCar(voucher));
}
@Test
public void should_not_pickUp_car_when_voucher_is_invalid(){
ParkingLot parkingLot = new ParkingLot(2);
ParkingLotBoy parkingLotBoy = new ParkingLotBoy(parkingLot);
Car car = new Car();
Voucher voucher = new Voucher();
assertThrows(VoucherException.class, ()->{
parkingLotBoy.pickUpCar(voucher);
});
}
}
import exception.ParkingLotFullException;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SmartParkingLotBoyTest extends ParingLotBoyTest {
@Test
public void should_save_car_when_parkingLot_hava_max(){
ParkingLot parkingLotFirst = new ParkingLot(1);
ParkingLot parkingLotSecond = new ParkingLot(3);
ParkingLot parkingLotThird = new ParkingLot(3);
SmartParkingLotBoy smartParkingLotBoy = new SmartParkingLotBoy(parkingLotFirst,parkingLotSecond,parkingLotThird);
Car car = new Car();
Voucher voucher = smartParkingLotBoy.saveCar(car);
Assert.assertEquals(true,parkingLotThird.haveSpace());
Assert.assertEquals(car,parkingLotSecond.pickUpCar(voucher));
}
@Test
public void should_not_save_car_when_parkingLot_is_Full(){
ParkingLot parkingLotFirst = new ParkingLot(0);
SmartParkingLotBoy smartParkingLotBoy = new SmartParkingLotBoy(parkingLotFirst);
assertThrows(ParkingLotFullException.class, ()->{
smartParkingLotBoy.saveCar(new Car());
});
}
}