停车场功能优化

代码

异常类

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;
    }

    /**
     * 存车
     * @param car
     * @return voucher
     */
    @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();
    }


    /**
     *  取车
     * @param voucher
     * @return car
     */


    @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);
    }

    /**
     * 存车
     * @param car
     * @return voucher
     */
    @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()  {
        //Given
        ParkingLot parkingLot = new ParkingLot(0);
        //when
        Car car = new Car();
        Car car1 = new Car();
        //then
        assertThrows(ParkingLotFullException.class, ()->{
                parkingLot.saveCar(car);
                parkingLot.saveCar(car1);
        });
    }
    @Test
    public void  should_save_car_when_the_parkingLot_has_place()  {
        //Given
        ParkingLot parkingLot = new ParkingLot(2);
        //when
        Car car = new Car();
        Voucher voucher = parkingLot.saveCar(car);
        //then
        assertEquals(car, parkingLot.pickUpCar(voucher));
    }

    @Test
    public void should_pick_up_the_car_when_voucher_is_effective()  {
        //Given
        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(){
        //Given
        ParkingLot parkingLotFirst = new ParkingLot(0);
        ParkingLot parkingLotSecond = new ParkingLot(1);
        ParkingLot parkingLotThird = new ParkingLot(2);
        ParkingLotBoy parkingLotBoy =new ParkingLotBoy(parkingLotFirst,parkingLotSecond,parkingLotThird);
        //when
        parkingLotBoy.saveCar(new Car());
        parkingLotBoy.saveCar(new Car());
        //then
        Assert.assertEquals(false,parkingLotSecond.haveSpace());
        Assert.assertEquals(true,parkingLotThird.haveSpace());
    }

    @Test
    public void  should_not_save_car_when_parkingLot_is_full(){
        //Given
        ParkingLotBoy parkingLotBoy =new ParkingLotBoy(new ParkingLot(0),new ParkingLot(0),new ParkingLot(1));
        //when then
        assertThrows(ParkingLotFullException.class, ()->{
            parkingLotBoy.saveCar(new Car());
            parkingLotBoy.saveCar(new Car());
        });
        
    }

    @Test
    public void should_pick_up_car_when_voucher_is_effective()  {
        //Given
        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);
        //when then
        Assert.assertEquals(car, parkingLotBoy.pickUpCar(voucher));
    }

    //凭证无效
    @Test
    public void should_not_pickUp_car_when_voucher_is_invalid(){
        //Given
        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(){
        //Given
        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();
        //when
        Voucher voucher = smartParkingLotBoy.saveCar(car);
        //then
        Assert.assertEquals(true,parkingLotThird.haveSpace());
        Assert.assertEquals(car,parkingLotSecond.pickUpCar(voucher));
    }

    //无停车位
    @Test
    public void should_not_save_car_when_parkingLot_is_Full(){
        //Given
        ParkingLot parkingLotFirst = new ParkingLot(0);
        SmartParkingLotBoy smartParkingLotBoy = new SmartParkingLotBoy(parkingLotFirst);
        //when then
        assertThrows(ParkingLotFullException.class, ()->{
           smartParkingLotBoy.saveCar(new Car());
        });
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想去拉萨。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值