parking lot

edited December 2017 in Questions about Code

I'm trying to make a parking lot simulator, I have managed to make my cars enter the lot and randomly park, and I've set up my clock on the upper right hand corner of the screen however I've tried to make the cars stay in the stalls for only a random amount of time before leaving and I can't seem to do it, can anyone help with this?

Here's the portion of my code that pertains to this:

Gate G1, G2;
ParkingLot L1;
Street first, second, vert;
Car [] c;
ControlPanel CP;
int day=int(random(0,7));
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);


  for (int i=0; i<c.length; i++) {
    c[i]=new Car(int(random(-100000, 0)), 180, 60, 30);
  }
}

void draw() {
  background(#0E620E);
  CP.drawcontrol();
  L1.drawLot();
  Date1=new Date (day, hour(), minute(), false);
  textSize(18);
  text(Date1.toString(), 1060, 70);
  for (int i=0; i<c.length; i++) {
    c[i].drawcar();
    c[i].movecar();
    if (c[i].parked==true && c[i].y>=400 && c[i].insideparking==true && c[i].carparked==false) {
      L1.selectrandomstall(i);
    }
  }
}

class Car {
  float x;
  float y;
  float wide;
  float high;
  boolean parked;
  boolean insideparking;
  boolean carparked;
  int stallnumber;
  int col1=int(random(0, 255));
  int col2=int(random(0, 255));
  int col3=int(random(0, 255));
  float temp;
  float temp2;
  int customers;


  Car(float posx, float posy, float w, float h) {
    x=posx;
    y=posy;
    wide=w;
    high=h;
    temp=high;
    temp2=wide;
    stallnumber=-1;
    parked=boolean(int(random(0, 2)));
    insideparking=false;
  }


  void movecar() {
    insideparking=false;
    if (carparked==true) {
    } else if (carparked==false) {
      if ((parked==false)||(parked==true && x<610 && y>=180)||(parked==true && G1.Spaces==0))
        x+=(int(random(10, 15)));
      else if (parked==true && x>=610 && y<910) {
        high=temp2;
        wide=temp;
        y+=(int(random(10, 15)));
        insideparking=true;
      } else if (y>=910) {
        high=temp;
        wide=temp2;
        x+=(int(random(5, 10)));
      }
    }
  }


  void drawcar() {
    noStroke();
    fill(col1, col2, col3);
    rect(x, y, wide, high);
    if (carparked==true) {
      fill(#696B71);
      rect(x, y, wide, high);
    }
  }
}

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 selectrandomstall(int carnumber) {
    int lotspot=int(random(0, 3));
    int lotspoty=int(random(0, 2));
    int secspot=int(random(0, 5));
    int secspoty=int(random(0, 2));
    if (L1.PLot[lotspot][lotspoty].PS1[secspoty][secspot].occupied==false) {
      c[carnumber].carparked=true;
      CP.customers++;
         L1.PLot[lotspot][lotspoty].PS1[secspoty][secspot].setStatus(true, Date1);
    }
  }


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

Answers

  • if this is a continuation of your previous parking lot problem (which it is) then please try and keep it all together.

    there are currently 3 different people asking about the parking lot problem, it gets confusing if they all have multiple threads.

Sign In or Register to comment.