0

I need to create a program consisting of two classes; a "person" class and a "friend" class. The person class must have the attributes( first_name, last_name, phone_number ). The "friend" class must have birth date and email.
The application will present a main menu to the user that will allow the user to add a contact, lookup a contact by name, or exit the application.
I have found a great guide here
I am not sure how to accomplish this with multiple classes. I am in a 5 week course, with a textbook that is no help. If someone could please walk me though this, I would appreciate it!
Here is what I have now:

class person:
    def information:
        first = print(input("first: ")
        last = print(input("last: ")
        number = print(input("phone number: ")
 class friend:
     def friendinfo:
         email = print(input("email: ")
         birthday = print(input("birthday: ")

When the user chooses to add a contact, the application will ask the user if they want to add a regular person or a friend. If the user wants to add a new regular person contact then the application will ask for the first name, last, name and phone number for that person.
If the user wants to add a new friend contact then the application will ask the user for the first name, last name, phone number, email address, and birth date. When the user chooses to lookup a contact by name, the application will ask the user for the last name, and then the application will display the full information for all contacts with that last name.

Stevenson
  • 617
  • 1
  • 6
  • 10

3 Answers3

4

Notes on Classes

A class is like a blueprint for a thing. When you create a class, you're just making a blueprint, like drawing out architectural plans for a building. That building, however, has yet to be created. You can also take these plans and create tons of buildings that all are formed in the same way (I live in Southern California and it's a very OOP-oriented place as illustrated by the suburbs). At any rate, a class is just a template. When you actually make the thing the class defines, then you can populate it with data.

Problem Part I

So let's look at what you need:

The person class must have the attributes( first_name, last_name, phone_number ). The "friend" class must have birth date and email.

So let's make a class called person:

class Person:

When we create classes, we 'instantiate' them and we often need some instructions so the program knows what to do when one of these "Person" things is created/instantiated. We often put those instructions in a section called 'init':

class Person:
    def __init__(self):

The 'self' part is confusing, but every method in our class needs some way of knowing that it's part of a particular class object. This is a really fuzzy way of glossing over what's going on with 'self'. For now, it's just a general rule: all the functions we put in our class will have 'self' as their first arguments.

Next we want to add the attributes you mentioned. That usually looks like this:

class Person:

    def __init__(self, first_name, last_name, phone_number):
        self.first_name = first_name
        self.last_name = last_name
        self.phone_number = phone_number

I put these attributes in the __init__ function, which means that whenever I first create one of these 'Person' things, I must specify these things or my creation will fail to work. When I create one, I just have to call my class Person and give it the stuff it expects:

bob = Person('Bob', 'Something', '43819110')

Now I can access bob's attributes in the following way:

>>> bob.phone_number
'43819110'

We can add a new person (potentially using input() as you have above...):

sinatra = Person('Frank', 'Sinatra', 217653918')

And we're doing well, creating all kinds of people with our Person blueprint. We're going to need some container to hold all of these people:

contacts = [sinatra, bob, etc.]

We can get someone's phone numbers by iterating through that container:

for contact in contacts:
    if contact.first_name == 'Bob':
      print(contact.phone_number)

There's a lot to work with here, but it may help you get started in the right direction. The point is to separate tasks and try to get them to work together. Happy coding!


[Deleted everything else because it's too long to be really useful to anyone else]

erewok
  • 7,555
  • 3
  • 33
  • 45
  • +10 for a much more detailed and friendly answer than mine! – Joohwan Aug 18 '13 at 05:32
  • Well, thanks, friend. Classes are a lot of fun. – erewok Aug 18 '13 at 18:21
  • @erewok You explain it very well, I have made corrections. I cannot test it though do you know why I am getting syntax errors? What do I need to do to fix it? – Stevenson Aug 18 '13 at 19:12
  • @erewok I want the program to choose ether **just person** or both **person and friend** . In the code I attempted to create an option for the user to choose if this is just a person they are adding to the contacts or are they a friend. – Stevenson Aug 18 '13 at 19:23
  • @erewok Would using a get_info method work? – Stevenson Aug 18 '13 at 19:27
  • _This is what I have to work with:_ Both your Person class and your Friend class will have a get_info method. For the Person class, the get_info method will return a string with the full name and phone number of the person. For the Friend class, the get_info method will return a string with the full name, phone number, email address, and birth date. – Stevenson Aug 18 '13 at 19:29
  • 1
    Unfortunately, my post is getting a bit long, but I think your menu should really work *if your classes are constructed correctly*. Focus on getting the classes right and *focus on understanding how classes are built* and the rest should be easy. Lastly, at this time, I strongly recommend *not* coding in response to errors. Your time will be better spent writing stuff that is small that works and building on top of that stuff. Don't simply try to fix the errors that appear until you understand what you are writing. – erewok Aug 18 '13 at 19:32
  • 1
    I will add a `get_info` method and show you what that might look like. – erewok Aug 18 '13 at 19:33
1

This may not fit your structure requirements, but if I were to do what you're doing, I would probably try something that's structured more like this:

class contacts:
    contacts = [{...}, {...}] # Contains all the contacts as dictionaries in a list

    def add_person(...):
        self.all_contacts += ...

    def add_friend(...):
        self.all_contacts += ...

    def get_contact(...):      # This is just potential code
        if ... in self.all_contacts:
            return ...

( Maybe having friends and people separate if I really wanted: )

people = [{...}, {...}]            # all people contacts
friends = [{...}, {...}]           # all friend contacts
all_contacts = people + friends    # both

I prefer this method because (I think) it would have clearer hierarchy than having separate classes for two similar things. (i.e. friends and people )

This way makes it more comprehensible to have different 'contact books' as multiple instances of the contacts class, with friend and people categories in each.

jims_contacts = contacts()
jims_contacts.add_friend("Megan", "Cook", 02754321, "email@something.com", 170398)

more_contacts = contacts()

P.S. A single contact would look like a dictionary:

{'first':"Zaphod", 'last':"Beeblebrox", 'number':2293107}

... and each contact stored in a list. That makes it easy to pull out attributes and find things later on.

Good luck!

Jollywatt
  • 1,382
  • 2
  • 12
  • 31
0

Well I can't give you all the answers since this seems like it's your homework, but I will help you get started. This is the minimum requirement (and probably all you need) for the Person class you described:

class Person(object):

    def __init__(self, first, last, phone):
        self.first_name = first
        self.last_name = last
        self.phone_number = phone

And I am guessing your course instructor wants you to use inheritance. If he did... well even if he didn't it would be a good idea to use inheritance for Friend (i.e. the Friend class should be a derived class of Person).

For more on inheritance check out this thread:

What is a basic example of single inheritance using the super() keyword in Python?

Feel free to leave a comment if you have any questions.

Community
  • 1
  • 1
Joohwan
  • 2,374
  • 1
  • 19
  • 30