GoMMA
Creating a simple Ruby application that sends martial artists to the homes of their patrons and students involves multiple components. For this example, let's construct a basic command-line application that allows patrons to request a martial artist to come to their home. We'll keep it straightforward, using simple in-memory storage for our data.
### Requirements
1. A `MartialArtist` class to represent individual martial artists.
2. A `Patron` class to represent patrons requesting martial artists.
3. A service class to handle the logic of sending martial artists to patrons.
4. A simple user interface in the command line.
### Step 1: Setting Up the Classes
We'll start by defining our classes.
```ruby
# martial_artist.rb
class MartialArtist
attr_accessor :name, :skills
def initialize(name, skills)
@name = name
@skills = skills
end
def to_s
"#{name} (Skills: #{skills.join(", ")})"
end
end
# patron.rb
class Patron
attr_accessor :name, :address
def initialize(name, address)
@name = name
@address = address
end
def to_s
"#{name} (Address: #{address})"
end
end
# martial_artist_service.rb
class MartialArtistService
attr_accessor :martial_artists, :patrons
def initialize
@martial_artists = []
@patrons = []
end
def add_martial_artist(martial_artist)
@martial_artists << martial_artist
end
def add_patron(patron)
@patrons << patron
end
def send_martial_artist_to_patron(patron_name)
patron = @patrons.find { |p| p.name == patron_name }
if patron.nil?
puts "Patron not found!"
return
end
martial_artist = @martial_artists.sample
puts "Sending #{martial_artist} to #{patron}"
end
end
```
### Step 2: Creating the Application Interface
Now, let's set up a simple command-line interface for the application.
```ruby
# app.rb
require_relative 'martial_artist'
require_relative 'patron'
require_relative 'martial_artist_service'
def display_menu
puts "1. Add Martial Artist"
puts "2. Add Patron"
puts "3. Send Martial Artist to Patron"
puts "4. Exit"
end
def main
service = MartialArtistService.new
loop do
display_menu
choice = gets.chomp.to_i
case choice
when 1
puts "Enter Martial Artist's Name:"
name = gets.chomp
puts "Enter Martial Artist's Skills (comma separated):"
skills = gets.chomp.split(",").map(&:strip)
artist = MartialArtist.new(name, skills)
service.add_martial_artist(artist)
puts "Martial Artist added!"
when 2
puts "Enter Patron's Name:"
name = gets.chomp
puts "Enter Patron's Address:"
address = gets.chomp
patron = Patron.new(name, address)
service.add_patron(patron)
puts "Patron added!"
when 3
puts "Enter Patron's Name to send Martial Artist:"
patron_name = gets.chomp
service.send_martial_artist_to_patron(patron_name)
when 4
puts "Exiting the application."
break
else
puts "Invalid choice. Please try again."
end
end
end
main
```
### Step 3: Running the Application
To run the application, create a directory for your application, create the three Ruby files (`martial_artist.rb`, `patron.rb`, `martial_artist_service.rb`, and `app.rb`) containing the above code, and then run the `app.rb` file using the following command in your terminal:
```bash
ruby app.rb
```
### Summary
This simple Ruby application allows you to manage a list of martial artists and patrons, as well as send martial artists to patrons based on their requests. You can enhance this further by adding features such as saving the data to a file or a database, implementing more complex logic for matching patrons and martial artists, or even creating a web interface.
Comments
Post a Comment