Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complement Search, Amend Features #18

Merged
merged 2 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 62 additions & 38 deletions backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}

Expand All @@ -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 = '<i class="bi bi-x"></i>';
div.appendChild(buttonRemove);

buttonRemove.addEventListener("click", () => {
div.remove();
});
});

//정보추가 버튼 - #new_add
Expand Down Expand Up @@ -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");
});
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
.container {
width: 720px;
}

</style>
</head>
+
<body>
<!-- HTML Code -->
<nav class="navbar navbar-light bg-primary">
Expand Down