8.3.11

The Team / Person / Require exercise

Categories:

So far we have learned how to create classes, define methods, create variables that can be used by the methods, and create arrays.  The next thing I think is to get all these bits and pieces to talk to each other.  So we've made a person class that makes people, we've made a team.class that makes teams, but now we need to be able to make the put the people into the teams. So I finally think I've got my head around how this all could work.  I need to be able to pass an object I have created into a method that I have defined.  I was given that nugget a week ago but it sounded like swahili to me.  What a difference a week makes. :)

I have 3 files person.rb team.rb and require.rb

This is the first file that holds the person class.


class Person
  # Our initialize method which will get called any time someone
  # creates a new instance of our person class (e.g. Person.new("Name", 01-01-1980))
  # Everytime i make a person, he is going to need a name and date of birth
 

def initialize(name_parameter, date_of_birth_parameter)
 @name = name_parameter   
@date_of_birth = date_of_birth_parameter
 end

# This next method is part of the previous class I made
# but I'm going to leave it in as it will help me print my new people
# and I can see if the class is working.
 

def say_hello
    puts "Hi, my name is " + @name 
end

  ## This next "Getter" method I'm still not really very clear on
  # A getter method to access the persons name attribute
 
def name
    return @name 
end

  ## I'm not too clear on this either - Is it something to do with
  ## making sure you don't keep overwriting your instance variables.
  ## Definitely need to do a bit more reading based around this
 
 
  # A setter method to change the persons name attribute 

def set_name(new_name)
    # Take the parameter that's passed in and assign
    # to the @name instance variable
   
@name = new_name 
end
end

My Next class I'm going to make is a team and save it in the file team.rb  and it looks something like this.


## As I'm going to be adding people to my teams
## I'll need to call the person class and I'm doing that
## with the require method (is require a method?)

require 'person'
## In my team we can change the number of members and the team leaders name
## but the name of each team can only be read.


class Team
 attr_accessor :number_of_members, :team_leaders_name attr_reader :name

 ## Every time we call the team class by using Team.new
 ## We are going to have to enter in the name of the team
 ## the number of members, but the "" means that we can
 ## but we don't have to enter a team_leader_name parameter

 ## when we use the @ symbol I'm defining the variable for the parameters

 ## also every time we initialize the team class we create a team array using the []  

def initialize(name_parameter,
                      number_of_members_parameter,
                      team_leader_name_parameter="")

    @name = name_parameter   
@number_of_members = number_of_members_parameter   
@team_leaders_name = team_leader_name_parameter   
@team = []
  end

  ## This method is losing a member from the team
  ## I'm passing in an object that I have created by using the (person)
 

def lose_member(person)
    @number_of_members = @number_of_members - 1
    @team.delete person 
end

  def size
    return @number_of_members 
end

## Here I'm using the syntax << to push a new person object into the array
## I'm also passing a person object that I have created into the method add_member
 

def add_member(person)
    @number_of_members = @number_of_members + 1
    @team << person 
end
end

The next thing I want to do is make a program that will allow me to make people, make teams, and add / or subtract the people I've made into the teams I make.

So First of all I'm going to need to call both my team.rb and my person.rb programs...

require 'person'require 'team'

## I'm going to make a team called Wesley.
## that team will have be called the A- team, have no members and it's team leader will be linda
## It will also create an array.


wesley = Team.new("The A-Team",0,"linda")my_team = Team.new("The Best Team of Course",0,"Ciaran")

## I'm also going to make a person, and that person will be called Hannibal Smith
## and was born on new years day 40 years ago.  But the object person will simply be called Hannibal
## and I'll make another person object and he'll be called face.


hannibal = Person.new("Hanibal Smith", 1-1-1970)
face = Person.new("Templeton Peck", 1-1-1976)

## I just put this line in so I could make sure that the file was calling the person.rb file ok
## so If it prints "Hello my name is Hannibal Smith" I'll be doing ok.


puts hannibal.say_hello

## So now I have a Team object and a Person object
## and I want to add my person object face to my team object wesley
## The team object wesley has an array so hopefully I can put the person into that array.
## remember the def add_member(person) bit of my team class well that allows me to pass the
## person object face into the method add_member
## there was also the bit @team << person that should push my face object into the wesley array.
## and ditto for the hannibal object into my_team array.


wesley.add_member face
my_team.add_member hannibal

puts wesley.size #=> 2
wesley.lose_member face
puts wesley.size #=> 1
puts my_team.size

my_team.add_member face
puts face.say_hello
puts my_team.size

And the output I got from all of that was....

Hi, my name is Hanibal Smith
nil
1
0
1
Hi, my name is Templeton Peck
nil
2
Which looks like its adding and subtracting team members alright.  I'm not sure why I'm getting the Nil bit.  But for today I've done enough.  Tomorrow I'm going to try to get my head around the arrays.  So I might mess a bit with putsing the arrays so I can see what's going on with those.

Spread The Love, Share Our Article

Related Posts

No Response to "The Team / Person / Require exercise"

Post a Comment