diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..de5d5f33 --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,114 @@ +require_relative 'planet' +require_relative 'solar_system' + +def user_choice + puts "\nWhat would you like to do? Choose from the following options:" + puts "List Planets\nPlanet Details\nAdd Planet\nGet Distance\nExit" + choice = gets.chomp.downcase + until choice == "list planets" || choice == "exit" || choice == "planet details" || choice == "add planet" || choice == "get distance" + puts "NOT A VALID OPTION\n\n" + choice = user_choice + end + return choice +end + +def planet_details(planet, solar_system) + which_planet = solar_system.find_planet_by_name(planet) + return which_planet.summary +end + +def pre_existing_planets(solar_system) + return solar_system.list_planets +end + +def add_planet(solar_system) + print "Please enter the new planet name: " + new_planet = gets.chomp.downcase + check_planet = solar_system.find_planet_by_name(new_planet) + + if check_planet == nil + print "What is planet's color? " + new_color = gets.chomp + + print "What is planet's mass (in Kg)? " + new_mass = gets.chomp.to_f + + print "What is planet's distance from the Sun (in Km)? " + new_distance = gets.chomp.to_i + + print "Add a fun fact about #{new_planet}: " + new_fact = gets.chomp + + brand_new = Planet.new(new_planet.capitalize, new_color.capitalize, new_mass, new_distance, new_fact) + solar_system.add_planet(brand_new) + + puts "New planet added: #{brand_new.name}.\n" + puts brand_new.summary + else + puts "This planet is already listed in the Solar System." + end + return brand_new +end + +def get_distance(solar_system) + distant_planets = [] + 2.times do + print "Please enter one planet: " + plan1 = gets.chomp + check_plan1 = solar_system.find_planet_by_name(plan1) + if check_plan1.nil? + puts "This planet is not listed in the Solar System" + return + else + distant_planets << plan1 + end + end + return solar_system.distance_between(distant_planets[0], distant_planets[1]) + +end + +def main + earth = Planet.new("Earth", "Blue", 5.972e24, 1.496e8, 'Only planet known to support life') + mars = Planet.new("Mars", "Red", 6.41693e23, 230e8, 'Carries the name of the Roman God of War') + jupiter = Planet.new("Jupiter", "Yellow", 1.898e27, 817e6, 'The largest planet in the Solar System.') + saturn = Planet.new("Saturn", "Pale yellow", 5.69e26, 1.5e9, "Has rings made of ice mixed with dust and other chemicals.") + uranus = Planet.new("Uranus", "Blue-green", 8.68e25, 3e9, 'The coldest of the planets.') + + solar_system = SolarSystem.new('Sun') + solar_system.add_planet(earth) + solar_system.add_planet(mars) + solar_system.add_planet(jupiter) + solar_system.add_planet(saturn) + solar_system.add_planet(uranus) + + #CLI + keep_going = true + + while keep_going + choice = user_choice + if choice == "exit" + puts "Good-bye" + return + elsif choice == "list planets" + puts pre_existing_planets(solar_system) + "\n" + elsif choice == "planet details" + puts pre_existing_planets(solar_system) + "\n" + print "Choose a Planet and I will tell you about it: " + chosen_planet = gets.chomp + check = solar_system.find_planet_by_name(chosen_planet.downcase) + + if check.nil? + puts "It looks like #{chosen_planet} is not in this Solar System" + else + puts planet_details(chosen_planet, solar_system) + "\n" + end + + elsif choice == "add planet" + puts add_planet(solar_system) + + elsif choice == "get distance" + puts get_distance(solar_system) + end + end +end +main \ No newline at end of file diff --git a/lib/planet.rb b/lib/planet.rb new file mode 100644 index 00000000..19bdfe1b --- /dev/null +++ b/lib/planet.rb @@ -0,0 +1,38 @@ +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 + + #wave1 - optional // error checking + if @mass_kg.is_a?(Float) == false && @mass_kg.is_a?(Integer) == false + raise ArgumentError.new("Expected number, got #{@mass_kg}.") + elsif @mass_kg < 0 + raise ArgumentError.new("Expected value greater than zero, got #{@mass_kg}.") + end + + if @distance_from_sun_km.is_a?(Float) == false && @distance_from_sun_km.is_a?(Integer) == false + raise ArgumentError.new("Expected number, got #{@distance_from_sun_km}.") + elsif distance_from_sun_km < 0 + raise ArgumentError.new("Expected value greater than zero, got #{@distance_from_sun_km}.") + end + end + + def summary + summary = " + - Planet: #{@name} + - Color: #{@color} + - Mass (in Kg): #{@mass_kg} + - Distance from the sun (in Km): #{@distance_from_sun_km} + - Fun Fact: #{@fun_fact}" + + return summary + end + + +end \ No newline at end of file diff --git a/lib/solar_system.rb b/lib/solar_system.rb new file mode 100644 index 00000000..0a4b7cf6 --- /dev/null +++ b/lib/solar_system.rb @@ -0,0 +1,38 @@ +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 + str_planets = "Planets orbiting #{self.star_name}:" + @planets.each_with_index do |planet, i| + str_planets += "\n#{i+1}. #{planet.name}" + end + return str_planets + end + + def find_planet_by_name(planet_name) + @planets.each do |planet| + if planet_name.upcase == planet.name.upcase + return planet + end + end + return nil + end + + def distance_between(planet_name1, planet_name2) + planet1 = find_planet_by_name(planet_name1) + planet2 = find_planet_by_name(planet_name2) + + return (planet1.distance_from_sun_km - planet2.distance_from_sun_km).abs + end + + end \ No newline at end of file diff --git a/rakefile.rb b/rakefile.rb new file mode 100644 index 00000000..fb007c61 --- /dev/null +++ b/rakefile.rb @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['test/*_test.rb'] +end + +task default: :test \ No newline at end of file diff --git a/test/planet_test.rb b/test/planet_test.rb new file mode 100644 index 00000000..d2cff7ac --- /dev/null +++ b/test/planet_test.rb @@ -0,0 +1,46 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' + +require_relative '../lib/planet' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +#wave 1 + +describe 'Planet' do + + it 'raises ArgumentError if mass_kg is not a number' do + + expect { + weird_planet = Planet.new("Weird Planet", "Purple", "Ada Lovelace", 20, 'It exist in my dreams') + }.must_raise ArgumentError + + end + + it 'raises ArgumentError for mass_kg smaller than 0' do + + expect { + weird_planet = Planet.new("Weird Planet", "Purple", -4, 20, 'It exist in my dreams') + }.must_raise ArgumentError + + end + + it 'raises ArgumentError if distance_from_sun_km is not a number' do + + expect { + weird_planet = Planet.new("Weird Planet", "Purple", 800, "Giraffe", 'It exist in my dreams') + }.must_raise ArgumentError + + end + + it 'raises ArgumentError for distance_from_sun_km smaller than 0' do + + expect { + weird_planet = Planet.new("Weird Planet", "Purple", 600000, -3, 'It exist in my dreams') + }.must_raise ArgumentError + + end +end \ No newline at end of file