Example #1
0
 def __init__(self):
     self.cm = ContactManager()
     self.run()
Example #2
0
class ContactManagerGUI:
    def __init__(self):
        self.cm = ContactManager()
        self.run()

    def run(self):
        self.window = Tk()
        self.window.title("Contact Manager")
        self.window.geometry('450x185')

        frm_content = Frame(self.window)
        frm_content.grid(column=0, row=0)
        self.listbox = Listbox(frm_content, width=40, height=10)
        self.listbox.grid(column=0, row=0)
        self.listbox.bind('<<ListboxSelect>>', self.show_detail)

        frm_detail = Frame(frm_content)
        frm_detail.grid(sticky=N, column=1, row=0)
        detail_title = Label(frm_detail, text="Details:", justify=LEFT)
        detail_title.pack(side=TOP)
        self.detail_name = Label(frm_detail, text="-", justify=LEFT)
        self.detail_name.pack(side=TOP)
        self.detail_email_address = Label(frm_detail, text="-", justify=LEFT)
        self.detail_email_address.pack(side=TOP)
        self.detail_phone_number = Label(frm_detail, text="-", justify=LEFT)
        self.detail_phone_number.pack(side=TOP)

        frm_buttons = Frame(self.window)
        frm_buttons.grid(sticky=W, column=0, row=1)
        btn_add_contact = Button(frm_buttons,
                                 text="Add contact",
                                 command=self.btn_add_contact_action)
        btn_add_contact.pack(side=LEFT)
        btn_remove_contact = Button(frm_buttons,
                                    text="Remove contact",
                                    command=self.btn_remove_contact_action)
        btn_remove_contact.pack(side=LEFT)

        frm_find = Frame(frm_buttons)
        frm_find.pack(side=RIGHT)
        self.ent_find_contacts = Entry(frm_find)
        btn_find_contacts = Button(
            frm_find,
            text="Find contacts",
            command=lambda: self.btn_find_contacts_action(
                self.ent_find_contacts.get()))
        btn_find_contacts_cancel = Button(
            frm_find, text="x", command=self.btn_find_contacts_cancel_action)
        btn_find_contacts.pack(side=LEFT)
        btn_find_contacts_cancel.pack(side=RIGHT)
        self.ent_find_contacts.pack(side=RIGHT)

        frm_status = Frame(self.window)
        frm_status.grid(sticky=W, column=0, row=2)
        self.status = Label(frm_status, text="OK", justify=LEFT)
        self.status.grid(sticky=W, column=0, row=0)

        self.populate_list()
        self.window.mainloop()

    def populate_list(self, contacts=None):
        self.listbox.delete(0, END)
        self.detail_name.config(text="-")
        self.detail_email_address.config(text="-")
        self.detail_phone_number.config(text="-")
        if contacts is None:
            contact_list = self.cm.get_contacts()
        else:
            contact_list = contacts
        for contact in contact_list:
            self.listbox.insert(END, contact.get_name())

    def show_detail(self, idx):
        selected_name = self.listbox.get(ACTIVE)
        contact = self.cm.get_contact(selected_name)
        if contact is not None:
            self.detail_name.config(text=contact.get_name(), justify=LEFT)
            self.detail_email_address.config(text=contact.get_email_address(),
                                             justify=LEFT)
            self.detail_phone_number.config(text=contact.get_phone_number(),
                                            justify=LEFT)

    def btn_add_contact_action(self):
        wnd_add_contact = Toplevel(self.window)
        wnd_add_contact.title("Add contact")
        wnd_add_contact.geometry('235x82')
        Label(wnd_add_contact, text="Name", justify=LEFT).grid(sticky=W,
                                                               column=0,
                                                               row=0)
        Label(wnd_add_contact, text="Email address",
              justify=LEFT).grid(sticky=W, column=0, row=1)
        Label(wnd_add_contact, text="Phone number",
              justify=LEFT).grid(sticky=W, column=0, row=2)
        self.add_contact_name = Entry(wnd_add_contact, width=21)
        self.add_contact_name.grid(column=1, row=0)
        self.add_contact_email_address = Entry(wnd_add_contact, width=21)
        self.add_contact_email_address.grid(column=1, row=1)
        self.add_contact_phone_number = Entry(wnd_add_contact, width=21)
        self.add_contact_phone_number.grid(column=1, row=2)
        Button(wnd_add_contact,
               text="Add",
               command=lambda: self.btn_add_contact_add_action(wnd_add_contact)
               ).grid(sticky=W, column=0, row=3)
        Button(wnd_add_contact, text="Cancel",
               command=wnd_add_contact.destroy).grid(sticky=E, column=1, row=3)

    def btn_add_contact_add_action(self, parent):
        name = self.add_contact_name.get()
        email_address = self.add_contact_email_address.get()
        phone_number = self.add_contact_phone_number.get()
        if self.cm.is_valid_email_address(email_address) == True:
            if self.cm.is_valid_phone_number(phone_number) == True:
                self.cm.create_contact(name, email_address, phone_number)
                self.populate_list()
                parent.destroy()
            else:
                messagebox.showwarning("Invalid", "Invalid phone number!")
                parent.deiconify()
        else:
            messagebox.showwarning("Invalid", "Invalid email!")
            parent.deiconify()

    def btn_remove_contact_action(self):
        selected_name = self.listbox.get(ACTIVE)
        contact = self.cm.get_contact(selected_name)
        if contact is not None:
            self.cm.delete_contact(contact.get_name())
            self.populate_list()

    def btn_find_contacts_action(self, name):
        contacts = self.cm.find_contacts(name)
        if contacts:
            self.status.config(text="Found contacts!", justify=LEFT)
            self.populate_list(contacts)
        else:
            self.status.config(text="No contacts found.", justify=LEFT)

    def btn_find_contacts_cancel_action(self):
        self.status.config(text="OK", justify=LEFT)
        self.ent_find_contacts.delete(0, END)
        self.populate_list()
 def setUp(self):
     self.cm = ContactManager()
     self.contact = Contact("Bob", "*****@*****.**", "123456789")
Example #4
0
    def __init__(self):
        controller = ContactManager()

        while True:
            line = input()

            if line == "":
                exit(0)

            commands = line.split(" ")

            if commands[0] == "AC":
                name = commands[1]
                email_address = input()
                phone_number = input()

                if controller.has_contact(name):
                    print("The contact already exists.")
                else:
                    controller.create_contact(name, email_address,
                                              phone_number)
                    print("Contact successfully created.")

            elif commands[0] == "ECE":
                name = commands[1]
                email_address = input()

                if not controller.has_contact(name):
                    print("No such contact.")
                elif not controller.is_valid_email_address(email_address):
                    print("Invalid email")
                else:
                    controller.change_email_address(name, email_address)
                    print("Contact successfully updated.")

            elif commands[0] == "ECP":
                name = commands[1]
                phone_number = input()

                if not controller.has_contact(name):
                    print("No such contact.")
                elif not controller.is_valid_phone_number(phone_number):
                    print("Invalid phone number")
                else:
                    controller.change_phone_number(name, phone_number)
                    print("Contact successfully updated.")

            elif commands[0] == "LC":
                if not controller.has_contacts():
                    print("No contacts available.")
                else:
                    contacts = controller.get_contacts()
                    for contact in contacts:
                        print(contact.get_name())

            elif commands[0] == "SC":
                name = commands[1]
                if not controller.has_contact(name):
                    print("No such contact.")
                else:
                    contact = controller.get_contact(name)
                    print(contact.get_name())
                    print(contact.get_email_address())
                    print(contact.get_phone_number())

            elif commands[0] == "DC":
                name = commands[1]
                name = commands[1]
                if not controller.has_contact(name):
                    print("No such contact.")
                else:
                    controller.delete_contact(name)
                    print("Contact deleted.")

            elif commands[0] == "FC":
                name = commands[1]
                if not controller.has_contacts():
                    print("No contacts available.")
                else:
                    contacts = controller.find_contacts(name)
                    if not contacts:
                        print("No results.")
                    else:
                        for contact in contacts:
                            print(contact.get_name())
class TestContactManager(unittest.TestCase):
    def setUp(self):
        self.cm = ContactManager()
        self.contact = Contact("Bob", "*****@*****.**", "123456789")

    def add_contact(self):
        self.cm.contacts.append(self.contact)

    def test_has_contacts(self):
        self.assertFalse(self.cm.has_contacts())
        self.add_contact()
        self.assertTrue(self.cm.has_contacts())

    def test_get_contacts(self):
        self.assertEqual(self.cm.get_contacts(), [])
        self.add_contact()
        self.assertEqual(self.cm.get_contacts(), [self.contact])

    def test_get_contact(self):
        self.assertIsNone(self.cm.get_contact(self.contact.get_name()))
        self.add_contact()
        self.assertEqual(self.cm.get_contact(self.contact.get_name()),
                         self.contact)

    def test_has_contact(self):
        self.assertFalse(self.cm.has_contact(self.contact.get_name()))
        self.add_contact()
        self.assertTrue(self.cm.has_contact(self.contact.get_name()))

    def test_create_contact(self):
        self.cm.create_contact(self.contact.get_name(),
                               self.contact.get_email_address(),
                               self.contact.get_phone_number())
        self.assertEqual(self.cm.get_contacts()[0].get_name(),
                         self.contact.get_name())
        self.assertEqual(self.cm.get_contacts()[0].get_email_address(),
                         self.contact.get_email_address())
        self.assertEqual(self.cm.get_contacts()[0].get_phone_number(),
                         self.contact.get_phone_number())

    def test_delete_contact(self):
        self.add_contact()
        self.cm.delete_contact(self.contact.get_name())
        self.assertListEqual(self.cm.get_contacts(), [])

    def test_change_email_address(self):
        self.add_contact()
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_email_address(),
            self.contact.get_email_address())
        self.cm.change_email_address(self.contact.get_name(),
                                     "*****@*****.**")
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_email_address(),
            "*****@*****.**")

    def test_is_valid_email_address(self):
        self.assertTrue(self.cm.is_valid_email_address("[email protected]"))
        self.assertFalse(self.cm.is_valid_email_address("a@a"))
        self.assertFalse(self.cm.is_valid_email_address(""))

    def test_change_phone_number(self):
        self.add_contact()
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_phone_number(),
            self.contact.get_phone_number())
        self.cm.change_phone_number(self.contact.get_name(), "987654321")
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_phone_number(),
            "987654321")

    def test_is_valid_phone_number(self):
        self.assertTrue(self.cm.is_valid_phone_number("999999999"))
        self.assertFalse(self.cm.is_valid_phone_number("99999999"))
        self.assertFalse(self.cm.is_valid_phone_number(""))
        self.assertFalse(self.cm.is_valid_phone_number("9999999999"))

    def test_find_contacts(self):
        self.add_contact()
        contacts = self.cm.find_contacts("alice")
        self.assertListEqual(contacts, [])
        contacts = self.cm.find_contacts("o")
        self.assertListEqual(contacts, [self.contact])
        contacts = self.cm.find_contacts("")
        self.assertListEqual(contacts, [self.contact])