---
# System prepended metadata

title: Task 1 (Use class)
tags: [Gemcamp]

---

# Task 1 (4 hours)

Set up a new repository named ruby_task and create a file named main.rb with the code

   ```ruby
        puts 'Hello World'
   ```

### Create an Array of People

* Remove the puts "Hello, World!" line.
* Create an array of hashes, where each hash represents a person. The hashes should include the following fields: national_id, name, and age.
* Add at least 20 people with unique records.
* Print the entire list of people after creating the array.
### Add a New User

* Write a script that prompts the user to input a national_id, name, and age to add a new person to the list.
* Ensure that the national_id must be unique. If the national_id is already in the list, print an error message like "Failed to add: National ID already exists."
* If the user is added successfully, print a success message like "User added successfully!"
### Add or Delete a User

* Modify the program to allow the user to choose between two actions: add_user or delete_user.
* If the user selects delete_user, prompt for the national_id to delete a person from the list.
* If the national_id is not found, print "User not found."
* If the national_id is found, delete the user and print "Successfully deleted."
### Display the List of Records

* After each action (whether adding or deleting), display the updated list of people.
* The most recently added records should appear at the top of the list.
### Repeat Actions Using a Loop

1. Use a while loop to keep the program running until the user decides to stop (exit keyword).
1. After each action (add_user or delete_user), allow the user to perform another action or exit the program.


## Bonus questions 

### Display 5 users only
* Display only the last 5 users

### Search a User
* Allow users to search for a user by national_id or name and display the user’s details if found otherwise print "User not dounf".

### Edit User
* Add a new option to allow users to edit (edit_user) an existing user's details (name or age) by searching with national_id. 

* If the user chooses to edit_user, prompt for the national_id, and allow them to update the user's name and age.


### Exiting the loop

* Add a confirmation message `("Are you sure you want to exit (y/n)?")` when user choose the `exit` option 


## Classes

### Create the Person Class:

The class should have the following attributes: `name`, `age`, and `national_id`.

### Create the save Instance Method
* The class should have the following attributes: `name`, `age`, and `national_id`.
### Create the save Instance Method
* This method should save a record (a new person) into a class variable `@@records`, which will store all person instances.
* The records are originally set up in an array of hashes. Make sure that when calling save, each person instance is saved to `@@records`.
### Create the all Class Method
* This method should return all the person instances stored in @@records.
### Create the `first` Instance Method
* This method should return the first person record from `@@records`.
### Create the `last` Instance Method
* This method should return the last person record from `@@records`.
### Create the display Instance Method
* This method should print the `name`, `age`, and `national_id` of a person.
### Create a find_by_national_id Class Method
* This method should accept a national_id as a parameter and return the person instance that matches that ID, or nil if no match is found.
### Create an `update` Instance Method
* This method should update the current instance from the `@@records` class variable.
### Create a `destroy` Instance Method
* This method should remove the current instance from the `@@records` class variable.
### Create a `destroy_all` Class Method
* This method should reset the current `@@records` to `[]`.
### Create a `count` Class Method
* This method should return the number of person instances stored in `@@records`.