From 6e3eafdaaaaa56d259e9ba060d99cf3af0c15271 Mon Sep 17 00:00:00 2001 From: Kate Date: Tue, 26 Feb 2019 23:27:31 -0800 Subject: [PATCH 1/2] finishing project --- main.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ planet.rb | 20 +++++++++++++++++++ solar_system.rb | 26 +++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 main.rb create mode 100644 planet.rb create mode 100644 solar_system.rb diff --git a/main.rb b/main.rb new file mode 100644 index 00000000..48237c31 --- /dev/null +++ b/main.rb @@ -0,0 +1,52 @@ +require "terminal-table" +require_relative "planet" +require_relative "solar_system" + +def main + solar_system = SolarSystem.new("A Wrinkle in Time") + + camazotz = Planet.new(“Camazotz”, “red”, 68.9, 22.777, “Ruled by a disembodied brain”) + ixchel = Planet.new(“Ixchel”, “pink”, 49, 10.8E9, “Inhabited by sightless creatures”) + uriel = Planet.new(“Uriel”, “blue”, 10, 411.5E1, “Lots of extremely tall mountains”) + + solar_system.add_planet(camazotz) + solar_system.add_planet(ixchel) + solar_system.add_planet(uriel) + + directions = "" + + until directions == "exit" + puts "What would you like to do? Enter LIST PLANETS, PLANET DETAILS, ADD PLANET, or EXIT." + directions = gets.chomp.downcase + if directions == "list planets" + list = solar_system.list_planets + puts list + elsif directions == "planet details" + puts "Which planet are you interested in?" + show_details = gets.chomp + found_planet = solar_system.find_planet_by_name(show_details) + puts found_planet.summary + elsif directions == "add planet" + planet_new = add_new_planet + solar_system.add_planet(planet_new) + else + puts "\nGood bye!" + end + end +end + +def add_new_planet + puts "What is the name of the planet?" + name = gets.chomp.capitalize + puts "What color is the planet?" + color = gets.chomp.downcase + puts "What is the planet's mass in kilograms?" + mass_kg = gets.chomp.to_f + puts "What is the planet's distance from A Wrinkle in Time?" + distance_from_sun_km = gets.chomp.to_i + puts "What is a fun fact about the planet?" + fun_fact = gets.chomp + new_planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) +end + +main diff --git a/planet.rb b/planet.rb new file mode 100644 index 00000000..cd32f251 --- /dev/null +++ b/planet.rb @@ -0,0 +1,20 @@ +require "terminal-table" + +class Planet + 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 + + attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact + + def summary + table = Terminal::Table.new :headings => ["Name", "Color", "Mass (kg)", "Distance From Sun (km)", "Fun Fact"] do |t| + t.add_row [@name, @color, @mass_kg, @distance_from_sun_km, @fun_fact] + end + return table + end +end diff --git a/solar_system.rb b/solar_system.rb new file mode 100644 index 00000000..3700fcc2 --- /dev/null +++ b/solar_system.rb @@ -0,0 +1,26 @@ +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 + list = "Planets orbiting #{@star_name}:\n" + @planets.each_with_index do |planets, number| + item = "#{number + 1}. #{planets.name}\n" + list += item + end + return list + end + + def find_planet_by_name(find_planet) + repeat_name = @planets.find { |planet_name| planet_name.name == find_planet.capitalize } + return repeat_name + end +end From aa448ec416801d0feacea9b4e0354a581a7a49c4 Mon Sep 17 00:00:00 2001 From: Kate Nichols <43222927+KateAnnNichols@users.noreply.github.com> Date: Tue, 26 Feb 2019 23:35:37 -0800 Subject: [PATCH 2/2] Update main.rb --- main.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.rb b/main.rb index 48237c31..85dabf01 100644 --- a/main.rb +++ b/main.rb @@ -16,13 +16,13 @@ def main directions = "" until directions == "exit" - puts "What would you like to do? Enter LIST PLANETS, PLANET DETAILS, ADD PLANET, or EXIT." + puts "Please enter list planets to see all the planets, planet details to learn more about each planet, or add planet to enter a new planet. directions = gets.chomp.downcase - if directions == "list planets" + if directions == "list planets"EXIT list = solar_system.list_planets puts list elsif directions == "planet details" - puts "Which planet are you interested in?" + puts "Please enter the planet name you'd like to see details for?" show_details = gets.chomp found_planet = solar_system.find_planet_by_name(show_details) puts found_planet.summary @@ -30,7 +30,7 @@ def main planet_new = add_new_planet solar_system.add_planet(planet_new) else - puts "\nGood bye!" + puts "\nThanks!" end end end