วันอังคารที่ 20 ตุลาคม พ.ศ. 2558

v1.0

/*vending machine value*/
int insert=0; //1->Select Product & Input money | 2->coin[0]>Buy & Can Buy now | 3->Press 'c' to clear
int buy=0; // total buy
int[] coin={0, 100, 100, 100, 0, 0, 0, 0, 0, 0} ;//0 total input value |1-3 remain | 4-6 input | 7-9 change
int[] n= {30, 30, 30, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //0-4 remain | 5-9 get | 10-14 sold
int[] price={12, 20, 7, 15, 10}; //pepi orage watr tea mlk
/*animated*/
int posY=90, dy=80, c=0; //control line
int xin=0; //animated skewline
float show=-1, dx=0;//animated coin
int s=0; //coin Y position
float light_opacity=50, plusl=0.25;
/*admin value*/
String trans=""; // HISTORY detail
String timeBF=""; //checking time if same then do not add time
int count=0;
boolean admin=true;
int action=0; //'1' select action '2'insert or remove
String info="Select action by button";
int frameclick, frameZ=0, reset=0; //animated 1 second button & Z
int allzero=0;
void setup() {
  size(350, 520);
  textSize(15);
}
void draw() {
  if (n[0]==0 && n[1]==0 && n[2]==0 && n[3]==0 && n[4]==0 && insert==0)
    allzero=1;
  else
    allzero=0;
  if (admin==true) {
    background(51);
    admin_panel();
  } else {
    ven_img();
    coin[0]=coin[4]+coin[5]*5+coin[6]*10; //calc money u have
    if (allzero==0) {
      if (insert==1 || insert==2) {
        /*TEXT ZONE*/
        String textcoin="Coin 1(1):"+coin[4]+" \nCoin 5(2):"+coin[5]+" \nCoin 10(3):"+coin[6]+" \n= "+coin[0]+" Baht";
        if (reset==0) {
          show-=0.05;
          dx+=0.5;
          if (show<=-1) {
            show=-1;
            dx=0;
          }
          if (show>-1) {
            draw_coin(100+dx, 428+s, map(show, 1, -1, 255, 0));
          }
          fill(255);
          text("ENTER COIN", 20, 410);
          fill(#78BC80);
          text(textcoin, 20, 428);
          // show text total buy
          if (buy>0) { //buy > 0
            fill(#C6D842);
            text("Total : "+buy+".-", 130, 410);
            fill(#FF5858);
            text("Press Z to reset coin and item", 130, 450);
            insert=2;
            //================show "YOU CAN BUY" ++ ================
            if (coin[0]>=buy) {
              fill(#F6FF08);
              text("You can BUY by press ENTER", 130, 430);
            }
          }
        }
        if (reset==1) {
          fill(#f02e6e);
          text("Your Change : ", 20, 410);
          text(textcoin, 20, 428);
          if (frameCount-frameZ==60) {
            reset_CBGET();
            reset=0;
            insert=1;
          }
        }
      } else if (insert==3) { //calc Change and show what you get
        fill(#929292);
        text("Total : "+buy+".-", 90, 430);
        fill(#7c852b);
        text("Your money : "+coin[0]+".-", 40, 410);
        fill(#F6FF08);
        text("Change : "+(coin[0]-buy)+".- "+calc_coin(coin[0]-buy), 199, 410);
        fill(#C6D842);
        text("Press 'C' to clear", 119, 512);
      }
    } else if (allzero==1) {
      fill(#FC666B);
      text("PLEASE CONTACT OWNER - OUT OF STOCK", 20, 450);
    }
  }
}

//==================================================INTERACTIVE AND INPUT======================================================================//
void mousePressed() {
  if (mouseButton==LEFT) {
    if (mouseX>=220 && mouseX<=340 && mouseY>=368 && mouseY<=388 && insert==0) { //can enter admin only insert==0
      if (keyPressed && (key=='X' || key=='x')) {
        admin=true;
      }
    }
    //admin click quit & action//
    if (admin==true) {
      if (mouseX>=20 && mouseX<=70 && mouseY>=20 && mouseY<=40 && action==0) {
        admin=false;
      }
      if (mouseX>=280 && mouseX<=340 && mouseY>=20 && mouseY<=40 && action==0) {
        action=1;
      }
      if (mouseX>=20 && mouseX<=70 && mouseY>=20 && mouseY<=40 && action==1) {
        action=0;
      }
      if (mouseX>=50 && mouseX<=300 && mouseY>=170 && mouseY<=250 && action==1) { //RESET MACHINE
        info="Reset Machine Completed !";
        frameclick=frameCount;
        reset();
      }
      if (mouseX>=50 && mouseX<=300 && mouseY>=270 && mouseY<=350 && action==1) { //RESET SIMULATOR
        info="Reset Simulator Completed !";
        frameclick=frameCount;
        reset();
        count=0;
        for (int i=10; i<=14; i++) {
          n[i]=0;
        }
        trans="";
      }
      if (mouseX>=50 && mouseX<=300 && mouseY>=70 && mouseY<=150 && action==1) { //GO TO action2 = INSERT/REMOVE COINS PRODUCTS
        action=2;
      }
      if (action==2) {
        if (mouseX>=20 && mouseX<=70 && mouseY>=20 && mouseY<=40) { //back to action1
          action=1;
        }
        for (int i=0; i<5; i++) { //add
          if (mouseX>=42+60*i && mouseX<=57+60*i && mouseY>=59 && mouseY<=74) {
            if (n[i]<30)
              n[i]+=1;
          }
        }
        for (int i=0; i<5; i++) { //minus
          if (mouseX>=42+60*i && mouseX<=57+60*i && mouseY>=123 && mouseY<=136) {
            if (n[i]>0)
              n[i]-=1;
          }
        }
      }
    }
    //buying zone//
    if ((insert==0 || insert==1) && allzero==0) {
      if (mouseX>=10 && mouseX<=130 && mouseY>=368 && mouseY<=388) {
        if (insert==0) {
          insert=1;
        } else if (insert==1) {
          insert=0;
          show=-1; //resetcoin ani
          reset_CBGET(); // reset coin
        }
      }
    }
    if (insert==1 || insert==2) {
      for (int i=0; i<5; i++) {
        if (mouseX>=315 && mouseX<=335 && mouseY>=60+60*i && mouseY<=80+60*i && n[i]>0) {
          buy+=price[i];
          n[i+5]+=1;
          n[i]--;
        }
      }
    }
    //end buying zone//
  }
}
void mouseDragged() {
  if (mouseX>=169 && mouseX<=183 && mouseY>=71 && mouseY<=229) {
    dy=mouseY;
    if (dy>=230)dy=230;
    if (dy<=80)dy=80;
    posY=int(map(dy, 80, 230, 90, -c));
  }
}
void keyPressed() {
  if (insert==1 || insert==2 && reset==0) {
    if (key=='1') {
      coin[4]+=1;
      show*=-1;
      s=0;
      dx=0;
    }
    if (key=='2') {
      coin[5]+=1;
      show*=-1;
      s=20;
      dx=0;
    }
    if (key=='3') {
      coin[6]+=1;
      show*=-1;
      s=40;
      dx=0;
    }
  }
  if (insert==2) {
    if (key=='Z' || key=='z') {
      for (int i=0; i<=4; i++) { //reset number of pieces [by add nget to nremain]
        n[i]+=n[i+5];
      }
      frameZ=frameCount;
      reset=1;
    }
    if (keyCode==ENTER && coin[0]>=buy) {
      for (int i=5; i<=9; i++) {      //add sold product
        n[i+5]+=n[i];
      }
      //add coin to remain coin
      coin[1]+=coin[4];
      coin[2]+=coin[5];
      coin[3]+=coin[6];
      count++;
      String timenow=nf(hour(), 2)+":"+nf(minute(), 2); //check timebefore
      if (timenow.equals(timeBF)==false) {
        trans+="Time : "+timenow+"\n"  ;
        timeBF=timenow;
      }
      c+=25;// scrolling line
      println(c);
      trans+="+";
      //check and add to history
      if (n[5]>0)trans+="Px"+n[5]+" ";
      if (n[6]>0)trans+="Ox"+n[6]+" ";
      if (n[7]>0)trans+="Wx"+n[7]+" ";
      if (n[8]>0)trans+="Tx"+n[8]+" ";
      if (n[9]>0)trans+="Mx"+n[9]+" ";
      trans+="\n";
      insert=3;
      show=-1;
    }
  }
  if (insert==3) {
    if (key=='c' || key=='C') {
      //dec coin from changing
      for (int i=1; i<=3; i++) {
        coin[i]-=coin[i+6];
        coin[i+6]=0;
      }
      reset_CBGET();
      insert=0;
    }
  }
  if (action==2 && admin==true) {
    if (key=='+') {
      if (mouseX>=61 && mouseX<=121 && mouseY>=207 && mouseY<=267) {
        if (coin[3]<1000)
          coin[3]++;
      }
      if (mouseX>=146 && mouseX<=197 && mouseY>=212 && mouseY<=263) {
        if (coin[2]<1000)
          coin[2]++;
      }
      if (mouseX>=215 && mouseX<=258 && mouseY>=218 && mouseY<=258) {
        if (coin[1]<1000)
          coin[1]++;
      }
    } else if (key=='-') {
      if (mouseX>=61 && mouseX<=121 && mouseY>=207 && mouseY<=267) {
        if (coin[3]>0)
          coin[3]--;
      }
      if (mouseX>=146 && mouseX<=197 && mouseY>=212 && mouseY<=263) {
        if (coin[2]>0)
          coin[2]--;
      }
      if (mouseX>=215 && mouseX<=258 && mouseY>=218 && mouseY<=258) {
        if (coin[1]>0)
          coin[1]--;
      }
    }
  }
}
//==================================================END INTERACTIVE AND INPUT======================================================================//
// draw vending machine and product
void ven_img() {
  fill(31);
  rect(0, 0, width, height);
  fill(51);
  rect(0, 0, width-20, height-170);//inner black
  /*==== button ====*/
  fill(0);
  rect(13, 372, 120, 20);
  rect(223, 372, 120, 20);
  fill(#C18585);
  rect(10, 368, 120, 20); //pink rect [end =130]
  fill(#FF812C);
  rect(220, 368, 120, 20); //yellow rect [end =340]
  fill(255);
  text("BUY", 20, 385);
  text("ADMIN", 230, 385);
  /*====draw goods zone====*/
  for (int x=0; x<n[0]; x++) {
    fill(0);
    ellipse(20+x*40, 65, 35, 35);
    pepsi(20+x*40, 60, 35, 35); //pepsi
  }
  for (int x=0; x<n[1]; x++) {
    fill(0);
    ellipse(20+x*40, 125, 35, 35);
    orange(20+x*40, 120, 35, 35); //orange
  }
  for (int x=0; x<n[2]; x++) {
    fill(0);
    ellipse(20+x*40, 190, 35, 35);
    water(20+x*40, 180, 35, 35); //water
  }
  for (int x=0; x<n[3]; x++) {
    fill(0);
    ellipse(20+x*40, 250, 35, 35);
    tea(20+x*40, 240, 35, 35); //tea
  }
  for (int x=0; x<n[4]; x++) {
    fill(0);
    ellipse(20+x*40, 310, 35, 35);
    milk(20+x*40, 300, 35, 35); //milk
  }
  /*end draw goods*/
  if (insert>=1) { //light animated
    if (light_opacity==25 || light_opacity==50)plusl*=-1;
    light_opacity+=plusl;
    for (int i=16; i<46; i++) { //light
      stroke(#39A52B, map(i, 16, 45, light_opacity, 0)); //<change_lightcolor>
      line(0, i, width, i);
      line(0, 364-i, width, 364-i);
    }
  }
  fill(20);
  rect(300, 0, 50, height-170);//right tab
  /*======== Draw button ==========*/
  for (int i=1; i<=5; i++) {
    fill(#F2315E);
    rect(315, 60*i, 20, 20);
  }
  /*=========Show prices and show green color============*/
  for (int i=0; i<=4; i++) {
    if (n[i]>0) {
      fill(#81FF97);
      rect(315, 60*(i+1), 20, 20);
      if (n[i]>0)
        text(price[i]+".-", 315, 50+60*i);
    }
  }
  fill(33);
  rect(0, 0, width, 33);// Title Vending Machine & Animated
  skew_line(xin, 30, 20, 30, 50, 1);
  textSize(25);
  fill(0);
  text("Drinking Water", 93, 25);
  fill(#47EAE9);
  text("Drinking Water", 90, 22);
  textSize(15);
}
// ======================================= Drawing Function =====================================//
void pepsi(int x, int y, int w, int h) {
  noStroke();
  fill(#F01D44);
  ellipse(x, y, w, h);
  fill(#1A73FF);
  arc(x, y, w, h, radians(-20), radians(160));
  stroke(255);
  noFill();
  strokeWeight(3);
  bezier(x-w*0.47, y, x-w*0.4, y+h*0.5, x+w*0.4, y-h*0.5, x+w*0.47, y);
  noStroke();
}

void orange(int x, int y, int w, int h) {
  fill(#FCC80A);
  ellipse(x, y, w, h);
  fill(255);
  ellipse(x, y, w-5, h-5);
  fill(#FCC80A);
  ellipse(x, y, w-10, h-10);
}

void water(int x, int y, int w, int h) {
  fill(#47EAE9);
  noStroke();
  ellipse(x, y+h*0.159, w, h);
  triangle(x, y-h*0.5, x+w*0.49, y, x-w*0.49, y);
}

void tea(int x, int y, int w, int h) {
  String tea="tea";
  fill(#2AB434);
  noStroke();
  ellipse(x, y+h*0.159, w, h);
  triangle(x, y-h*0.5, x+w*0.49, y, x-w*0.49, y);
  fill(255);
  text(tea, x-textWidth(tea)/2, y+h*0.3);
}
void milk(int x, int y, int w, int h) {
  String tea="milk";
  fill(255);
  noStroke();
  ellipse(x, y+h*0.159, w, h);
  triangle(x, y-h*0.5, x+w*0.49, y, x-w*0.49, y);
  fill(#5067D6);
  text(tea, x-textWidth(tea)/2, y+h*0.3);
}
void plus(float x, float y, int sizebox) {
  noStroke();
  fill(#9CFC82);
  rect(x, y, sizebox*3, sizebox*0.8); //x
  rect(x+sizebox*1.1, y-sizebox*1.1, sizebox*0.8, sizebox*3); //y
}

void minus(float x, float y, int sizebox) {
  noStroke();
  fill(255, 0, 0);
  rect(x, y, sizebox*3, sizebox*0.8);
}
void skew_line(int x, int y, int w, int h, int n, int dx) {
  noStroke();
  fill(#F52A67, 50);
  xin+=dx;
  if (xin>=20)xin=-100;
  for (int i=0; i<n; i++) {
    quad(x+(i*w*2), y, x+w+(i*w*2), y, x+w*2+(i*w*2), y-h, x+w+(i*w*2), y-h);
  }
}
// ======================================= END Drawing Function =====================================//
String calc_coin(int n) { //show change in coins and - coins [coin7-9 for change only]
  String coins="";
  if (coin[3]>0) {
    coin[9]=int(n/10);
    if ( coin[9]>coin[3]) coin[9]=coin[3];
    n=n-( coin[9]*10);
  }
  if (coin[2]>0) {
    coin[8]=int(n/5);
    if (coin[8]>coin[2])coin[8]=coin[2];
    n=n-(coin[8]*5);
  }
  if (coin[1]>0) {
    coin[7]=n;
    if (coin[7]>coin[1])coin[7]=coin[1];
  }
  if (coin[9]>0)    coins+="\nCoin10="+coin[9]+" ";
  if (coin[8]>0)   coins+="\nCoin5="+coin[8]+" ";
  if (coin[7]>0)    coins+="\nCoin1="+coin[7]+" " ;
  if (coin[7]==0 && coin[8]==0 && coin[9]==0) {  // no coin and no change //coin1 5 10 for change
    if (coin[0]!=buy)
      coins="\nSorry no coin left.";
    else
      coins="\nNo Change";
  }
  return coins;
}

// ============================================================= ADMIN ======================================================//
void admin_panel() {
  if (action!=0) {
    fill(33);
    rect(0, 15, width, 33);// Animated skew line
    skew_line(xin, 45, 20, 30, 50, 1);
  }
  if (action==0) {
    fill(30);
    rect(185, 55, 155, 450); //box right
    rect(0,73,182,444); // box left
    /* =================== remaining ============== */
    fill(#FA0A42);
    text("Remaining Product", 190, 360);
    String remain="Pepsi(P) : "+n[0]+"\nOrange Juice(O) : "+n[1]+"\nWater(W) : "+n[2]+"\nTea(T) : "+n[3]+"\nMilk(M) : "+n[4];
    fill(#94FCB4);
    text(remain, 190, 380);
    /* ===================== sold ================= */
    fill(#FF9100);
    text("Sold Product", 190, 190);
    String sold="Pepsi : "+n[10]+"["+(n[10]*12)+"]"+
      "\nOrange Juice : "+n[11]+"["+(n[11]*20)+"]"+
      "\nWater : "+n[12]+"["+(n[12]*7)+"]"+
      "\nTea : "+n[13]+"["+(n[13]*15)+"]"+
      "\nMilk : "+n[14]+"["+(n[14]*10)+"]";
    fill(#94FCB4);
    text(sold, 190, 210);
    /* ===================== coin ================= */
    fill(#35B45C);
    text("Remaining Coins", 190, 70);
    String coins="One : "+coin[1]+"\nFive : "+coin[2]+"\nTen : "+coin[3];
    fill(#94FCB4);
    text(coins, 190, 90);
    fill(#FFC16F);
    text("Value : "+(coin[1]+coin[2]*5+coin[3]*10)+" Baht", 190, 155);
    text("Sold : "+(n[10]*12+n[11]*20+n[12]*7+n[13]*15+n[14]*10)+" Baht", 190, 325); //sold value
    /* ===================== history ================= */
    fill(120);
    text(trans, 5, posY);
    fill(51);
    rect(0, 0, 183, 70);
    fill(#FCAB40);
    text("History "+day()+"/"+month()+"/"+year()+"["+count+"]", 5, 70);
    ctrl_line(175, 80, 0, 255);
    /*==============header====================*/
    fill(33);
    rect(0, 15, width, 33);// Animated skew line
    skew_line(xin, 45, 20, 30, 50, 1);
    fill(#6EF798);
    text("ADMIN", 150, 35);
    fill(30);
    rect(20, 20, 50, 20, 8); //button
    rect(280, 20, 60, 20, 8);
    fill(#FFF708);
    text("Quit", 30, 35);
    text("Action", 287, 35);
  } else if (action==1) {
    fill(#ACA8EA);
    text("ADMIN ACTION", 120, 35);
    for (int i=0; i<3; i++) {
      fill(35);
      rect(50, 70+100*i, 250, 80, 8);
    }
    fill(30);
    rect(20, 20, 50, 20, 8);
    fill(#FFF708);
    text("Back", 30, 35);
    fill(#64F77D);
    text("Insert/Remove Product & Coins", 60, 110);
    fill(#FFDA03);
    text("RESET MACHINES", 110, 210);
    text("RESET SIMULATOR", 110, 310);
    fill(#00B9FF);
    if (frameCount-frameclick==30)info="Select action by button";
    text("INFO : "+info, 71, 400);
  } else if (action==2) { // INSERT OR REMOVE PRODUCT & INSERT OR REMOVE COIN
    fill(#ACA8EA);
    text("ADMIN ACTION", 120, 35);
    fill(30);
    rect(20, 20, 50, 20, 8);//back button
    fill(#FFF708);
    text("Back", 30, 35);//back
    fill(100);
    rect(0, 80, width, 40);
    /*====draw goods && +,- buttonin admin zone====*/
    pepsi(50, 100, 35, 35); //pepsi
    orange(110, 100, 35, 35); //orange
    water(170, 95, 35, 35); //water
    tea(230, 95, 35, 35); //tea
    milk(290, 95, 35, 35); //milk
    for (int i=0; i<5; i++) {
      plus(42+i*60, 65, 5);
      minus(42+i*60, 130, 5);
      fill(0);
      rect(35+i*60, 140, 30, 20);
    }
    fill(#F23350);
    for (int i=0; i<5; i++) {
      text(nf(n[i], 2), 40+60*i, 155);
    }
    /*end draw goods*/
    fill(#795A5F);
    ellipse(92, 238, 60, 60);
    ellipse(173, 238, 50, 50);
    ellipse(237, 238, 40, 40);
    fill(255);
    text("10", 82, 243);
    text("5", 168, 243);
    text("1", 232, 243);
    text(nf(coin[3], 3), 78, 295);
    text(nf(coin[2], 3), 159, 295);
    text(nf(coin[1], 3), 222, 295);
    text("How to +/- coin", 88, 354);
    fill(#E395A1);
    text("1.place mouse on coin", 92, 374);
    text("2.press + or - ", 92, 394);
  }
}
void reset() { //reset machine
  allzero=0;
  for (int i=0; i<=4; i++) {
    n[i]=30;
  }
  coin[1]=100;
  coin[2]=100;
  coin[3]=100;
}
void reset_CBGET() { //reset coin & nget & buy 'z'
  coin[4]=0; //coin input reset
  coin[5]=0;
  coin[6]=0;
  buy=0;
  for (int i=5; i<=9; i++) { //reset number you get
    n[i]=0;
  }
}
void draw_coin(float x, int y, float z) {
  noStroke();
  fill(#FC2626, z);
  ellipse(x, y, 15, 15);
  text("+1", x+10, y);
}
void ctrl_line(int x, int y, int index, int col) {
  strokeWeight(3);
  stroke(255, 50);
  line(x, y+150, x, y);
  fill(col);
  ellipse(x, dy, 10, 10);
  noStroke();
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

commit เพิ่มเติม : เพื่ม userstory, bookmark

>>  add userstory ทำการเพิ่ม userstory ใน functional tests >>  add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...