forked from AdaGold/solar-system
-
Notifications
You must be signed in to change notification settings - Fork 48
Ports - Sopheary #34
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
Open
sophearychiv
wants to merge
10
commits into
Ada-C11:master
Choose a base branch
from
sophearychiv:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ports - Sopheary #34
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9edb33
finished wave 1
f1165eb
finished wave 2
d0b917b
finished wave 3
84f9750
finished wave 3
c8a0949
did optional enhancements
51d52a6
colorized the outputs
718e98f
refactored
5d7423e
refactored
52befe1
added Rakefile
4563bb5
refactored: added helper methods in main file
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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['specs/*_spec.rb'] | ||
| end | ||
|
|
||
| task default: :test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| require_relative "planet" | ||
| require_relative "solar_system" | ||
|
|
||
| require "awesome_print" | ||
| require "colorize" | ||
|
|
||
| SOLAR_SYSTEM = SolarSystem.new("Sol") | ||
|
|
||
| # Optional: handling user entering bad command | ||
| def handling_input(input) | ||
| possible_inputs = ["list planets", "planet details", "add planet", "find distance", "exit"] | ||
| until possible_inputs.any? { |word| word == input } | ||
| print "Invalid input.\nPlease enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " | ||
| input = gets.chomp.downcase | ||
| end | ||
| return input | ||
| end | ||
|
|
||
| def existing_planets | ||
| earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "it's the only planet known to support life") | ||
| SOLAR_SYSTEM.add_planet(earth) | ||
|
|
||
| venus = Planet.new("Venus", "yellow", 4.867e24, 67.3e6, "40 spacecraft have explored Venus") | ||
| SOLAR_SYSTEM.add_planet(venus) | ||
|
|
||
| mercury = Planet.new("Mercury", "blue", 3.285e23, 28.6e6, "kid-friendly") | ||
| SOLAR_SYSTEM.add_planet(mercury) | ||
|
|
||
| mars = Planet.new("Mars", "orange", 6.39e23, 141.7e6, "it has 2 moons") | ||
| SOLAR_SYSTEM.add_planet(mars) | ||
| end | ||
|
|
||
| def list_planets(input) | ||
| if input == "list planets" | ||
| list = SOLAR_SYSTEM.list_planets | ||
| puts list.colorize(:green) | ||
| end | ||
| end | ||
|
|
||
| def planet_details(input) | ||
| if input == "planet details" | ||
| puts "Which planet do you want to learn about? Choose one: Earth, Venus, Mercury, Mars" | ||
| planet_chosen = gets.chomp.downcase | ||
| # Optional: when user enters a planet that doesn't exist, raise an exception. | ||
| if !SOLAR_SYSTEM.planets.map { |planet| planet.name }.include?(planet_chosen.capitalize) | ||
| raise ArgumentError, "Planet entered is not in the list." | ||
| end | ||
| puts "\n" | ||
| puts SOLAR_SYSTEM.find_planet_by_name(planet_chosen).summary.colorize(:green) | ||
| end | ||
| end | ||
|
|
||
| def add_planet(input) | ||
| if input == "add planet" | ||
| puts "What is the planet's name?" | ||
| name = gets.chomp.capitalize | ||
| puts "What is its color?" | ||
| color = gets.chomp | ||
| puts "What is its mass(kg)?" | ||
| mass_kg = gets.chomp.to_f | ||
| # Optional: if user enters a bad value | ||
| until mass_kg > 0 | ||
| puts "Mass must be a number greater than 0. Please enter again: " | ||
| mass_kg = gets.chomp.to_f | ||
| end | ||
| puts "What is its distance from the Sun in km?" | ||
| distance_from_sun_km = gets.chomp.to_f | ||
| # Optional: if the user enters a bad value | ||
| until distance_from_sun_km > 0 | ||
| puts "Distance from the Sun must be a number greater than 0. Please enter again: " | ||
| distance_from_sun_km = gets.chomp.to_f | ||
| end | ||
| puts "What is its fun fact?" | ||
| fun_fact = gets.chomp | ||
|
|
||
| planet = Planet.new(name, color, mass_kg, distance_from_sun_km, fun_fact) | ||
| SOLAR_SYSTEM.add_planet(planet) | ||
| puts "#{name} has been added with the details below:".colorize(:green) | ||
| puts SOLAR_SYSTEM.find_planet_by_name(name).summary.colorize(:green) | ||
| end | ||
| end | ||
|
|
||
| def find_distance(input) | ||
| if input == "find distance" | ||
| puts "Enter first planet: " | ||
| planet_name1 = gets.chomp.downcase | ||
| puts "Enter second planet: " | ||
| planet_name2 = gets.chomp.downcase | ||
| puts "The distance is #{SOLAR_SYSTEM.distance_between(planet_name1, planet_name2)}".colorize(:green) | ||
| end | ||
| end | ||
|
|
||
| def main | ||
| existing_planets | ||
| puts "What would you like to do?" | ||
| print "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " | ||
| input = gets.chomp.downcase | ||
| input = handling_input(input) | ||
|
|
||
| until input == "exit" | ||
| list_planets(input) | ||
| planet_details(input) | ||
| add_planet(input) | ||
| find_distance(input) # OPTIONAL: Implement the optional distance_between method | ||
| puts "What would you like to do next?" | ||
| puts "Please enter \"list planets\", \"planet details\", \"add planet\", \"find distance\" or \"exit\": " | ||
| input = gets.chomp | ||
| input = handling_input(input) | ||
| end | ||
| end | ||
|
|
||
| main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| 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 | ||
|
|
||
| # gets the details of the planet | ||
| def summary | ||
| return "#{name} is a/an #{color} planet. Its mass is #{mass_kg}kg, and it is #{distance_from_sun_km}km from the Sun. Fun fact: #{fun_fact}." | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| require_relative "planet" | ||
|
|
||
| class SolarSystem | ||
| attr_reader :star_name, :planets #should keep? if so, need to add to spec | ||
|
|
||
| def initialize(star_name) | ||
| @star_name = star_name | ||
| @planets = [] | ||
| end | ||
|
|
||
| def add_planet(planet) | ||
| raise ArgumentError, "Planet has to be an instance of Planet class" if !planet.is_a?(Planet) | ||
| raise ArgumentError, "Planet has already been added." if @planets.any? { |i| i.name.downcase == planet.name.downcase } | ||
| @planets.push(planet) | ||
| end | ||
|
|
||
| def list_planets | ||
| list = "\nPlanets orbitting #{@star_name}:\n" | ||
| @planets.each_with_index do |planet, index| | ||
| list = list + "#{index + 1}. #{planet.name}\n" | ||
| end | ||
| return list | ||
| end | ||
|
|
||
| def find_planet_by_name(name) | ||
| @planets.each do |planet| | ||
| return planet if (planet.name).downcase == name.downcase | ||
| end | ||
| # if the planet is not found, raise an exception | ||
| raise ArgumentError, "#{name} is not found." | ||
| end | ||
|
|
||
| # Optional: takes two planet names as parameters and returns the distance between them | ||
| def distance_between(planet_name1, planet_name2) | ||
| distance_planet1 = find_planet_by_name(planet_name1).distance_from_sun_km | ||
| distance_planet2 = find_planet_by_name(planet_name2).distance_from_sun_km | ||
| return (distance_planet1 - distance_planet2).abs | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
|
|
||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
| require "minitest/skip_dsl" | ||
|
|
||
| require_relative "../lib/planet" | ||
|
|
||
| # Get that nice colorized output | ||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| describe "planet" do | ||
| describe "initialize" do | ||
| it "will create an instance of the class Planet with 5 parameters" do | ||
| new_planet = Planet.new("earth", "blue", 45, 67, "supports life") | ||
| expect(new_planet).must_be_instance_of Planet | ||
| end | ||
|
|
||
| it "will correctly read the parameters of the planet" do | ||
| new_planet = Planet.new("earth", "blue", 45, 67, "supports life") | ||
| expect(new_planet.name).must_equal "earth" | ||
| expect(new_planet.color).must_equal "blue" | ||
| expect(new_planet.mass_kg).must_equal 45 | ||
| expect(new_planet.distance_from_sun_km).must_equal 67 | ||
| expect(new_planet.fun_fact).must_equal "supports life" | ||
| end | ||
|
|
||
| describe "summary" do | ||
| it "will return a summary of the planet" do | ||
| new_planet = Planet.new("earth", "blue", 45, 67, "supports life") | ||
| expect(new_planet.summary).must_be_instance_of String | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| require "minitest/autorun" | ||
| require "minitest/reporters" | ||
|
|
||
| require_relative "../lib/solar_system" | ||
|
|
||
| # Get that nice colorized output | ||
| Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new | ||
|
|
||
| describe "solar_system" do | ||
| describe "initialize method" do | ||
| new_sol = SolarSystem.new("New Sol") | ||
| it "will create an instance of the class SolarSystem with an empty array planets " do | ||
| expect(new_sol).must_be_instance_of SolarSystem | ||
| expect(new_sol.planets).must_equal [] | ||
| end | ||
|
|
||
| it "will read the star name when initialized" do | ||
| expect(new_sol.star_name).must_equal "New Sol" | ||
| end | ||
| end | ||
|
|
||
| describe "add_planet method" do | ||
| new_sol = SolarSystem.new("New Sol") | ||
| it "will take a planet and return an array of planets" do | ||
| pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") | ||
| expect(new_sol.add_planet(pluto)).must_be_instance_of Array | ||
| end | ||
|
|
||
| it "will take the parameter as a planet" do | ||
| pluto = "Pluto" | ||
| # note to me: when using curly braces, the code inside runs before expect | ||
| # when using with .must_raise | ||
| expect { new_sol.add_planet(pluto) }.must_raise ArgumentError | ||
| end | ||
| end | ||
|
|
||
| describe "list_planets method" do | ||
| new_sol = SolarSystem.new("New Sol") | ||
| pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") | ||
| new_sol.add_planet(pluto) | ||
| it "will return true if the list is an instance of a string" do | ||
| expect(new_sol.list_planets).must_be_instance_of String | ||
| end | ||
| end | ||
|
|
||
| describe "find_planet_by_name method" do | ||
| new_sol = SolarSystem.new("New Sol") | ||
| pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") | ||
| new_sol.add_planet(pluto) | ||
| it "will return an instance of Planet" do | ||
| expect(new_sol.find_planet_by_name("Pluto")).must_be_instance_of Planet | ||
| end | ||
| end | ||
|
|
||
| describe "distance_between method" do | ||
| it "will return the distance between two planets" do | ||
| new_sol = SolarSystem.new("New_Sol") | ||
| pluto = Planet.new("Pluto", "pink", 56, 87, "pet-friendly") | ||
| puffer = Planet.new("Puffer", "yellow", 78, 89, "not a planet") | ||
| new_sol.add_planet(pluto) | ||
| new_sol.add_planet(puffer) | ||
| expect(new_sol.distance_between(pluto.name, puffer.name)).must_equal 2 | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You could also use
.findto search for the planet.