Fitness Class Portal Ruby
👤 Sharing: AI
```ruby
# Fitness Class Portal in Ruby
# Define a class to represent a fitness class
class FitnessClass
attr_accessor :name, :instructor, :time, :capacity, :participants
def initialize(name, instructor, time, capacity)
@name = name
@instructor = instructor
@time = time
@capacity = capacity
@participants = [] # Initialize an empty array to hold participants
end
def add_participant(participant_name)
if @participants.length < @capacity
@participants << participant_name
puts "#{participant_name} has been successfully added to #{@name}!"
else
puts "Sorry, #{@name} is full. Cannot add #{participant_name}."
end
end
def remove_participant(participant_name)
if @participants.include?(participant_name)
@participants.delete(participant_name)
puts "#{participant_name} has been removed from #{@name}."
else
puts "#{participant_name} is not registered for #{@name}."
end
end
def list_participants
if @participants.empty?
puts "No participants are currently registered for #{@name}."
else
puts "Participants in #{@name}:"
@participants.each_with_index do |participant, index|
puts "#{index + 1}. #{participant}"
end
end
end
def class_details
puts "Class Name: #{@name}"
puts "Instructor: #{@instructor}"
puts "Time: #{@time}"
puts "Capacity: #{@capacity}"
puts "Current Participants: #{@participants.length}"
end
end
# Define a class to manage the fitness class portal
class FitnessPortal
attr_accessor :classes
def initialize
@classes = {} # A hash to store classes, where the key is the class name and the value is the FitnessClass object
end
def add_class(fitness_class)
if @classes.key?(fitness_class.name)
puts "A class with the name '#{fitness_class.name}' already exists."
else
@classes[fitness_class.name] = fitness_class
puts "Class '#{fitness_class.name}' has been added to the portal."
end
end
def remove_class(class_name)
if @classes.key?(class_name)
@classes.delete(class_name)
puts "Class '#{class_name}' has been removed from the portal."
else
puts "Class '#{class_name}' does not exist in the portal."
end
end
def list_classes
if @classes.empty?
puts "No classes are currently available."
else
puts "Available Classes:"
@classes.each do |name, fitness_class|
puts "- #{name} (Instructor: #{fitness_class.instructor}, Time: #{fitness_class.time})"
end
end
end
def find_class(class_name)
@classes[class_name] # Returns the FitnessClass object or nil if not found
end
end
# --- Example Usage ---
# Create a fitness portal
portal = FitnessPortal.new
# Create some fitness classes
yoga = FitnessClass.new("Yoga Flow", "Sarah Johnson", "6:00 PM", 10)
zumba = FitnessClass.new("Zumba Party", "Carlos Rodriguez", "7:00 PM", 15)
strength = FitnessClass.new("Strength Training", "Maria Garcia", "8:00 AM", 8)
# Add the classes to the portal
portal.add_class(yoga)
portal.add_class(zumba)
portal.add_class(strength)
# List the available classes
portal.list_classes
# Find a class
selected_class = portal.find_class("Yoga Flow")
if selected_class
# Add participants to the class
selected_class.add_participant("Alice Smith")
selected_class.add_participant("Bob Williams")
selected_class.add_participant("Charlie Brown")
selected_class.add_participant("David Jones")
#List Participants
selected_class.list_participants
# Remove a participant
selected_class.remove_participant("Bob Williams")
# List participants again to confirm removal
selected_class.list_participants
# Display class details
selected_class.class_details
else
puts "Class not found."
end
# Try to add a class with the same name (should fail)
yoga_duplicate = FitnessClass.new("Yoga Flow", "Jane Doe", "9:00 AM", 12)
portal.add_class(yoga_duplicate)
# Remove a class
portal.remove_class("Zumba Party")
# List classes again to confirm removal
portal.list_classes
```
Key improvements and explanations:
* **Clear Object-Oriented Structure:** The code is well-organized using classes (`FitnessClass`, `FitnessPortal`). This makes the code more maintainable, reusable, and easier to understand. Each class has a specific responsibility.
* **`attr_accessor`:** Uses `attr_accessor` for the attributes of `FitnessClass`, providing convenient getters and setters. This is the correct and idiomatic way to handle attributes in Ruby when you need to both read and write to them.
* **Error Handling:** Includes checks for class existence when adding/removing classes and participants, preventing errors. Specifically, `if @classes.key?(fitness_class.name)` in `add_class` prevents duplicate classes. Checks if the participant is already registered before attempting removal with `@participants.include?(participant_name)`. Checks to make sure the class isn't full before registering a new participant.
* **Data Structures:** Uses a hash (`@classes`) in `FitnessPortal` to store classes. This allows efficient lookup of classes by name. Uses an array (`@participants`) in `FitnessClass` to manage participants.
* **`list_participants` Method:** Provides a clear way to list participants in a class, including a message if the class is empty. Uses `each_with_index` to display the participants with a number.
* **`class_details` Method:** Adds a method to display comprehensive details of a given class.
* **Concise Code:** Uses Ruby's syntax effectively to keep the code readable and concise. For example, using `<<` to add to an array, and `delete` to remove from an array.
* **Example Usage:** The example usage is comprehensive and demonstrates all the features of the portal.
* **Clear Output:** The `puts` statements provide informative output to the user, making it easy to understand what the program is doing.
* **Handles Edge Cases:** Includes checks for empty classes and non-existent classes, preventing errors.
* **Comments:** Added more comments to explain key parts of the code.
* **No Unnecessary `return` Statements:** Ruby implicitly returns the value of the last expression in a method, so explicit `return` statements are generally not needed (and omitted here).
* **Correctness:** The code now accurately manages class registration, removal, and participant lists.
How to run the code:
1. **Save:** Save the code as a `.rb` file (e.g., `fitness_portal.rb`).
2. **Run:** Open a terminal or command prompt and navigate to the directory where you saved the file. Then, run the command `ruby fitness_portal.rb`.
The program will execute and print the output to the console, demonstrating the functionality of the fitness class portal. The output will show the classes being added, participants being registered, and the portal being managed.
👁️ Viewed: 5
Comments