Skip to content
Open
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
113 changes: 113 additions & 0 deletions 김윤희 1주차 과제
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>간단한 선거 시스템</title>
</head>
<body>
<h2>회원가입</h2>
<form id="signupForm">
<input type="text" id="signupUsername" placeholder="Username" required>
<input type="password" id="signupPassword" placeholder="Password" required>
<select id="signupRegion" required>
<option value="서울">서울</option>
<option value="부산">부산</option>
<option value="대구">대구</option>
</select>
<button type="submit">회원가입</button>
</form>

<h2>로그인</h2>
<form id="loginForm">
<input type="text" id="loginUsername" placeholder="Username" required>
<input type="password" id="loginPassword" placeholder="Password" required>
<button type="submit">로그인</button>
</form>

<h2>후보 등록</h2>
<form id="candidateForm">
<input type="text" id="candidateName" placeholder="후보 이름" required>
<button type="submit">후보 등록</button>
</form>

<h2>후보 목록</h2>
<div id="candidateList"></div>

<script>
// 로컬 스토리지를 활용한 회원가입
document.getElementById("signupForm").addEventListener("submit", function (e) {
e.preventDefault();
const username = document.getElementById("signupUsername").value;
const password = document.getElementById("signupPassword").value;
const region = document.getElementById("signupRegion").value;

const users = JSON.parse(localStorage.getItem("users")) || [];
users.push({ username, password, region });
localStorage.setItem("users", JSON.stringify(users));
alert("회원가입 성공!");
});

// 로그인 기능 구현
document.getElementById("loginForm").addEventListener("submit", function (e) {
e.preventDefault();
const username = document.getElementById("loginUsername").value;
const password = document.getElementById("loginPassword").value;

const users = JSON.parse(localStorage.getItem("users")) || [];
const user = users.find(u => u.username === username && u.password === password);
if (user) {
localStorage.setItem("loggedInUser", JSON.stringify(user));
alert("로그인 성공!");
displayCandidates(user.region);
} else {
alert("로그인 실패! 아이디와 비밀번호를 확인하세요.");
}
});

// 후보 등록 기능 구현
document.getElementById("candidateForm").addEventListener("submit", function (e) {
e.preventDefault();
const loggedInUser = JSON.parse(localStorage.getItem("loggedInUser"));
if (!loggedInUser) {
alert("로그인이 필요합니다!");
return;
}

const candidateName = document.getElementById("candidateName").value;
const candidates = JSON.parse(localStorage.getItem("candidates")) || {};
if (!candidates[loggedInUser.region]) {
candidates[loggedInUser.region] = [];
}
candidates[loggedInUser.region].push(candidateName);
localStorage.setItem("candidates", JSON.stringify(candidates));
alert("후보 등록 완료!");
displayCandidates(loggedInUser.region);
});

// 후보 목록 표시
function displayCandidates(region) {
const candidates = JSON.parse(localStorage.getItem("candidates")) || {};
const candidateList = candidates[region] || [];
const candidateListDiv = document.getElementById("candidateList");
candidateListDiv.innerHTML = "";
candidateList.forEach((candidate, index) => {
const candidateElement = document.createElement("div");
candidateElement.innerHTML = `
<input type="checkbox" id="candidate${index}" name="candidate" value="${candidate}">
<label for="candidate${index}">${candidate}</label>
`;
candidateListDiv.appendChild(candidateElement);
});
}

// 로그인 된 상태이면 후보 목록 표시
window.onload = function () {
const loggedInUser = JSON.parse(localStorage.getItem("loggedInUser"));
if (loggedInUser) {
displayCandidates(loggedInUser.region);
}
};
</script>
</body>
</html>