본문 바로가기
my_lesson/_Python

Python OOP 메모장 만들기

by boolean 2018. 9. 26.
728x90

Python OOP 메모장 만들기

## teamlab_note.py

class Note(object):

    """docstring for Note."""

    def __init__(self, content = None):

        super(Note, self).__init__()

        self.content = content


    def write_content(self, content):

        self.content = content


    def remove_all(self):

        self.content = ""


    def __str__(self):

        return str(self.write_content)



class NoteBook(object):

    """docstring for NoteBook."""

    def __init__(self, title):

        super(NoteBook, self).__init__()

        self.title = title

        self.page_number = 1

        self.notes = {}


    def add_note(self, note, page =0):

        if self.page_number < 300:

            if page ==0:

                self.notes[self.page_number] = note

                self.page_number += 1

            else:

                self.notes = {page: note}

                self.page_number += 1

        else:

            print("페이지가 모두 채워졌읍니다.")


    def remove_note(self, page_number):

        if page_number in self.notes.keys():

            return self.notes.pop(page_number)

        else:

            print("해당 페이지는 존재하지 않습니다.")


    def get_number_of_pages(self):

        return len(self.notes.keys())


## note_user.py


from teamlab_note import NoteBook

from teamlab_note import Note


quote_book = NoteBook("The Quote Book")


new_note = Note()

new_note.write_content("Don't cry because it's over, because it happende. - Dr. Seuss")

quote_book.add_note(new_note)


print(quote_book.get_number_of_pages())


quote_book.add_note(Note("Hello, World"))

quote_book.add_note(Note("Hello, World"))

quote_book.add_note(Note("Hello, World"))

quote_book.add_note(Note("Hello, World"))

quote_book.add_note(Note("Hello, World"))


print(quote_book.get_number_of_pages())


my_note = quote_book.remove_note(1)

print(my_note)

my_note = quote_book.remove_note(1)

print(my_note)

## reault
~$ python3 note_user.py 
1
6
<bound method Note.write_content of <teamlab_note.Note object at 0x7ffa6cbca908>>
해당 페이지는 존재하지 않습니다.
None




댓글