«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Code IT

Python - 실습 - 게시판 만들기 (클래스) 본문

Python & Django

Python - 실습 - 게시판 만들기 (클래스)

Codit 2019. 1. 31. 15:55
class Board:
	articles = []
	
	def print_welcome_message(self):
		print("""############################
# Welcome to Python Boards #
############################""")
	
	def print_menu(self):
		print("""##################################################################
# 1. List # 2. Search # 3. Read # 4. Delete # 5. Write # 0. Quit #
##################################################################""")	
	
	def input_menu(self):
		id = input("Choice Menu : ")
		return int(id)
		
	def print_all_articles(self):
		if len(self.articles) == 0:
			print("Have no articles.")
			return
		
		for artcl in self.articles:
			self.print_article(artcl)
	
	def print_article(self, article):
		print("Subject : " + article["subject"] + ", Read : " + str(article["read"]))
	
	def search_article(self):
		search_keyword = input("Input search keyword : ")
		
		for artcl in self.articles:
			if search_keyword in artcl["subject"]:
				self.print_article(artcl)
					
	def read_article(self):
		id = input("Choice article id : ")
		id = int(id)
		
		if len(self.articles) - 1 < id:
			print(id, " isn't exists.'")
		else:
			self.articles[id]["read"] += 1
			self.print_article(self.articles[id])
	
	def delete_article(self):
		id = input("Choice article id : ")
		id = int(id)
		
		if len(self.articles) - 1 < id:
			print(id, " isn't exists.'")
		else:
			self.articles.pop(id)
	
	def write_article(self):
		subject = input("Input Subject : ")
		article = {
			"subject": subject,
			"read": 0
		}
		
		self.articles.append(article)
	
	def run(self):
		self.print_welcome_message()
		
		while(True):
			self.print_menu()
			id = self.input_menu()
			
			if id == 1:
				self.print_all_articles()
			elif id == 2:
				self.search_article()
			elif id == 3:
				self.read_article()
			elif id == 4:
				self.delete_article()
			elif id == 5:
				self.write_article()
			elif id == 0:
				print("Quit!")
				break
			print("")
			
def start_board_app():
	board = Board()
	board.run()
	
if __name__ == "__main__":
	start_board_app()

'Python & Django' 카테고리의 다른 글

Python - Class Method, Instance Method  (0) 2019.02.03
Python - self  (0) 2019.02.03
Python - 실습 - 게시판 만들기 (함수)  (0) 2019.01.31
Python - class  (0) 2019.01.31
Python - loop ~ else  (0) 2019.01.31
Comments