온라인 투표 시스템을 설계하고 구현해야 합니다. 시스템은 사용자가 설문 조사를 생성, 관리 및 참여할 수 있도록 허용해야 합니다. 각 설문 조사는 질문과 답변에 대한 여러 옵션으로 구성됩니다. 사용자는 설문조사에 투표하고 결과를 볼 수 있습니다.
createPoll :
updatePoll :
설문조사 삭제 :
voteInPoll :
설문조사 결과 보기 :
{ "pollId": "123", "question": "What is your favorite color?", "options": ["Red", "Blue", "Green", "Yellow"], "createdAt": "2024-07-11T00:00:00Z" }
{ "pollId": "123", "userId": "user1", "option": "Red", "timestamp": "2024-07-11T01:00:00Z" }
이 코드는 JavaScript 클래스를 사용하여 기본 폴링 시스템을 정의합니다. 설문조사의 생성, 관리, 투표가 가능합니다. 코드의 각 부분을 분석해 보겠습니다.
createPoll(question, options)
updatePoll(pollId, question, options)
deletePoll(pollId)
voteInPoll(pollId, userId, option)
viewPollResults(pollId)
This implementation provides a basic but functional polling system, handling common scenarios such as poll creation, updating, voting, and deletion.
class Poll { constructor(id, question, options) { this.pollId = id; this.question = question; this.options = options; this.createdAt = new Date(); } } class Vote { constructor(pollId, userId, option) { this.pollId = pollId; this.userId = userId; this.option = option; this.timestamp = new Date(); } } class PollManager { constructor() { this.polls = new Map(); this.pollResults = new Map(); this.userVotes = new Map(); } createPoll(question, options) { const pollId = (this.polls.size + 1).toString(); const poll = new Poll(pollId, question, options); this.polls.set(pollId, poll); const result = new Map(); options.forEach(option => result.set(option, 0)); this.pollResults.set(pollId, result); return pollId; } updatePoll(pollId, question, options) { const poll = this.polls.get(pollId); if (poll) { poll.question = question; poll.options = options; // Update results for the new options const result = new Map(); options.forEach(option => result.set(option, 0)); this.pollResults.set(pollId, result); return "Poll updated successfully."; } return "Poll not found."; } deletePoll(pollId) { if (this.polls.delete(pollId)) { this.pollResults.delete(pollId); this.userVotes.delete(pollId); return "Poll deleted successfully."; } return "Poll not found."; } voteInPoll(pollId, userId, option) { const poll = this.polls.get(pollId); if (poll) { if (!this.userVotes.has(pollId)) { this.userVotes.set(pollId, new Map()); } const userVote = this.userVotes.get(pollId); if (userVote.get(userId)) { return "User has already voted."; } const result = this.pollResults.get(pollId); if (result.has(option)) { result.set(option, result.get(option) + 1); userVote.set(userId, true); return "Vote cast successfully."; } else { return "Invalid option."; } } return "Poll not found."; } viewPollResults(pollId) { const results = this.pollResults.get(pollId); if (results) { return Array.from(results.entries()); } return "Poll not found."; } } // Example usage const pollManager = new PollManager(); // Creating a poll const pollId = pollManager.createPoll("What is your favorite color?", ["Red", "Blue", "Green", "Yellow"]); console.log("Poll created with ID:", pollId); // Voting in the poll let voteMessage = pollManager.voteInPoll(pollId, "user1", "Red"); console.log(voteMessage); voteMessage = pollManager.voteInPoll(pollId, "user2", "Blue"); console.log(voteMessage); voteMessage = pollManager.voteInPoll(pollId, "user1", "Green"); console.log(voteMessage); // Should inform the user has already voted // Viewing poll results let results = pollManager.viewPollResults(pollId); console.log("Poll results for poll ID", pollId, ":", results); // Updating the poll let updateMessage = pollManager.updatePoll(pollId, "What is your favorite primary color?", ["Red", "Blue", "Yellow"]); console.log(updateMessage); // Voting in the updated poll voteMessage = pollManager.voteInPoll(pollId, "user3", "Yellow"); console.log(voteMessage); // Viewing updated poll results results = pollManager.viewPollResults(pollId); console.log("Updated poll results for poll ID", pollId, ":", results); // Deleting the poll let deleteMessage = pollManager.deletePoll(pollId); console.log(deleteMessage); // Attempting to view results of a deleted poll results = pollManager.viewPollResults(pollId); if (typeof results === "string") { console.log(results); } // Response // Poll created with ID: 1 // Vote cast successfully. // Vote cast successfully. // User has already voted. // Poll results for poll ID 1 : [['Red', 1], ['Blue', 1], ['Green', 0], ['Yellow', 0]] // Poll updated successfully. // Vote cast successfully. // Updated poll results for poll ID 1 : [['Red', 0], ['Blue', 0], ['Yellow', 1]] // Poll deleted successfully. // Poll not found.
Note: Please follow for more detail & Enchanced version of this article with DB, API & other system Design Concept.
More Details:
Get all articles related to system design
Hastag: SystemDesignWithZeeshanAli
zeeshanali와 함께하는 시스템 디자인
Git: https://github.com/ZeeshanAli-0704/SystemDesignWithZeeshanAli
위 내용은 저수준 설계: 폴링 시스템의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!