-
Notifications
You must be signed in to change notification settings - Fork 48
Sockets -Maria #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Sockets -Maria #33
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # main.rb | ||
| require_relative 'planet' | ||
| require_relative 'solar_system' | ||
|
|
||
| def main | ||
| solar_system = SolarSystem.new('Sol') | ||
|
|
||
| earth = Planet.new('earth', 'blue-green', '5.972e24', '1.496e8', 'only planet known to support life') | ||
| solar_system.add_planet(earth) | ||
| mars = Planet.new('mars', 'reddish-brown','0.64171','141.6 million mi','it has the longest rotation period, rotates in the opposite direction to most other planets') | ||
| solar_system.add_planet(mars) | ||
| jupiter = Planet.new('jupiter','orange with white bands','1,898.19','483.8 million mi','the largest in the Solar System') | ||
| solar_system.add_planet(jupiter) | ||
| saturn = Planet.new('saturn','pale-gold','5.9724','890.8 million mi','second-largest in the Solar System. It is a gas giant a radius nine times that of Earth') | ||
| solar_system.add_planet(saturn) | ||
|
|
||
| list = solar_system.list_planets | ||
| puts list | ||
|
|
||
| found_planet = solar_system.find_planet_by_name("earth") | ||
| puts found_planet.summary | ||
|
|
||
| options = [ "list planets", "get planet details", "add a planet", "exit"] | ||
|
|
||
| puts "What would you like to do, please choose from the following options:" | ||
| puts options | ||
| user_input = gets.chomp.downcase | ||
|
|
||
| while options.include? user_input | ||
| case user_input | ||
| when "list planets" | ||
| puts solar_system.list_planets | ||
| when "get planet details" | ||
| puts solar_system.planet_details.summary | ||
| when "add a planet" | ||
| puts solar_system.new_planet | ||
| when "exit" | ||
| exit | ||
| end | ||
|
|
||
| puts "What would you like to do next?" | ||
| user_input = gets.chomp.downcase | ||
| end | ||
| end | ||
|
|
||
| main | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
| class Planet | ||
| attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact | ||
|
|
||
| def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact) | ||
| @name = name | ||
| @color = color | ||
| @mass_kg = mass_kg | ||
| @distance_from_sun_km = distance_from_sun_km | ||
| @fun_fact = fun_fact | ||
| end | ||
|
|
||
| def summary | ||
| return "#{@name.capitalize}, has a #{color} color, with a mass of #{mass_kg}, holding a distance from the sun of #{distance_from_sun_km}, a fun fact is: #{fun_fact}" | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Solar System | ||
| class SolarSystem | ||
| attr_reader :star_name, :planets | ||
|
|
||
| def initialize(star_name) | ||
| @star_name = star_name | ||
| @planets = [] | ||
| end | ||
|
|
||
| def add_planet(planet) | ||
| @planets << planet | ||
| end | ||
|
|
||
| def list_planets | ||
| i = 0 | ||
| list = [] | ||
| @planets.each do |planet| | ||
| list << "#{i += 1}. #{planet.name}" | ||
| end | ||
| puts "Planets orbiting #{star_name}:" | ||
| return list | ||
| end | ||
|
|
||
| def find_planet_by_name(name) | ||
| @planets.each do |planet| | ||
| if planet.name == name.downcase | ||
| return planet | ||
| elsif planet.name != name.downcase | ||
| puts "no related data found, please enter a listed planet" | ||
| planet_details | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very dangerous way to call this method! Calling a method inside itself is called recursion, and if the method has a bug, there is no escape except to shut the program down. |
||
| end | ||
| end | ||
| end | ||
|
|
||
| def planet_details | ||
| puts "Enter the planet you want a little more info of" | ||
| user_input = gets.chomp.downcase | ||
|
|
||
| planet_info = find_planet_by_name(user_input) | ||
| return planet_info | ||
| end | ||
|
|
||
| def new_planet | ||
| puts "We will need the following planet information:" | ||
|
|
||
| puts "Planet name:" | ||
| name = gets.chomp.downcase | ||
|
|
||
| puts "Planet color:" | ||
| color = gets.chomp.downcase | ||
|
|
||
| puts "Planet Mass:" | ||
| mass = gets.chomp.to_i | ||
|
|
||
| puts "Planet Distance from the Sun:" | ||
| distance_from_sun = gets.chomp | ||
|
|
||
| puts "Planet Fun Fact:" | ||
| fun_fact = gets.chomp_to_i | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple of weird things are happening here. 1) You use |
||
|
|
||
| new_planet = Planet.new(name, color, mass, distance_from_sun, fun_fact) | ||
| add_planet(new_planet) | ||
|
|
||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it doesn't find the planet? you should probably return
planet.summaryfromplanet_detailsinstead.