-1

It stores the first employee, but when I try to add another employee I am unable to view the additional employees entered. Below is the method for viewing employee record. Thanks for any help!

Employee Class

class Employee
  attr_accessor :employee, :role, :location

  def initialize(employee, role, location)
    @employee = employee
    @role = role
    @location = location
  end

  def employee_change(new_emp)
    @employee = new_emp
  end

  def role_change(new_role)
    @role = new_role
  end.


  def location_change(new_loc)
    @location = new_loc
  end
end

Main Menu

def main_menu
  puts "Welcome to the Employee Portal"
  puts "Please select an option below: "
  puts "---------------------"
  puts "1. Create New Employee Record."
  puts "2. View an existing record."
  puts "3. Modify an existing record."
  puts "4. Exit Portal"

  option = gets.chomp.to_i

  if option == 1
    create_record
    main_menu

  elsif option == 2
    view_record

  elsif option == 3
    modify

  elsif option == 4
    puts "Thank you for using the Employee Portal"
    exit
  else
    puts "Not a valid option. Please try again."
    main_menu
    system("clear")

  end
end

Create New Employee (Option 1 from Main Menu)

def create_record
  puts "Create New Employee Record, click 'Enter' to begin"
  puts "Enter Employee Information: "
  employee = gets.chomp.capitalize

  puts "Enter Employee's Role: "
  role = gets.chomp.capitalize

  puts "Enter Employee's Current Work Location: "
  location = gets.chomp.capitalize

  puts "\n"

  new_record = Employee.new(employee, role, location)
  @record.push(new_record)

  puts "New Employee Record has been created."
  puts "Name: #{employee}"
  puts "Role: #{role}"
  puts "Location: #{location}"

  system("clear")

  main_menu
end

View Employee (Option 2 from Main Menu)

def view_record
  puts "Enter Employee Name to view record: "
  name = gets.chomp.capitalize
  system("clear")

  @record.each do |a|
    if a.employee == name

      puts "\n"
      puts "Employee Information "
      puts "--------------------"
      puts " Name :  #{a.employee}"
      puts " Role(s) :  #{a.role}"
      puts " Location(s) :  #{a.location}"
      puts " Type 'Exit' to return to the Main Menu "

    else
      puts "That is not a valid entry, please try again."
      view_record
      main_menu
    end
  end
end

Modify Employee (Option 3 from Main Menu)

def modify
  system("clear")
  puts "Enter employee name to modify existing record: "
  name = gets.chomp.capitalize

  @record.each do |r|
    if r.employee == name
      puts "Employee found."
      puts "Select an option to modify."
      puts "-----------------------------------"
      puts "1.) Modify employee's name."
      puts "2.) Modify employee's role."
      puts "3.) Modify employee's location."
      puts "4.) Return to Main Menu"
      puts "\n"

      option = gets.chomp.to_i

      if option == 1
        change_employee

      elsif option == 2
        change_role

      elsif option == 3
        change_location

      elsif option == 4
        main_menu
      else
        puts "Invalid selection. Please try again."
        modify
      end
    end
  end
end

Change Employee (Option 1 from Modify)

def change_employee
  puts "Enter new employee name: "
  new_emp = gets.chomp.capitalize
  @record.each do |r|
    if r.employee == employee
      r.employee_change(new_emp)
      puts "#{r.employee} has been updated to #{r.employee}"
    end
  end
end

Change Employee Role (Option 2 from Modify)

def change_role
  puts "What is #{r.employee}\'s new role?: "
  new_role = gets.chomp.capitalize
  @record.each do |r|
    if r.employee == employee
      r.role_change(new_role)
      puts "#{r.employee}\'s new role is #{r.role}"
    end
  end
end

Change Employee Location (Option 3 from Modify)

def change_location
  puts "What is #{r.employee}\'s new location?: "
  new_loc = gets.chomp.capitalize
  @record.each do |r|
    if r.employee == employee
      r.location_change(new_loc)
      puts "#{r.employee} has been transfer to new location, #{r.location}."
    end
  end
end

Run the Program

@record = []
system("clear")
main_menu
Simple Lime
  • 10,790
  • 2
  • 17
  • 32
  • 1
    This is not an answer, but a suggestion. Typically in ruby style you would name an array/list `@records` rather than `@record`, just because it is more clear. Most developers will see @record and think 'single database record'. – whodini9 Aug 02 '17 at 20:55
  • 6
    What actually happens? How do you know it doesn't work? Have you verified the array contains what you think it does? – Dave Newton Aug 02 '17 at 21:42
  • 3
    If you have to say “unable to” in describing what is happening you're probably not making your problem clear enough. Did the code physically prevent you from doing something? Does the mouse bite you when you try to invoke a particular action? Does the application crash? ;) – coreyward Aug 02 '17 at 22:07
  • @whodini9 thank you I will update that! – sullymodena Aug 03 '17 at 12:44
  • @DaveNewton When I run the program I add new employee's but for some reason I can't view any other employee beyond the first employee I add, also after the new_record variable it doesn't display that the employee has been created, etc. – sullymodena Aug 03 '17 at 12:48
  • It displays it, but then you're immediately calling out to `system("clear")` and removing the text you had just output, see my answer below for why you can't view any employee past the first employee you added – Simple Lime Aug 04 '17 at 02:59

1 Answers1

0

The problem is with your view_record method. If you change it to look something like:

def view_record
  puts "Enter Employee Name to view record: "
  name = gets.chomp.capitalize
  system("clear")

  if a = @record.detect { |rec| rec.employee == name }
    puts "\n"
    puts "Employee Information "
    puts "--------------------"
    puts " Name :  #{a.employee}"
    puts " Role(s) :  #{a.role}"
    puts " Location(s) :  #{a.location}"
    puts " Type 'Exit' to return to the Main Menu "
  else
    puts "That is not a valid entry, please try again."
    view_record
  end
end

It works properly.

The problem was that you were calling the if...else statement on every record in @record. So if you create 2 Employees the first one named "John" and the second named "Jane". When you go to view "Jane", you call the else portion for "John", since he's the first record and then once he finishes the else you call the if portion for "Jane". The else for "John", however, never returns because after you finally finish a [possibly lengthy] stack of view_record calls, with the same issue, you then go back to main_menu which never returns (due to the final else condition in that method that re-calls main_menu)

Hope that helps and makes sense.

Notes:

  1. The modify seems to have the same issue and the change_x methods will loop through and check each employee against the if statement, but since no else no problem (I'd still change them personally, to use the detect on these as well).
  2. The change_x methods don't look like they would run, because employee isn't defined in them
  3. If more than 1 employee can have the same name, you can use select instead of detect and then check for empty? or iterate through only those returned by select and call only the if portion on them.
Simple Lime
  • 10,790
  • 2
  • 17
  • 32