Parking Lot Simulation

edited November 2017 in Questions about Code

I am trying to create a parking lot simulator for my first year engineering class. You can find the details for the assignment here: http://docdro.id/7ZyzCEF. I'm now trying to complete the third milestone. The cars are supposed to randomly enter the lot and then random stalls should become occupied (turn red) when the car enters. The stalls should be occupied for a random amount of time before the car leaves the lot out the exit. I managed to make the cars enter the lot randomly, however from there I don't know how to make them randomly occupy stalls and stay there only for a specific time before leaving again. Does anyone know how I would do this?

[ code in comments ]

Tagged:

Answers

  • Third time we've seen this so search the forum.

    Edit post, highlight code, press Ctrl-o to format. We can't read the code you've posted the way it is.

  • When a car enters the lot, pick one of the parking lot sections for it to park in. Keep picking sections until you find one that has a free space. There must be one because the car was allowed into the lot, right? Then pick a parking spot in that lot. Keep picking them until you find a free space. There must be one because you picked a section that had a space!

    Then generate a random number that will represent the total time that car is going to be parked in that space for. Add that amount of time to the current simulation time to get the time the car will exit. You can store this value with the car, or you could associate it with the parking space instead. In either case, every time the simulation time changes, you should be checking all the spots to see if it is time for any of their cars to leave.

  • Hopefully this is easier to read,
    
    Gate G1, G2;
    ParkingLot L1;
    Street first, second, vert;
    Car [] c`;
    ControlPanel CP;
    Date Date1;
    
    
    void setup() {
      background(#0E620E);
      size(1250, 970);
      L1= new ParkingLot (10, 250, 3, 2);
      L1.drawLot();
      c= new Car[100];
      CP=new ControlPanel(10, 10, 400, 140);
      Date1 = new Date (int(random(0, 365)), int(random(0, 24)), int (random(0, 60)), boolean(int(random(0, 2))));
    
      for (int i=0; i<c.length; i++) {
        c[i]=new Car(int(random(-100000, 0)), 180, 60, 30, int(random(0, 2)), int(random(0, 3)), int(random(0, 6)), int(random(0, 2)));
      }
    }
    
    
    void draw() {
      background(#0E620E);
      CP.drawcontrol();
      L1.drawLot();
    
        for (int i=0; i<c.length; i++) {
          c[i].drawcar();
          c[i].movecar();
    
      }
    }
    
    class Car {
      float x;
      float y;
      float wide;
      float high;
      boolean parked;
      int col1=int(random(0, 255));
      int col2=int(random(0, 255));
      int col3=int(random(0, 255));
      float temp;
      float temp2;
      int i;
      int j;
      int u;
      int w;
    
      Car(float posx, float posy, float w, float h, int random1, int random2, int random3, int random4) {
        x=posx;
        y=posy;
        wide=w;
        high=h;
        temp=high;
        temp2=wide;
        i=random1;
        j=random2;
        u=random3;
        w=random4;
        parked=boolean(int(random(0, 2)));
      }
    
    
      void movecar() {
        if ((parked==false)||(parked==true && x<610 && y>=180)||(parked==true && G1.Spaces==0))
          x+=(int(random(5, 10)));
        else if (parked==true && x>=610 && y>=180) {
          high=temp2;
          wide=temp;
          y+=(int(random(5, 10)));
        }
      }
    
    
      void drawcar() {
        noStroke();
        fill(col1, col2, col3);
        rect(x, y, wide, high);
      }
    }
    
    class ControlPanel {
      float x;
      float y;
      float leng;
      float high;
      float textx;
      float texty;
      String words;
      int size;
    
      ControlPanel(float posx, float posy, float longs, float highs) {
        x=posx;
        y=posy;
        leng=longs;
        high=highs;
      }
    
      void drawcontrol() {
        fill(#989090);
        stroke(20);
        strokeWeight(4);
        rect(x, y, leng, high);
        rect(450,10,310,140);
        ellipse(530,80,120,120);
        ellipse(680,80,120,120);
        rect(790,y,leng-150,high);
        fill(#40AA54);
        triangle(505,35,505,125,580,80);
        strokeWeight(18);
        line(660,40,660,120);
        line(700,40,700,120);
        textSize(25);
        fill(0);
        text("Parking Rates", 20, 35);
        text("Simulation Values",800,35);
        textSize(20);
        text("$3.00/Hour    Mon-Sat     8AM to 6PM",20,75);
        text("$1.50/Hour    Sunday",20,110);
        textSize(16);
        text("Net Profits:",800,65);
        text("Time Elapsed:",800,95);
        text("Customers:",800,125);
    
      }
    }
    
    class Date {
      final String[] days = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
      int today;
      int hour;
      int minute;
      boolean before_noon;
    
    Date(int d, int h,int m, boolean beforeNoon){
      today = d%7;
      if (h%12==0)
      hour=12;
      else
      hour=h%12;
      minute=m%60;
      before_noon=before_noon;
    }
    Date(Date d){
     today =d.today;
     hour= d.hour;
     minute =d.minute;
     before_noon =d.before_noon;
     }
    
     void addHour(){
       ++hour;
       if (hour==13)
       hour-=12;
       if (hour==12 && before_noon==true)
       before_noon=false;
       else if (hour==12 && before_noon==false)
       before_noon=true;
     }
    
     void addMinute(){
       ++minute;
       if (minute==60)
      { minute-=60;
      addHour();
      }
     }
    
      String toString(){
        String date = days[today];
        if (hour<10)
        date+= " 0" + hour;
        else
        date+=" " + hour;
    
        if (minute <10)
        date += ":0" + minute;
        else 
        date +=":" + minute;
    
        if (before_noon)
        date += "AM";
        else
        date+="PM";
        return date;
      }
    
      boolean equal (Date date){
        if (date.today==today && date.hour==hour && date.minute==minute && date.before_noon==before_noon)
        return true;
    
        else
        return false;
      }
    }
    
    class Gate {
      float x1;
      float y1;
      float x2;
      float y2;
      boolean l;
      int Spaces=60-L1.LotAmount();
    
    
      Gate (float posx1, float posy1, float posx2, float posy2) {
        x1=posx1;
        y1=posy1;
        x2=posx2;
        y2=posy2;
      }
    
      void drawgate() {
        fill (255);
        strokeWeight(2);
        rect(480, 230, 110, 20);
        rect(480, 860, 110, 20);
        fill(#133BD3);
        rect(480, 250, 110, 20);
        rect(480, 880, 110, 20);
        strokeWeight(6);
        stroke (0);
        textSize(14);
        fill(255);
        text("Spaces Left: " + Spaces, 485, 265);
        text("Fee: "+"Fee", 495, 895);
        fill(0);
        text("ENTRANCE", 500, 245);
        text("EXIT", 525, 875);
        if (L1.StatusLot()==true) {
          line (x1, y1, x2, y1);
        }
    
        if (L1.StatusLot()==false) {
          line (x1, y1, x1, y2);
        }
      }
    }
    class ParkingLot
    {
      float x=0;
      float y=0;
      int row, cols;
      boolean l;
      int counter=0;
    
      ParkingStallSection [][] PLot;
    
      ParkingLot (float x_position, float y_position, int rows, int columns)
      {
        PLot= new ParkingStallSection[rows][columns];
    
        y=y_position+40;
        for (int i=0; i<rows; i++) {
          x=x_position+80;
          for (int j=0; j<columns; j++) {
            PLot[i][j] = new ParkingStallSection(x, y, 2, 5);
            x+=570;
          }
          y+=200;
        }
        x=x_position+30;
        y=y_position+30;
      }
    
      boolean StatusLot () {
        counter=0;
        for (int i=0; i<PLot.length; i++) {
          for (int j=0; j<PLot[i].length; j++) {
            if (PLot[i][j].StatusSection()==true) {
              counter++;
            }
          }
        }
        if (counter==6)
          return true;
        else
          return false;
      }
    
      int LotAmount () {
        counter=0;
        for (int i=0; i<PLot.length; i++) {
          for (int j=0; j<PLot[i].length; j++) {
            counter+=PLot[i][j].StatusAmount(); 
            {
            }
          }
        }
        return counter;
      }
    
    
        void drawLot()
        {
          strokeWeight(3);
          stroke(255);
          fill (#696B71);
          rect(x-20, y-20, x+1170, y+320);
          first = new Street(0, 160, width, 70, "North City Street", true);
          first.drawstreets(570, 200);
          second = new Street (0, 900, width, 70, "South City Street", true);
          second.drawstreets (570, 945);
          vert = new Street (595, 230, 60, height-300, "No name", false);
          vert.drawstreets(0, 0);
          G1 = new Gate (597, 250, 652, 315);
          G1.drawgate();
          G2 = new Gate (597, 885, 652, 820);
          G2.drawgate();
    
    
          for (int i=0; i<PLot.length; i++)
          { 
            for (int j=0; j<PLot[i].length; j++) {
              PLot[i][j].drawSection();
            }
          }
        }
      }
    
    class ParkingStall {
      // STALL ATTRIBUTES
      boolean occupied;
      Date timeTaken;
    
      // DIMENSIONS AND POSITION
      float stallWidth;
      float stallHeight;
      float posX;
      float posY;
    
    
    
      ParkingStall(float x, float y, float w, float h) {
        occupied = false;
        posX = x;
        posY = y;
        stallWidth = w;
        stallHeight = h;
      }
    
    
      void drawStall() {
        if (occupied)
          fill(color(255, 90, 71)); // RED STALL
        else
          fill(color(152, 251, 152));  // GREEN STALL
        strokeWeight(4);
        stroke(255);
        rect(posX, posY, stallWidth, stallHeight);
        strokeWeight(1);
      }
    
      // Sets whether the stall is occupied or not
      void setStatus(boolean status, Date time)
      {
        occupied = status;
        if (occupied) {
          timeTaken = new Date(time);
        }
      }
    }
    
    class ParkingStallSection {
      float x, y;
      ParkingStall[][] PS1;
      int counter;
      int fees;
    
      ParkingStallSection (float x_position, float y_position, int row, int col)
      {
        PS1= new ParkingStall [row][col];
    
        y=y_position;
        for (int i=0; i<PS1.length; i++) {
          x=x_position;
          for (int j=0; j<PS1[i].length; j++) {
            PS1[i][j]= new ParkingStall(x, y, 100, 70);
            x+=100;
          }
          y+=70;
        }
      }
    
      boolean StatusSection () {
        counter=0;
        for (int i=0; i<PS1.length; i++) {
          for (int j=0; j<PS1[i].length; j++) {
            if (PS1[i][j].occupied==true) {
              counter++;
            }
          }
        }
        if (counter==10)
          return true;
        else
          return false;
      }
    
      int StatusAmount () {
        counter=0;
        for (int i=0; i<PS1.length; i++) {
          for (int j=0; j<PS1[i].length; j++) {
            if (PS1[i][j].occupied==true) {
              counter++;
            }
          }
        }
        return counter;
      }
    
      void drawSection() {
        for (int i=0; i<PS1.length; i++) {
          for (int j=0; j<PS1[i].length; j++) {
            PS1[i][j].drawStall();
          }
        }
      }
    }
    
    class Street {
      String name;
      float wid;
      float hi;
      float x;
      float y;
      boolean horizontal;
    
      Street (float posx, float posy, float width1, float heigt1, String streetname, boolean horiz)
      {
        name=streetname;
        wid=width1;
        hi=heigt1;
        x=posx;
        y=posy;
        horizontal=horiz;
      }
    
      void drawstreets(float textx, float texty)
      { 
        if (horizontal==true) {
          noStroke();
          fill(#696B71);
          rect(x, y, wid, hi);
          textSize(14);
          fill(255);
          text(name, textx, texty);
        } 
        else {
          noStroke();
          fill(#696B71);
          rect(x, y, wid, hi);
        }
      }
    }
    
  • Yeah, but you could've edited the original post rather than posting again...

  • Sorry, this is my first post on this forum, any help with the question would be greatly appreciated.

Sign In or Register to comment.