언어 자료구조 알고리즘/프로그래밍 실습
[python] OOP 실습 - 커뮤니트 시뮬레이션 만들기
언제나휴일
2020. 10. 30. 16:04
반응형
시나리오
커뮤니티 시뮬레이션 응용을 작성하시오.
로긴=>사용=>로긴=>사용=>로긴=>사용...순으로 수행한다.
로긴에서는 사용자 이름을 입력한다.
사용에서는 메뉴 선택에 의해 기능을 수행하는 것을 반복한다.
메뉴: 1.게시글 작성, 2. 게시글 삭제, 3. 게시글 검색, 4. 전체 보기 0.종료
종료를 선택하면 자동 로그아웃 처리되며 다시 로긴을 수행한다.
*로긴에서 입력한 사용자 이름이 "Exit"이면 프로그램을 종료한다.
게시글 작성에서는 제목, 내용, 비밀키를 입력받는다.
비밀키를 입력하지 않으면 일반 게시글, 입력하면 비밀 게시글
게시글 삭제에서는 제목, 비밀키를 입력받는다.
비밀게시글은 비밀키가 일치해야 삭제할 수 있다.
(*작성자가 아니어도 삭제할 수 있음)
Main.py
from Application import Application
app = Application()
while app.Login():
app.Run()
Application.py
#Application.py
from Community import Community
class Application:
def __init__(self):
print("Application 생성")
community = Community()
self.user = ""
def Login(self):
print("로긴")
self.user = input("사용자:")
return self.user!="Exit"
def Run(self):
print("사용")
*Run 메서드부터 구체적으로 구현하시오.
구현 예:
Main.py
from Application import Application
app = Application()
while app.Login():
app.Run()
Application.py
#Application.py
import os
from Community import Community
class Application:
def __init__(self):
print("Application 생성")
self.community = Community()
self.user = ""
def Login(self):
print("로긴")
self.user = input("사용자:")
return self.user!="Exit"
def Run(self):
print("사용")
while True:
key = self.SelectMenu()
if key == '1':
self.WritePost()
elif key == '2':
self.RemovePost()
elif key == '3':
self.Find()
elif key == '4':
self.View()
elif key == '0':
break
else:
print("잘못 선택하였습니다.")
input("엔터 키를 누르세요.")
def SelectMenu(self):
os.system("cls")
print("1:게시글 작성 2:게시글 삭제 3:게시글 검색 4:전체 보기 0:종료")
return input("메뉴 선택:")
def WritePost(self):
print("게시글 작성")
title = input("제목:")
content = input("내용:")
secret = input("비밀번호:")
self.community.Publish(title,self.user,content,secret)
def RemovePost(self):
print("게시글 삭제")
title = input("제목:")
secret = input("비밀번호:")
self.community.Remove(title,secret)
def Find(self):
print("게시글 검색")
title = input("제목:")
post = self.community.Find(title)
if post == None:
print("없는 게시글입니다.")
self.community.ViewPost(post)
def View(self):
print("전체 보기")
self.community.View()
Community.py
#Community.py
from Post import Post
from SPost import SPost
class Community:
def __init__(self):
print("커뮤니티 생성")
self.posts = list()
def Publish(self,title,writer,content,secret=""):
print("Publish")
if secret=="":
post = Post(title,writer,content)
self.posts.append(post)
else:
post = SPost(title,writer,content,secret)
self.posts.append(post)
def View(self):
print("View")
for post in self.posts:
self.ViewPost(post)
def ViewPost(self,post):
print("제목:",post.title)
print("작성자:",post.writer)
print("내용:",post.content)
if(isinstance(post,SPost)):
print("비밀글")
def Find(self,title):
print("Find")
for post in self.posts:
if(post.title == title):
return post
return None
def Remove(self,title,secret=""):
print("Remove")
for post in self.posts:
if(post.title == title):
if(isinstance(post, SPost)):
if post.secret == secret:
self.posts.remove(post)
else:
self.posts.remove(post)
break
Post.py
#Post.py
class Post:
def __init__(self,title,writer,content):
print("Post 생성")
self.title = title
self.writer = writer
self.content = content
SPost.py
#SPost.py
from Post import Post
class SPost(Post):
def __init__(self,title,writer,content,secret):
#Post.__init__(self,title,writer,content)
super().__init__(title,writer,content)
self.secret = secret
반응형