Browse Source

HW<14>begining

Gennadysht 2 năm trước cách đây
mục cha
commit
da4f47d5f8

+ 22 - 0
js/14 FOOP/hw14_01_person_constructor.html

@@ -0,0 +1,22 @@
+<header>Person Constructor</header>
+
+<body>
+    <script>
+        function createPerson(name, surname) {
+            this.name = name;
+            this.surname = surname;
+            this.getFullName = () => {
+                return `${this.name} ${this.surname}`;
+            }
+        }
+        const a = new createPerson("Вася", "Пупкин")
+        const b = new createPerson("Анна", "Иванова")
+        const c = new createPerson("Елизавета", "Петрова")
+
+        console.log(a.getFullName())
+        a.fatherName = 'Иванович'
+
+        console.log(b.getFullName())
+
+    </script>
+</body>

+ 23 - 0
js/14 FOOP/hw14_02_person prototype.html

@@ -0,0 +1,23 @@
+<header>person prototype</header>
+
+<body>
+    <script>
+        function CreatePerson(name, surname) {
+            this.name = name;
+            this.surname = surname;
+        }
+        CreatePerson.prototype.getFullName = function () {
+            return `${this.name} ${this.surname}`;
+        }
+
+        const a = new CreatePerson("Вася", "Пупкин")
+        const b = new CreatePerson("Анна", "Иванова")
+        const c = new CreatePerson("Елизавета", "Петрова")
+
+        console.log(a.getFullName())
+        a.fatherName = 'Иванович'
+
+        console.log(b.getFullName())
+
+    </script>
+</body>

+ 152 - 0
js/14 FOOP/hw14_03_store.html

@@ -0,0 +1,152 @@
+<header>KIOSK</header>
+
+<body>
+    <script>
+
+        const addCell = (row, value) => {
+            let cell = document.createElement("td");
+            cell.innerText = value;
+            row.append(cell);
+            return cell;
+        }
+        const createAssortTable = (store) => {
+            let state = store.getState();
+            let table = document.createElement("table");
+            let row = document.createElement("tr");
+            table.append(row);
+            addCell(row, "NAME");
+            addCell(row, "PRICE");
+            addCell(row, "AMOUNT");
+            for (const itemKey in state) {
+                if (itemKey == "cashbox")
+                    continue;
+                let item = state[itemKey];
+                let row = document.createElement("tr");
+                table.append(row);
+
+                addCell(row, itemKey);
+                addCell(row, item.price);
+                let amountVal = addCell(row, item.amount);
+
+                store.subscribe(() => {
+                    const state = store.getState();
+                    const item = state[itemKey];
+                    const amount = item.amount;
+                    amountVal.innerText = amount;
+                });
+                //store.getState()[itemKey].amount);
+            }
+            return table;
+        }
+        const addLabel = (element, value) => {
+            let label = document.createElement("label");
+            label.htmlFor = element.id;
+            label.innerText = value + ": ";
+            element.parentElement.insertBefore(label, element);
+        }
+        const addBr = (element) => element.append(document.createElement("br"));
+
+        const createSale = (store) => {
+            let state = store.getState();
+            let resDiv = document.createElement("div")
+            let select = document.createElement("select");
+            resDiv.append(select);
+            for (itemKey in state) {
+                if (itemKey == "cashbox")
+                    continue;
+                let option = document.createElement("option");
+                option.value = itemKey;
+                option.text = itemKey;
+                select.append(option);
+            }
+            select.onchange = () => {
+                priceDiv.innerText = state[select.value].price;
+            }
+            addLabel(select, "NAME");
+            addBr(resDiv);
+            let priceDiv = document.createElement("a");
+            priceDiv.innerText = state[select.value].price;
+            priceDiv.id = "priceId";
+
+            resDiv.append(priceDiv);
+            addLabel(priceDiv, "price");
+            addBr(resDiv);
+            let amountInp = document.createElement("input");
+            amountInp.id = "amountId";
+            amountInp.value = 0;
+
+            resDiv.append(amountInp);
+            addLabel(amountInp, "amount");
+            addBr(resDiv);
+            let cashInp = document.createElement("input");
+            cashInp.id = "cashId";
+            cashInp.value = 0;
+            resDiv.append(cashInp);
+            addLabel(cashInp, "cash");
+            addBr(resDiv);
+            let buyBtn = document.createElement("button");
+            buyBtn.innerText = "BUY";
+            buyBtn.onclick = () => {
+                store.dispatch({ type: 'BUY', item: select.value, amount: +amountInp.value, cash: +cashInp.value });
+            }
+            resDiv.append(buyBtn);
+
+            return resDiv;
+        }
+        function CreateStore(reducer) {
+            let state = reducer(undefined, {}) //стартовая инициализация состояния, запуск редьюсера со state === undefined
+            let cbs = []                     //массив подписчиков
+
+            this.getState = () => state            //функция, возвращающая переменную из замыкания
+            this.subscribe = cb => (cbs.push(cb),   //запоминаем подписчиков в массиве
+                () => cbs = cbs.filter(c => c !== cb)) //возвращаем функцию unsubscribe, которая удаляет подписчика из списка
+
+            this.dispatch = action => {
+                const newState = reducer(state, action) //пробуем запустить редьюсер
+                if (newState !== state) { //проверяем, смог ли редьюсер обработать action
+                    state = newState //если смог, то обновляем state 
+                    for (let cb of cbs) cb() //и запускаем подписчиков
+                }
+            }
+        }
+        const seller = (state, { type, item, amount, cash, price }) => {
+            if (state === undefined) {
+                state = {
+                    beer: { amount: 20, price: 10 },
+                    vodka: { amount: 10, price: 30 },
+                    cigars: { amount: 100, price: 20 },
+                    cashbox: 0,
+                }
+            }
+            if (type === 'BUY' &&
+                item in state &&
+                typeof amount == "number" &&
+                typeof cash == "number" &&
+                state[item].amount >= amount &&
+                amount > 0 &&
+                cash > 0 &&
+                state[item].price <= cash / amount) { //если тип action - КУПИТЬ, то:
+
+                return {
+                    ...state, //берем все что было из ассортимента
+                    [item]: { amount: state[item].amount - amount, price: state[item].price },  //и уменьшаем то, что покупается на количество
+                    cashbox: state.cashbox + amount * state[item].price,
+                }
+            }
+
+            return state;
+        }
+        let store = new CreateStore(seller);
+        const unsubscribe = store.subscribe(() => console.log(store.getState()))
+
+
+
+        document.body.append(createAssortTable(store));
+        document.body.append(document.createElement("br"));
+        document.body.append(createSale(store));
+
+    </script>
+</body>
+<footer>
+
+</footer>

+ 50 - 0
js/14 FOOP/hw14_04_password.html

@@ -0,0 +1,50 @@
+<header>password</header>
+
+<body>
+    <script>
+        function Password(parent, open) {
+            this.input = document.createElement("input");
+            parent.append(this.input);
+            //this.input.type = open ? "text" : "password";
+            this.setValue = function (value) {
+                this.input.value = value;
+            }
+            this.getValue = function () {
+                return this.input.value;
+            }
+            this.setOpen = function (open) {
+                this.input.type = open ? "text" : "password";
+            }
+            this.getOpen = function () {
+                return this.input.type == "text";
+            }
+            this.input.onchange = function () {
+                if (this.onChange)
+                    this.onChange(this.getValue());
+            }.bind(this);
+
+
+            let check = document.createElement("input");
+            parent.append(check);
+            check.onchange = function () {
+                this.setOpen(check.checked);
+                if (this.onOpenChange)
+                    this.onOpenChange(this.getOpen());
+            }.bind(this);
+
+            check.checked = open;
+            check.value = "open";
+            check.type = "checkbox";
+        }
+
+        let p = new Password(document.body, true);
+        p.onChange = data => console.log(data)
+        p.onOpenChange = open => console.log(open)
+
+        p.setValue('qwerty')
+        console.log(p.getValue())
+
+        p.setOpen(false)
+        console.log(p.getOpen()) 
+    </script>
+</body>

+ 54 - 0
js/14 FOOP/hw14_05_login_form .html

@@ -0,0 +1,54 @@
+<header>password</header>
+
+<body>
+    <script>
+        function Password(parent, open) {
+            this.input = document.createElement("input");
+            parent.append(this.input);
+            //this.input.type = open ? "text" : "password";
+            this.setValue = function (value) {
+                this.input.value = value;
+            }
+            this.getValue = function () {
+                return this.input.value;
+            }
+            this.setOpen = function (open) {
+                this.input.type = open ? "text" : "password";
+            }
+            this.getOpen = function () {
+                return this.input.type == "text";
+            }
+            this.input.onchange = function () {
+                if (this.onChange)
+                    this.onChange(this.getValue());
+            }.bind(this);
+
+
+            let check = document.createElement("input");
+            parent.append(check);
+            check.onchange = function () {
+                this.setOpen(check.checked);
+                if (this.onOpenChange)
+                    this.onOpenChange(this.getOpen());
+            }.bind(this);
+
+            check.checked = open;
+            check.value = "open";
+            check.type = "checkbox";
+        }
+        let form = document.createElement("form");
+        document.body.append(form);
+        let login = document.createElement("input");
+        form.append(login);
+        let password = new Password(form, true);
+        let button = document.createElement("button");
+        button.innerText = "OK";
+        form.append(button);
+        function setButtonState() {
+            button.disabled = !login.value || !password.getValue();
+        }
+        login.onchange = setButtonState;
+        password.onChange = setButtonState;
+        button.disabled = true;
+    </script>
+</body>

+ 10 - 0
js/14 FOOP/hw14_06_login_form_constructor.html

@@ -0,0 +1,10 @@
+<header>
+
+</header>
+
+<body>
+    <script>
+
+        
+    </script>
+</body>

+ 8 - 0
js/14 FOOP/hw14_07_password_verify.html

@@ -0,0 +1,8 @@
+<header>Password Verify</header>
+
+<body>
+    <script>
+
+        
+    </script>
+</body>

+ 1 - 4
js/HW_13_kiosk/index.html

@@ -132,10 +132,7 @@
                 amount > 0 &&
                 cash > 0 &&
                 state[item].price <= cash / amount) { //если тип action - КУПИТЬ, то:
-                /*let newState = {...state};
-                newState[item] =  state[item].amount - amount;
-                newState.cashbox = state.cashbox + amount * price;
-                return newState;*/
+                
                 return {
                     ...state, //берем все что было из ассортимента
                     [item]: { amount: state[item].amount - amount, price: state[item].price },  //и уменьшаем то, что покупается на количество