-
Notifications
You must be signed in to change notification settings - Fork 47
Branches - Eve: Solar System #37
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?
Changes from all commits
f5365c3
5b01ef4
0e17d8c
216da74
b662bf4
8b7983b
e024889
a51a176
93e12dc
415faae
5258146
1e8a2c5
9a97d8f
2d95031
d3244c0
33e26a4
e44fbf2
71a0e76
2fc6f2b
d55c784
8ea3909
f573b30
1006676
2cb1ded
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,9 @@ | ||
| require 'rake/testtask' | ||
|
|
||
| Rake::TestTask.new do |t| | ||
| t.libs = ["lib"] | ||
| t.warning = true | ||
| t.test_files = FileList['tests/*_test.rb'] | ||
| end | ||
|
|
||
| task default: :test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class Planet | ||
| attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact | ||
|
|
||
| def initialize(name: , color: , mass_kg:, distance:, fact:) | ||
| @name = name | ||
| @color = color | ||
|
|
||
| raise ArgumentError.new("Planet mass_kg must be a positive number") if mass_kg <= 0 | ||
| @mass_kg = mass_kg | ||
|
|
||
| raise ArgumentError.new("Distance must be a positive number") if distance <= 0 | ||
| @distance_from_sun_km = distance | ||
|
|
||
| @fun_fact = fact | ||
| end | ||
|
|
||
| def summary | ||
| return "Planet's name is #{@name}, it's #{@color}. Fun fact about #{name}: #{fun_fact}" | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| require_relative 'planet' | ||
|
|
||
| class SolarSystem | ||
| attr_reader :star_name, :planets | ||
|
|
||
| def initialize(star_name) | ||
| raise ArgumentError.new("Input must be a string for star name") if !(star_name.instance_of? String) | ||
| @star_name = star_name | ||
| @planets = Array.new | ||
| end | ||
|
|
||
| def add_planet(planet) | ||
| raise ArgumentError.new("Input must be a Planet!") if !(planet.instance_of? Planet) | ||
| @planets << planet | ||
| end | ||
|
|
||
| def list_planets | ||
| planets_list = "Planets orbiting #{@star_name}\n" | ||
| @planets.length.times do |index| | ||
| planets_list << "#{index + 1}. #{planets[index].name.capitalize}\n" | ||
| end | ||
| return planets_list | ||
| end | ||
|
|
||
| def find_planet_by_name(planet_name) | ||
| raise ArgumentError.new("Planet name must be a string") if !(planet_name.instance_of? String) | ||
| planets_by_name = @planets.select {|obj| obj.name == planet_name.capitalize} | ||
| planet = (planets_by_name.empty?) ? nil : planets_by_name[0] | ||
| return planet | ||
| end | ||
|
|
||
| def distance_between(planet_name1, planet_name2) | ||
| raise ArgumentError.new("Arguments must be strings") if !(planet_name1.instance_of? String) || !(planet_name2.instance_of? String) | ||
| first_planet = find_planet_by_name(planet_name1) | ||
| second_planet = find_planet_by_name(planet_name2) | ||
|
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. Great work re-using the other instance methods! |
||
| raise ArgumentError.new("Planet doesn't exist in this Solar System") if first_planet == nil || second_planet == nil | ||
| return (first_planet.distance_from_sun_km - second_planet.distance_from_sun_km).to_i.abs | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| require_relative 'lib/solar_system' | ||
|
|
||
| def get_positive_number_from_user | ||
| input = Integer(gets.chomp) rescue false | ||
| while !input || input <= 0 | ||
| print "Invalid input. Input must be a positive number: " | ||
| input = Integer(gets.chomp) rescue false | ||
| end | ||
| return input | ||
| end | ||
|
|
||
| def get_string_input_from_user() | ||
|
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. Really thorough and thoughtful logic in here. It anticipates a lot of interesting things users can do. Well done |
||
| input = gets.chomp.strip.capitalize | ||
| while input.empty? | ||
| print "Input cannot be empty" | ||
| input = gets.chomp.strip.capitalize | ||
| end | ||
| return input | ||
| end | ||
|
|
||
| def print_decisions(decisions) | ||
| puts "Here are some options you can choose: " | ||
| decisions.length.times do |index| | ||
| puts "#{index + 1}. #{decisions[index].capitalize}" | ||
| end | ||
| end | ||
|
|
||
| def get_decision(decisions) | ||
| puts "\nWhat would you like to do next?" | ||
| print_decisions(decisions) | ||
|
|
||
| decision = get_string_input_from_user | ||
| while !(decisions.include? decision) | ||
| puts "Hey, that's not a valid decision" | ||
| print_decisions(decisions) | ||
| decision = get_string_input_from_user | ||
| end | ||
| return decision | ||
| end | ||
|
|
||
| def create_planet | ||
| print "Please enter in your planet's name: " | ||
| name = get_string_input_from_user | ||
|
|
||
| print "Please enter in your planet's color: " | ||
| color = get_string_input_from_user | ||
|
|
||
| print "Please enter in your planet's mass_kg: " | ||
| mass_kg = get_positive_number_from_user | ||
|
|
||
| print "Please enter in your planet's distance from the sun: " | ||
| distance = get_positive_number_from_user | ||
|
|
||
| print "Please enter in your planet's fun fact: " | ||
| fact = get_string_input_from_user | ||
|
|
||
| return Planet.new(name: name, color: color, mass_kg: mass_kg, distance: distance, fact: fact) | ||
| end | ||
|
|
||
| def get_2_planet_names_from_user(solar_system) | ||
|
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. I love how your code looks in this method! It looks so nice!! |
||
| puts "Please enter in name of 2 planets of which you want to calculate the distance between: " | ||
| user_planet_names = Array.new | ||
|
|
||
| 2.times do | ||
| print "Planet name: " | ||
| planet = get_string_input_from_user | ||
|
|
||
| while solar_system.find_planet_by_name(planet) == nil | ||
| puts "#{planet} doesn't exist in our Solar System. This is our Solar System:" | ||
| puts solar_system.list_planets | ||
| print "Planet name: " | ||
| planet = get_string_input_from_user | ||
| end | ||
|
|
||
| user_planet_names << planet | ||
| end | ||
| return user_planet_names | ||
| end | ||
|
|
||
| def main | ||
| solar_system = SolarSystem.new('Sol') | ||
| print "How many planets would you like to enter in: " | ||
| number_of_planets = get_positive_number_from_user | ||
|
|
||
| number_of_planets.times do |i| | ||
| planet = create_planet | ||
| solar_system.add_planet(planet) | ||
| end | ||
|
|
||
| available_decisions = ["Planet details", "Add planet", "List planets", "Calculate distance", "Exit"] | ||
| while true | ||
| user_decision = get_decision(available_decisions) | ||
|
|
||
| case user_decision | ||
| when "Calculate distance" | ||
| planet_names = get_2_planet_names_from_user(solar_system) | ||
| distance = solar_system.distance_between(planet_names[0],planet_names[1]) | ||
| puts "The distance between #{planet_names[0].capitalize} and #{planet_names[1].capitalize} is #{distance} km" | ||
|
|
||
| when "Planet details" | ||
| print "Please enter in name of a planet you want to look up in our Solar System: " | ||
| user_planet = get_string_input_from_user | ||
| planet = solar_system.find_planet_by_name(user_planet) | ||
|
|
||
| result_message = "" | ||
| if planet == nil | ||
| result_message << "Oops, #{user_planet} doesn't exist in our Solar System\n" | ||
| result_message << solar_system.list_planets | ||
| else | ||
| result_message << "This is the first result we found in our Solar System about #{user_planet}:\n #{planet.summary}" | ||
| end | ||
|
|
||
| puts result_message | ||
|
|
||
| when "Add planet" | ||
| planet = create_planet() | ||
| solar_system.add_planet(planet) | ||
| puts solar_system.list_planets | ||
|
|
||
| when "List planets" | ||
| puts solar_system.list_planets | ||
|
|
||
| else | ||
| exit | ||
| end | ||
| end | ||
| end | ||
| main | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| require 'minitest/autorun' | ||
| require 'minitest/reporters' | ||
| require 'minitest/skip_dsl' | ||
|
|
||
| require_relative '../lib/planet' | ||
|
|
||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| describe "Planet class" do | ||
|
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. Really really wonderful tests! They're really thorough and cover all of the important parts. Keep it up! |
||
| describe "Constructor" do | ||
| it "Error checks input being passed in for mass_kg" do | ||
|
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. Minor nitpick: It may be helpful to include in the test name that this error happens for negative mass_kg (same as the next distance test) |
||
| expect {Planet.new(name:'Earth', color: 'blue-green', mass_kg: -5.972e24, distance: 1.496e8, fact: 'Only planet known to support life')}.must_raise ArgumentError | ||
| end | ||
|
|
||
| it "Error checks input being passed in for distance" do | ||
| expect {Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: -1.496e8, fact: 'Only planet known to support life')}.must_raise ArgumentError | ||
| end | ||
|
|
||
| it "Initializes instance variables" do | ||
| earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life') | ||
| expect (earth.name).must_equal 'Earth' | ||
| expect (earth.fun_fact).must_equal 'Only planet known to support life' | ||
| expect (earth.color).must_equal 'blue-green' | ||
| expect (earth.mass_kg).must_equal 5.972e24 | ||
| expect (earth.distance_from_sun_km).must_equal 1.496e8 | ||
| end | ||
|
|
||
| it "Does not allow reassigning instance variables" do | ||
| earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life') | ||
| expect (earth.name).must_equal 'Earth' | ||
| expect (earth.fun_fact).must_equal 'Only planet known to support life' | ||
| expect (earth.color).must_equal 'blue-green' | ||
| expect (earth.mass_kg).must_equal 5.972e24 | ||
| expect (earth.distance_from_sun_km).must_equal 1.496e8 | ||
|
|
||
| expect {earth.color = 'pink'}.must_raise NoMethodError | ||
| end | ||
|
|
||
| it "Takes in arguments not in order" do | ||
| earth = Planet.new(color: 'blue-green', mass_kg: 5.972e24, fact: 'Only planet known to support life', name:'Earth', distance: 1.496e8) | ||
|
|
||
| expect (earth.name).must_equal 'Earth' | ||
| expect (earth.fun_fact).must_equal 'Only planet known to support life' | ||
| expect (earth.color).must_equal 'blue-green' | ||
| expect (earth.mass_kg).must_equal 5.972e24 | ||
| expect (earth.distance_from_sun_km).must_equal 1.496e8 | ||
| end | ||
| end | ||
|
|
||
| describe "summary method" do | ||
| it "Returns a string" do | ||
| earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life') | ||
| expect (earth.summary).must_be_instance_of String | ||
| end | ||
|
|
||
| it "Summarizes planet's information" do | ||
| earth = Planet.new(name:'Earth', color: 'blue-green', mass_kg: 5.972e24, distance: 1.496e8, fact: 'Only planet known to support life') | ||
| expect (earth.summary).must_equal "Planet's name is Earth, it's blue-green. Fun fact about Earth: Only planet known to support life" | ||
| end | ||
| 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.
Nice!
Here's a possible refactoring: using
findinstead ofselect. It will give back the elementthat matches first. If there is no match, then it will give back
nil