From d7865e3925472acf9cd42e5d07727712cb60562e Mon Sep 17 00:00:00 2001 From: ChanuYu Date: Sun, 23 May 2021 16:09:05 +0900 Subject: [PATCH 1/2] Complement Search, Amend Features --- backend.js | 100 +++++++++++++++++++++++++++++++++-------------------- index.html | 1 + 2 files changed, 63 insertions(+), 38 deletions(-) diff --git a/backend.js b/backend.js index e595cba..5bec3f8 100644 --- a/backend.js +++ b/backend.js @@ -3,19 +3,17 @@ let user = { /* user_name, - stock_list - */ -}; -let stock = { - /* stock_name, stock_amount, stock_price, + total, + current_price, + margin */ }; + //localStorage에 저장하는 key도 user_list로 저장 let user_list = []; -let stock_list = []; //save data function saveInfo() { @@ -36,25 +34,26 @@ window.addEventListener("load", () => { }); //정보 조회 (버튼 = #search, input _text = #name) -function selectData(name) { - let target = user_list.filter((t) => { - t["user_name"] === name; - }); - return target; +function getIndex(name) { + let idx = user_list.findIndex((obj) => obj.user_name === name); + return idx; } -//한 사람당 여러 주식 갖도록 바꾸면 이 함수도 변경해야 함 -function getMargin(user, current_price) { - let amount = parseInt(user["stock_amount"]); - let old_price = parseInt(user["stock_price"]); - let diff = current_price - old_price; - return diff * amount; +function getCurrentPrice(obj) {} + +function getMarginSearch(user) { + let price = parseInt(user["current_price"]); + let amount = parseInt(user.stock_amount); + let avg_price = parseInt(user.total) / amount; + let margin = (price - avg_price) * amount; + return margin; } -function addSpanChild(div, user_key) { +function addSpanChild(div, idx) { + let target = user_list[idx]; let child = document.createElement("span"); - child.className = "me-1 mb-2 mt-2"; - child.textContent = data[user_key]; + child.className = "me-4"; + child.textContent = `${target["user_name"]}님의 주식 ${target["stock_name"]}의 정보: 현재 가격은 ${target["current_price"]}, 수익금은 ${target["margin"]}입니다.`; div.appendChild(child); } @@ -69,23 +68,29 @@ button_search.addEventListener("click", () => { return; } - let data = selectData(name); - let current_price; - //let margin = getMargin(data, current_price); - //data["margin"] = margin; + let s_idx = getIndex(name); + let current_price = getCurrentPrice(user_list[s_idx]); + user_list[s_idx]["current_price"] = current_price; + let margin = getMarginSearch(user_list[s_idx]); + user_list[s_idx]["margin"] = margin; //html document element 만들고 추가 let div = document.createElement("div"); - div.className = "d-flex-grow-1 align-items-center bg-light rounded-2 p-2"; - - addSpanChild(div, "user_name"); - addSpanChild(div, "stock_name"); - addSpanChild(div, "stock_amount"); - addSpanChild(div, "stock_price"); - addSpanChild(div, "stock_margin"); + div.className = + "d-flex-grow-1 align-items-center bg-light rounded-2 p-2 outer"; + addSpanChild(div, s_idx); let show_area = document.querySelector("#info"); show_area.appendChild(div); + + let buttonRemove = document.createElement("button"); + buttonRemove.className = "btn btn-sm btn-danger"; + buttonRemove.innerHTML = ''; + div.appendChild(buttonRemove); + + buttonRemove.addEventListener("click", () => { + div.remove(); + }); }); //정보추가 버튼 - #new_add @@ -123,24 +128,43 @@ button_newadd.addEventListener("click", () => { //console.log(user_list); }); +function getMarginAmend(user, price, change) { + change = -change; + let avg_price = parseInt(user.total) / parseInt(user.stock_amount); + let margin = (price - avg_price) * change; + return margin; +} + //정보갱신 let button_existadd = document.querySelector("#exist_add"); button_existadd.addEventListener("click", () => { - let name = getData("#exist_name"); + let getname = getData("#exist_name"); let stname = getData("#exist_stname"); let change = parseInt(getData("#stock_change")); let price = parseInt(getData("#exist_stprice")); - let t_idx = user_list.findIndex((obj) => obj.user_name === name); + //매수/매도량에 0을 입력하면 데이터 삭제 + if (change === 0) { + user_list = user_list.filter((obj) => obj["user_name"] !== getname); + saveInfo(); + console.log(user_list); + return; + } + + if (price === 0) { + price = parseInt(user["current_price"]); + } else { + price = parseInt(user["stock_price"]); + } + + let t_idx = getIndex(getname); let original_amount = parseInt(user_list[t_idx].stock_amount); if (change < 0) { - let avg_price = user_list[t_idx].total / user_list[t_idx].stock_amount; - let margin = (price - avg_price) * change; - } else { - user_list[t_idx].total += change * price; + let margin = getMarginAmend(user_list[t_idx], price, change); } + user_list[t_idx].total += change * price; user_list[t_idx].stock_amount = original_amount + change; - let open = window.open("./window.html"); + //let open = window.open("./window.html"); }); diff --git a/index.html b/index.html index 750de25..0e9e21c 100644 --- a/index.html +++ b/index.html @@ -21,6 +21,7 @@ .container { width: 720px; } + + From 693c77d5901ab9da0b64c9f7974c62cdb077e00e Mon Sep 17 00:00:00 2001 From: ChanuYu Date: Sun, 23 May 2021 17:01:37 +0900 Subject: [PATCH 2/2] index.html bug fixed --- index.html | 1 - 1 file changed, 1 deletion(-) diff --git a/index.html b/index.html index 0e9e21c..5b63de1 100644 --- a/index.html +++ b/index.html @@ -24,7 +24,6 @@ -+