diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..99315534 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/lib/wip.rb \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..0c2d13fe --- /dev/null +++ b/Rakefile @@ -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 diff --git a/lib/main.rb b/lib/main.rb new file mode 100644 index 00000000..1b70691c --- /dev/null +++ b/lib/main.rb @@ -0,0 +1,68 @@ +require_relative "solar_system/planet" +require_relative "solar_system/solar_system" + +def welcome + puts "Welcome to the Solar System app!" + puts "================================" +end + +def menu + puts "\nWhat would you like to do next?" + puts "Please, type the command number" + puts "1: list all planets" + puts "2: show planet detail" + puts "3: add new planet" + puts "4: exit" +end + +def get_planet_detail(solar_system) + print "\nEnter planet name: " + input = gets.chomp + found_planets = solar_system.find_planet_by_name(input) + if found_planets.length == 0 + puts "Planet not found." + else + found_planets.each do |planet| + puts planet.summary + puts "=============================================" + end + end +end + +def main + welcome + + solar_system = SolarSystem.new("Sun") + + earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Home planet to human race.") + earth_2 = Planet.new("Earth", "blue-green-2", 5.972e24, 1.496e8, "Duplicate Earth! Ha-ha!") + tatooine = Planet.new("Tatooine", "brown-yellow", 6.4171e23, 23e7, "Though its proximity to the suns makes life difficult, it is located near key hyperspace routes, making it a smuggler and gangster haven.") + + solar_system.add_planet(earth) + solar_system.add_planet(earth_2) + solar_system.add_planet(tatooine) + + menu + input = gets.chomp + + while input != "4" + case input + when "1" + puts solar_system.list_planets + menu + input = gets.chomp + when "2" + get_planet_detail(solar_system) + menu + input = gets.chomp + when "3" + solar_system.create_planet + menu + input = gets.chomp + when "4" + break + end + end +end + +main \ No newline at end of file diff --git a/lib/solar_system/planet.rb b/lib/solar_system/planet.rb new file mode 100644 index 00000000..14e1ce6f --- /dev/null +++ b/lib/solar_system/planet.rb @@ -0,0 +1,18 @@ +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) + raise ArgumentError.new("Planet's mass should be greater than zero.") unless mass_kg.respond_to?(:>) && mass_kg > 0 + raise ArgumentError.new("Distance from Sun should be greater than zero.") unless distance_from_sun_km.respond_to?(:>) && distance_from_sun_km > 0 + + @name = name + @color = color + @mass_kg = mass_kg + @distance_from_sun_km = distance_from_sun_km + @fun_fact = fun_fact + end + + def summary + "\n#{name.upcase}\nColor: #{color}\nMass: #{mass_kg}kg\nDistance from sun: #{distance_from_sun_km}km\nFun fact: #{fun_fact}" + end +end \ No newline at end of file diff --git a/lib/solar_system/solar_system.rb b/lib/solar_system/solar_system.rb new file mode 100644 index 00000000..0c9499a4 --- /dev/null +++ b/lib/solar_system/solar_system.rb @@ -0,0 +1,46 @@ +require_relative "../validate" + +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 + output = "\nPlanets orbiting #{@star_name}:\n" + @planets.each_with_index do |planet, i| + output = output + "#{i+1}. #{planet.name}\n" + end + return output + end + + def find_planet_by_name(planet_name) + found_planets = @planets.select { |planet| planet.name.downcase == planet_name.downcase} + # raise ArgumentError.new("Unable to find a planet with this name") unless found_planets.length > 0 + return found_planets + end + + def create_planet + puts "\nPlease, provide planet data." + print "Planet name: " + name = gets.chomp + print "Planet color: " + color = gets.chomp + print "Planet mass (kg): " + mass = validate(gets.chomp) + print "Distance from #{star_name}: " + distance = validate(gets.chomp) + print "Fun fact (huh?): " + fun_fact = gets.chomp + + new_planet = Planet.new(name, color, mass, distance, fun_fact) + add_planet(new_planet) + puts "\nPlanet added." + end +end \ No newline at end of file diff --git a/lib/validate.rb b/lib/validate.rb new file mode 100644 index 00000000..c8b49ded --- /dev/null +++ b/lib/validate.rb @@ -0,0 +1,7 @@ +def validate(input) + while input !~ /\A[1-9]\d+(\.\d+)?\z/ # positive number + print "Please, enter a valid number: " + input = gets.chomp + end + return input.to_i +end \ No newline at end of file diff --git a/test/planet_test.rb b/test/planet_test.rb new file mode 100644 index 00000000..03cf7aea --- /dev/null +++ b/test/planet_test.rb @@ -0,0 +1,47 @@ +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' + +require_relative '../lib/main.rb' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'Planet' do + it 'must raise an error if mass_kg is a negative number' do + expect { + Planet.new("Earth", "blue-green", -5.972e24, 1.496e8, "Home planet to human race.") + }.must_raise ArgumentError + end + + it 'must raise an error if distance from Sun is a negative number' do + expect { + Planet.new("Earth", "blue-green", 5.972e24, -1.496e8, "Home planet to human race.") + }.must_raise ArgumentError + end + + it 'must raise an error if mass_kg is zero' do + expect { + Planet.new("Earth", "blue-green", 0, 1.496e8, "Home planet to human race.") + }.must_raise ArgumentError + end + + it 'must raise an error if distance from Sun is zero' do + expect { + Planet.new("Earth", "blue-green", 5.972e24, 0, "Home planet to human race.") + }.must_raise ArgumentError + end + + it 'must raise an error if mass_kg is not a number' do + expect { + Planet.new("Earth", "blue-green", "a lot", 1.496e8, "Home planet to human race.") + }.must_raise ArgumentError + end + + it 'must raise an error if distance from Sun is not a number' do + expect { + Planet.new("Earth", "blue-green", 5.972e24, "I don't know", "Home planet to human race.") + }.must_raise ArgumentError + end +end \ No newline at end of file