Browse Source

HW<11> done

Levshin95 2 years ago
parent
commit
29058896d7

BIN
HW011/images/chips.jpg


BIN
HW011/images/chok.jpg


BIN
HW011/images/cola.jpg


BIN
HW011/images/kassa.jpeg


BIN
HW011/images/kesh.jpg


BIN
HW011/images/pivo.jpg


BIN
HW011/images/sigi.jfif


+ 87 - 0
HW011/index.html

@@ -0,0 +1,87 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+
+    <div class="wrapper">
+        <select id="product">
+            <option value="пиво" id="pivo1">Пиво</option>
+            <option value="кола" id="cola1">Кола</option>
+            <option value="шоколад" id="cola1">Шоколад</option>
+            <option value="чипсы" id="chips1">Чипсы</option>
+            <option value="сиги" id="sigi1">Сигареты</option>
+        </select>
+        <input type="number" id="number" placeholder="Введите колличество">
+        <button id="button">Купить</button>
+    </div>
+    <div class="availability">
+        <h1>Наличие:</h1>
+        <div style="display: flex; justify-content:space-around;">
+
+            <div class="pivo2">
+
+                <img src="images/pivo.jpg" alt="" style="max-height: 200px; max-width: 200px">
+                <h2 style="text-align: center">Пиво: <span id="pivo"></span></h2>
+                <h2 style="text-align: center">Цена: <span id="pivoK"></span></h2>
+
+                
+            </div>
+
+            <div class="cola2">
+
+                <img src="images/cola.jpg" alt="" style="max-height: 200px; max-width: 200px">
+                <h2 style="text-align: center">Кола: <span id="cola"></span></h2>
+                <h2 style="text-align: center">Цена: <span id="colaK"></span></h2>
+                
+            </div>
+
+            <div class="chok2">
+
+                <img src="images/chok.jpg" alt="" style="max-height: 200px;">
+                <h2 style="text-align: center">Шоколад: <span id="chok"></span></h2>
+                <h2 style="text-align: center">Цена: <span id="chokK"></span></h2>
+                
+            </div>
+
+            <div class="chips2">
+
+                <img src="images/chips.jpg" alt="" style="max-height: 200px; max-width: 200px">
+                <h2 style="text-align: center">Чипсы: <span id="chips"></span></h2>
+                <h2 style="text-align: center">Цена: <span id="chipsK"></span></h2>
+                
+            </div>
+
+            <div class="sigi2">
+
+                <img src="images/sigi.jfif" alt="" style="max-height: 200px; max-width: 200px">
+                <h2 style="text-align: center">Сигареты: <span id="sigi"></span></h2>
+                <h2 style="text-align: center">Цена: <span id="sigiK"></span></h2>
+                
+            </div>
+
+        </div>
+    </div>
+    <hr>
+    <div style="display: flex; justify-content:space-between;">
+
+        <div class="cashbox">
+
+            <h2>Касса: <span id="kassa"></span></h2>
+            <img src="images/kassa.jpeg" alt="" style="max-height: 200px; max-width: 400px">
+            
+        </div>
+        <div class="wallet">
+
+            <h2>Остаток средств: <span id="money"></span></h2>
+            <img src="images/kesh.jpg" alt="" style="max-height: 200px; max-width: 400px">
+            
+        </div>
+        <script src="index.js"></script>
+    </div>
+</body>
+</html>

+ 81 - 0
HW011/index.js

@@ -0,0 +1,81 @@
+function reducer(state, {type, ШО, СКОКА}){ 
+    
+    if (!state){ 
+        return {
+            пиво: {number : 300, price : 40},
+            чипсы: {number : 50, price : 30},
+            сиги: {number : 100, price : 70},
+            кола: {number: 80, price: 20},
+            шоколад: {number: 30, price: 25},
+            бабло: {cashbox : 0, mycash : 2000}
+        }
+    }
+    if (type === 'КУПИТЬ' && (state[ШО].number) >= 0 && state.бабло.mycash - state[ШО].price * СКОКА >= 0) {
+        return {
+            ...state, 
+            [ШО]: {...state[ШО], number: state[ШО].number - СКОКА},
+            бабло: {
+                mycash: state.бабло.mycash - state[ШО].price * СКОКА,
+                cashbox: state.бабло.cashbox + state[ШО].price * СКОКА
+            }
+        }
+    }
+    return state 
+}
+
+function createStore(reducer){
+    let state       = reducer(undefined, {})  
+    let cbs         = []                     
+    
+    const getState  = () => state            
+    const subscribe = cb => (cbs.push(cb),   
+                             () => cbs = cbs.filter(c => c !== cb)) 
+                             
+    const dispatch  = action => { 
+        const newState = reducer(state, action) 
+        if (newState !== state){ 
+            state = newState  
+            for (let cb of cbs)  cb() 
+        }
+    }
+    
+    return {
+        getState, 
+        dispatch,
+        subscribe 
+    }
+}
+
+const store = createStore(reducer)
+console.log(store.getState())
+
+let productN = document.getElementById("product");
+let numberN = document.getElementById("number");
+let buttonN = document.getElementById("button");
+
+let pivoN = document.getElementById("pivo").innerHTML= store.getState().пиво.number + ' шт'
+let chipsN = document.getElementById("chips").innerHTML= store.getState().чипсы.number + ' шт'
+let sigiN = document.getElementById("sigi").innerHTML= store.getState().сиги.number + ' шт'
+let colaN = document.getElementById("cola").innerHTML= store.getState().кола.number + ' шт'
+let chokN = document.getElementById("chok").innerHTML= store.getState().шоколад.number + ' шт'
+
+let pivoM = document.getElementById("pivoK").innerHTML= store.getState().пиво.price + ' грн'
+let chipsM = document.getElementById("chipsK").innerHTML= store.getState().чипсы.price + ' грн'
+let sigiM = document.getElementById("sigiK").innerHTML= store.getState().сиги.price + ' грн'
+let colaM = document.getElementById("colaK").innerHTML= store.getState().кола.price + ' грн'
+let chokM = document.getElementById("chokK").innerHTML= store.getState().шоколад.price + ' грн'
+
+let kassaN = document.getElementById("kassa").innerHTML= store.getState().бабло.cashbox + ' грн'
+let moneyN = document.getElementById("money").innerHTML= store.getState().бабло.mycash + ' грн'
+
+buttonN.onclick = () => {
+    store.dispatch({type: 'КУПИТЬ', ШО: productN.value, СКОКА: numberN.value})
+    let pivoN = document.getElementById("pivo").innerHTML= store.getState().пиво.number + ' шт'
+    let chipsN = document.getElementById("chips").innerHTML= store.getState().чипсы.number + ' шт'
+    let sigiN = document.getElementById("sigi").innerHTML= store.getState().сиги.number + ' шт'
+    let colaN = document.getElementById("cola").innerHTML= store.getState().кола.number + ' шт'
+    let chokN = document.getElementById("chok").innerHTML= store.getState().шоколад.number + ' шт'
+    let kassaN = document.getElementById("kassa").innerHTML= store.getState().бабло.cashbox + ' грн'
+    let moneyN = document.getElementById("money").innerHTML= store.getState().бабло.mycash + ' грн'
+}
+