Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting Program to OOP
#1
Hello, I recently created a makeshift 'My Contact' program that is completely text based. I'm wondering if it is possible to turn this code into Object Oriented Programming, and if yes would the code become much better in terms of organization?

def main():
    #init variables 
    users = [{"name": 'John Doe', "phone": '123-456-7890', "address": '1000 Constitution Ave'}]
    loop = True;
    
    while loop:
        #Main menu options 
        print('Main Menu')
        print('1. Display Contact Names')
        print('2. Search For Contacts')
        print('3. Edit Contact')
        print('4. New Contact')
        print('5. Remove Contact')
        print('6. Exit')
        
        selection = input('Enter a # form the menu: ')
        
        #Display contact names
        if (selection == "1"):
            for index in range(len(users)):
                print("Name: " + users[index]["name"])
                
                
        #Search for contacts
        if (selection == "2"):
            searchname = input('What is the name of your contact: ')
            for index in range(len(users)):
                if (users[index]["name"] == searchname):
                    print("Name: " + users[index]["name"])
                    print("Address: " + users[index]["address"])
                    print("Phone: " + users[index]["phone"])
                
        #Edit contacts
        if (selection == "3"):
            searchname = input('What is the name of the contact that you want to edit: ')
            for index in range(len(users)):
                if (users[index]["name"] == searchname):  
                    users.pop(index)
                    
                    name = input('What is your name: ')
                    address = input('What is your address: ')
                    phone = input('What is your phone number: ')
                    
                    users.append({"name": name, "phone": phone, "address": address})                      
                    
                    
            
            
            
        #New contact
        if (selection == "4"):
            name = input('What is your name: ')
            address = input('What is your address: ')
            phone = input('What is your phone number: ')
            
            users.append({"name": name, "phone": phone, "address": address})  
            
        #Remove contact
        if (selection == "5"):
            searchname = input('What is the name of the contact that you want to delete: ')
            for index in reversed(range(len(users))):
                if (users[index]["name"] == searchname):  
                    users.pop(index)
                    
            print(searchname, 'has been removed')
                    
                    
        #Exit the program           
        if (selection == "6"):
            print('Thank you for using the contact book, have a nice day')
            print('Carson147 2019©, All Rights Reserved')
            loop = False;
            
            
            
main()
Reply
#2
I don't think OOP would help organization much now, but it would help in terms of expanding it. Right now it would allow easier access to attributes, and by overriding __str__, easier printing. You could add a match method to it for finding contacts, which would allow you to code some partial matching. That would help as you are doing matching in more than one place and repeating your code.

Also, don't loop over indexes, loop over items in the list. For example:

            for index in range(len(users)):
                print("Name: " + users[index]["name"])
should be:

            for user in users:
                print("Name: " + user["name"])
Much simpler, so less likely to have bugs and easier to maintain.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
A User class could be made for this and a Rolodex Class as a controller for a list of User objects/dictionaries.

I did it that way, but yea like ichabod said, I don't think your script is complicated enough for that investment yet.

( The only pro I see is that objects would be easier to test if you do make objects. )

PM me directly if you want to collaborate on this project!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020