<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@*" data-semver="2.2.0" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>

</head>
<body>
<script>
function sellingmachine(){ //기계 Object
    this.bobpaper = 1000; //식권종이
    //---------------------------------------------------------
    this.ch100 = 10; //거스름돈100원 
    this.ch1000 = 10; //거스름돈1000원
    //---------------------------------------------------------
    this.cs100 = 0; //투입지폐100원 
    this.cs500 = 0; //투입지폐500원 
    this.cs1000 = 0; //투입지폐1000원 
    this.cs5000 = 0; //투입지폐5000원 
    this.cs10000 = 0; //투입지폐10000원
    //---------------------------------------------------------
    this.name = ["정식", "라면", "김밥", "우동"]; //메뉴
    this.price = [3000, 2500, 1800 ,3500]; ///메뉴가격 
    this.cout = [0,0,0,0]; // 구매수량 
    this.price2 = [0,0,0,0]; // 수량*가격합계
    this.menuArr = []; //상품출력배열
    //---------------------------------------------------------
    this.sumprice = 0; //전체상품합계금액
    this.summoney = 0; //투입금합계금액 
    this.change = 0; //거스름돈합계
   //---------------------------------------------------------
    this.profits = 0; //수익금 
    this.cscout = [0,0,0,0,0]; // 투입지폐 수량관리
    this.cout2 = [0,0,0,0]; // 구매수량관리 
    
   //-----------------------------------------------------------------------------------------------------------
    this.screen = function(x){ //스크린에 출력 
    if(this.bobpaper>0){
      if(this.ch1000<=0 || this.ch100<=0){
       alert("거스름돈이 없습니다. 관리자에게 문의하세요\n010-3933-2444 관리자");
      }
      else{
      if(this.cout[x]<10){
      this.sumprice += this.price[x]; //지출총액을 계산 
      document.querySelector('#input2').setAttribute('value', this.sumprice); //지출총액을 출력
      this.cout[x]++;
      this.price2[x] += this.price[x];
      this.menuArr[x] = this.name[x]+" "+this.cout[x]+" "+this.price2[x]; 
      document.getElementById("screen").innerHTML = this.menuArr.join("<br><br>"); //메뉴 수량 총액 출력 
      }
      
      else{
       setTimeout(function(){document.getElementById("result").innerHTML="최대구매수량을 초과하였습니다."; });
       setTimeout(function(){document.getElementById("result").innerHTML=""; }, 3000);
       }
     }
    }
    else{
       alert("인쇄용지가 없습니다. 관리자에게 문의하세요\n010-3933-2444 관리자");
     }
    }
    
   //-----------------------------------------------------------------------------------------------------------
    this.money = function(x){
      if(this.summoney<this.sumprice){
      this.summoney += Number(x.querySelector('span').innerHTML);
      if(Number(x.querySelector('span').innerHTML)==100){ //수익금 지폐 관리 
       this.cs100 += 1;  
      }
      else if(Number(x.querySelector('span').innerHTML)==500){
       this.cs500 += 1;  
      } 
      else if(Number(x.querySelector('span').innerHTML)==1000){
       this.cs1000 += 1;  
      } 
      else if(Number(x.querySelector('span').innerHTML)==5000){
       this.cs5000 += 1;  
      } 
      else{
       this.cs10000 += 1;  
      }
      document.querySelector('#input1').setAttribute('value', this.summoney);
      }
      else{
        if(this.sumprice==0){
      document.getElementById("result").innerHTML = "반환액:" + " "+Number(x.querySelector('span').innerHTML);
      setTimeout(function(){document.getElementById("result").innerHTML="메뉴를 먼저 선택해주세요"; }, 2000);
      setTimeout(function(){document.getElementById("result").innerHTML=""; }, 4000);
       }
        else{
      document.getElementById("result").innerHTML = "반환액:" + " "+Number(x.querySelector('span').innerHTML);
      setTimeout(function(){document.getElementById("result").innerHTML="투입액을 초과하였습니다."; }, 2000);
      setTimeout(function(){document.getElementById("result").innerHTML=""; }, 4000);
      }
     }
    }
    
   //-----------------------------------------------------------------------------------------------------------
    this.cancel = function(){ //모든값 초기화 & 모든출력값 지우기 
    this.cout = [0,0,0,0]; 
    this.price2 = [0,0,0,0];
    this.menuArr = []
     if(this.summoney>0){
     this.cs100 = 0; //투입지폐초기화
     this.cs500 = 0; //투입지폐초기화
     this.cs1000 = 0; //투입지폐초기화
     this.cs5000 = 0; //투입지폐초기화
     this.cs10000 = 0; //투입지폐초기화
     document.getElementById("result").innerHTML = "반환액:" + " "+this.summoney;   
     setTimeout(function(){document.getElementById("result").innerHTML=""; }, 4000);
     }
    this.sumprice = 0; //합계금액 초기화
    this.summoney =0; //투입액 초기화 
    document.querySelector('#input2').setAttribute('value', ""); //합계금액을 없앤다 
    document.querySelector('#input1').setAttribute('value', ""); //투입액을 없앤다
    document.getElementById("screen").innerHTML = ""; // 스크린을 깨끗히 한다. 
    }
    
    //-----------------------------------------------------------------------------------------------------------
    this.calculate = function(){
      this.change = this.summoney-this.sumprice;
    if(this.bobpaper>0){
     if(this.bobpaper-(this.cout[0]+this.cout[1]+this.cout[2]+this.cout[3])<0){ //식권수량 초과구매시
       alert("인쇄용지가 부족합니다 \n 발매가능갯수:"+this.bobpaper);
     }
     
     else{
      if(this.change == 0){
       this.cscout[0] += this.cs100;
       this.cscout[1] += this.cs500; 
       this.cscout[2] += this.cs1000;
       this.cscout[3] += this.cs5000; 
       this.cscout[4] += this.cs10000;
       this.profits += this.sumprice; //수익금합계 
         for(var k=0; k<4; k++){ //구매수량관리 
          this.cout2[k] = this.cout[k];
         }
      //----------------------------------------------------------------------------//
         document.querySelector("#result").innerHTML = "감사합니다."; 
         setTimeout(function(){document.getElementById("result").innerHTML=""; }, 4000);
         this.bobpaper -=(this.cout2[0]+this.cout2[1]+this.cout2[2]+this.cout2[3]);//식권수량관리 
         
         if(this.bobpaper==0){
         alert("식권수량이 다떨어졌습니다");
         }
         this.cout = [0,0,0,0]; 
         this.price2 = [0,0,0,0];
         this.menuArr = []; //상품출력 초기화
         this.sumprice = 0; //합계금액 초기화
         this.summoney =0; //투입액 초기화 
         document.querySelector('#input2').setAttribute('value', ""); //합계금액을 없앤다 
         document.querySelector('#input1').setAttribute('value', ""); //투입액을 없앤다
         document.getElementById("screen").innerHTML = ""; // 스크린을 깨끗히 한다.
      }
      else if(this.change>0){
         if(this.ch1000 - parseInt(this.change/1000)>=0 && this.ch100-parseInt((this.change%1000)/100)>=0){ //거스름돈이 있을때 
         this.cscout[0] += this.cs100;
         this.cscout[1] += this.cs500; 
         this.cscout[2] += this.cs1000;
         this.cscout[3] += this.cs5000; 
         this.cscout[4] += this.cs10000;
         this.profits += this.sumprice; //수익금합계 
        
         for(var k=0; k<4; k++){ //구매수량관리 
          this.cout2[k] = this.cout[k];
         }
        
         //거스름돈 계산 
         this.ch1000 -= parseInt(this.change/1000); 
         this.ch100 -= parseInt((this.change%1000)/100);
         if(this.ch1000<=0 && this.ch100<=0){
          alert("거스름돈이 없습니다."); 
         }
        //----------------------------------------------------------------------------//
         document.querySelector("#result").innerHTML = "거스름돈:"+" "+this.change;
         setTimeout(function(){document.getElementById("result").innerHTML=""; }, 4000); 
        
         this.bobpaper -=(this.cout2[0]+this.cout2[1]+this.cout2[2]+this.cout2[3]);//식권수량관리 
         if(this.bobpaper==0){
         alert("인쇄용지가 부족합니다 \n 발매가능갯수:"+this.bobpaper);
         }
         
         this.cout = [0,0,0,0]; 
         this.price2 = [0,0,0,0]; 
         this.menuArr = []; //상품출력 초기화
         this.sumprice = 0; //합계금액 초기화
         this.summoney =0; //투입액 초기화 
         document.querySelector('#input2').setAttribute('value', ""); //합계금액을 없앤다 
         document.querySelector('#input1').setAttribute('value', ""); //투입액을 없앤다
         document.getElementById("screen").innerHTML = ""; // 스크린을 깨끗히 한다.
        }
        
        else{
          alert("거스름돈이 없습니다. 관리자에게 문의하세요\n010-3933-2444 관리자");
        }
      }
       
      else{
        document.querySelector("#result").innerHTML = "입금액이 부족합니다."; //입금액 부족 출력 
        setTimeout(function(){document.getElementById("result").innerHTML=""; }, 4000);
       }
      }
    }
    else{
       alert("인쇄용지가 없습니다. 관리자에게 문의하세요");
    }
    this.cs100 = 0; //투입지폐초기화
    this.cs500 = 0; //투입지폐초기화
    this.cs1000 = 0; //투입지폐초기화
    this.cs5000 = 0; //투입지폐초기화
    this.cs10000 = 0; //투입지폐초기화
    }
    
    //-----------------------------------------------------------------------------------------------------------
    this.colsing = function(){
      document.getElementById("screen").innerHTML ="10000원"+" "+"수량:"+this.cscout[4]+" "+"합계:"+this.cscout[4]*10000+"원"+"<br>"
      +" 5000원"+" "+"수량:"+this.cscout[3]+" "+"합계:"+this.cscout[3]*5000+"원"+"<br>"
      +" 1000원"+" "+"수량:"+this.cscout[2]+" "+"합계:"+this.cscout[2]*1000+"원"+"<br>"
      +"  500원"+" "+"수량:"+this.cscout[1]+" "+"합계:"+this.cscout[1]*500+"원"+"<br>"
      +"  100원"+" "+"수량:"+this.cscout[0]+" "+"합계:"+this.cscout[0]*100+"원"+"<br>"
      +"------------------------------------------------------<br>"
      +"총 식권 판매량"+" "+(this.cout2[0]+this.cout2[1]+this.cout2[2]+this.cout2[3])+"장"+"<br>"
      +"정식"+" "+"수량:"+this.cout2[0]+" "+"합계:"+this.cout2[0]*3000+"원"+"<br>"
      +"라면"+" "+"수량:"+this.cout2[1]+" "+"합계:"+this.cout2[1]*2500+"원"+"<br>"
      +"김밥"+" "+"수량:"+this.cout2[2]+" "+"합계:"+this.cout2[2]*1800+"원"+"<br>"
      +"우동"+" "+"수량:"+this.cout2[3]+" "+"합계:"+this.cout2[3]*3500+"원"+"<br>"
      +"총수익금:"+" "+(this.cout2[0]*3000+this.cout2[1]*2500+this.cout2[2]*1800+this.cout2[3]*3500)+"원"+"<br>"
      +"----------------------------------------------------------<br>"
      +"현금시재<br>"
      +"  100원"+" "+"수량:"+this.ch100+"<br>"//거스름돈100원 
      +"1000원"+" "+"수량:"+this.ch1000//거스름돈1000원
      +"<br>----------------------------------------------------------<br>"
      +"남은식권수량:"+this.bobpaper+"<br>";
    
    }
}
var Food = new sellingmachine();

</script>
<button onclick="Food.screen(0)">정식 3000</button>
<button onclick="Food.screen(1)">라면 2500</button>
<button onclick="Food.screen(2)">김밥 1800</button>
<button onclick="Food.screen(3)">우동 3500</button><br><br>
<button onclick="Food.colsing()">마감</button><br><br>
<div id= "screen"></div><br>
<input id="input1" name="money" value="" placeholder="투입액">
<input id="input2"  value="" placeholder="합계금액" >
<input type="button" value="확인" onclick="Food.calculate(this.form)">
<input type="button" value="취소" onclick="Food.cancel()">
<div id="result"></div><br>

<button class="chmoney" onclick="Food.money(this)"><span>100</span></button>
<button class="chmoney" onclick="Food.money(this)"><span>500</span></button>
<button class="chmoney" onclick="Food.money(this)"><span>1000</span></button>
<button class="chmoney" onclick="Food.money(this)"><span>5000</span></button>
<button class="chmoney" onclick="Food.money(this)"><span>10000</span></button>
</body>
</html>
// Code goes here

    #result{
    border:2px solid black;
    line-height:50px;
    text-align:center;
    margin-top:10px;
    width :400px;
    height:50px;
    background:cyan;
    }
    
    #screen{
      width :450px;
      height:400px;
      border-style: solid;
      font-size: 15px;
    }
    .but1{
    width :200px;
    height:60px;
    }
    
    #input1{
      width :150px;
    }
    
    #input2{
      width :150px;
    }
    
    .chmoney{
     width :100px;
     height:20px;
    }