停车场模拟

模拟停车场停车的情景,当车位满的时候无法进行停车,当车位空的时候无法把车开走。

停车位类:

public class Space {
    private int id;
    private boolean state;

    public Space(int id){
        this.id = id;
        state = false;
    }

    public void in(){
        state = true;
    }

    public void out(){
        state = false;
    }

    public boolean getState(){
        return state;
    }

    public int getId(){
        return id;
    }

    public String toString(){
        if(state){
            return id + ": This space is full.";
        }else{
            return id + ": This space is empty.";
        }
    }
}

停车场类:

import java.util.*;

public class Park {

    private ArrayList<Space> all = new ArrayList<>();
    private ArrayList<Space> emptySpace = new ArrayList<>();
    private ArrayList<Space> fullSpace = new ArrayList<>();
    private int capacity;
    private int fullNum;

    public Park(int num){
        capacity = num;
        Random r = new Random();
        for(int i = 1; i < capacity + 1; i++){
            if(r.nextBoolean()){
                Space temp = new Space(i);
                temp.in();
                fullSpace.add(temp);
                all.add(temp);
            }else{
                Space temp = new Space(i);
                temp.out();
                emptySpace.add(temp);
                all.add(temp);

            }
        }
        fullNum = fullSpace.size();
        //emptySpace = (ArrayList) all.clone();
    }

    //定义停车方法
    public synchronized void in(){
        if(fullNum < capacity){
            int index = (int) (Math.random() * emptySpace.size());
            System.out.println(Thread.currentThread().getName() + "在" + emptySpace.get(index).getId() + "车位上停车了");
            emptySpace.get(index).in();
            fullSpace.add(emptySpace.remove(index));
            fullNum++;
            notifyAll();
        }else{
            try{
                System.out.println("车位已满");
                wait();
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }
        }


    }

    //定义车开走方法
    public synchronized void out(){
        if(fullNum > 0){
            int index = (int) (Math.random() * fullSpace.size());
            System.out.println(Thread.currentThread().getName() + "把" + fullSpace.get(index).getId() + "车位上的车开走了");
            //System.out.println(fullSpace.get(index).getId() + "车位上的车开走了");
            fullSpace.get(index).out();
            emptySpace.add(fullSpace.remove(index));
            fullNum--;
            notifyAll();
        }else{
            try{
                System.out.println("车位已空");
                wait();
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }
        }

    }

    public void show(){
        System.out.println();
        for(int i = 0; i < all.size(); i++){
            System.out.println(all.get(i));
        }
    }
}

停车类,从Thread继续

public class ParkIn extends Thread{

    private Park park;
    private int num;

    public ParkIn(String name, Park park, int num){
        super(name);
        this.park = park;
        this.num = num;

    }

    public void run(){
        for(int i = 0; i < num; i++){
            park.in();
        }

    }

}

车开走类

public class ParkOut extends Thread{

    private Park park;
    private int num;

    public ParkOut(String name, Park park, int num){
        super(name);
        this.park = park;
        this.num = num;
    }

    public void run(){
        for(int i = 0; i < num; i++){
            park.out();
        }
    }
}

测试类,总共有3个停车类,3个开走类。

import java.util.*;

public class ParkTest {
    public static void main(String[] args) {

        int num = 20;
        Random random = new Random();
        Park p = new Park(random.nextInt(num));
        p.show();
        ParkIn pi1 = new ParkIn("IN:A", p, random.nextInt(num));
        ParkIn pi2 = new ParkIn("IN:B", p, random.nextInt(num));
        ParkIn pi3 = new ParkIn("IN:C", p, random.nextInt(num));
        ParkOut po1 = new ParkOut("OUT A", p, random.nextInt(num));
        ParkOut po2 = new ParkOut("OUT B", p, random.nextInt(num));
        ParkOut po3 = new ParkOut("OUT C", p, random.nextInt(num));
        pi1.start();
        pi2.start();
        pi3.start();
        po1.start();
        po2.start();
        po3.start();
        //p.show();

    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值