forked from AdaGold/solar-system
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Ports - Ngoc #32
Open
lebaongoc
wants to merge
9
commits into
Ada-C11:master
Choose a base branch
from
lebaongoc: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 - Ngoc #32
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ab54b6
Created planet.rb, solar_system.rb, and main.rb to display name of pl…
lebaongoc 493f51f
Revert "Created planet.rb, solar_system.rb, and main.rb to display na…
lebaongoc 2b94d51
Created planet.rb, solar_system.rb, and main.rb to output a listing …
lebaongoc 741a105
Added test to check if a new instance is an instance of the class Planet
lebaongoc d75510b
updated main.rb and solar_system.rb to return the summary of an insta…
lebaongoc c4fc0d7
Create loop to prompt user inputs 'list planets or exit'
lebaongoc ca70ac2
Added plant details option -part 2 of wave 3 and add planet option - …
lebaongoc a33b964
minor format changes
lebaongoc 153c93f
Update planet.rb
lebaongoc 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 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,51 @@ | ||
require_relative "planet.rb" | ||
require_relative "solar_system.rb" | ||
|
||
def main | ||
|
||
#Added planets' info to each planet instance | ||
solar_system = SolarSystem.new("Sun") | ||
|
||
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") | ||
solar_system.add_planet(earth) | ||
|
||
mercury = Planet.new("Mercury", "dark grey", 4.867e24, 5.791e7, "Mercury is the smallest plannet in our solar system") | ||
solar_system.add_planet(mercury) | ||
|
||
neptune = Planet.new("Neptune", "purple", 1.024e26, 4.495e9, "It is four times wider than Earth") | ||
solar_system.add_planet(neptune) | ||
|
||
saturn = Planet.new("Saturn", "yellowish-brown", 5.683e26, 1.434e9, "Saturn is the second largest planet in our solar system") | ||
solar_system.add_planet(saturn) | ||
|
||
venus = Planet.new("Venus", "opal", 4.867e24, 1.082e8, "It has intense heat and volcanic activity") | ||
solar_system.add_planet(venus) | ||
|
||
######## Prompt User's inputs######## | ||
|
||
puts "Welcome to Solar System program!" | ||
puts "What would you like to do?" | ||
puts "[list planets] [planet details] [add planet] [exit]" | ||
user_input = gets.chomp | ||
|
||
until user_input.downcase == "exit" | ||
case user_input.downcase | ||
when "list planets" | ||
puts solar_system.list_planets | ||
when "planet details" | ||
puts "Which planet would you like to learn about?" | ||
planet_details_input = gets.chomp | ||
found_planet = solar_system.find_planet_by_name(planet_details_input.downcase) | ||
puts found_planet.summary | ||
when "add planet" | ||
solar_system.user_added_planet | ||
puts solar_system.list_planets | ||
end | ||
|
||
puts "What would you like to do next?" | ||
puts "[list planets] [planet details] [add planet] [exit]" | ||
user_input = gets.chomp | ||
end | ||
end | ||
|
||
main |
This file contains 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,15 @@ | ||
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 | ||
|
||
def summary | ||
return "Planet #{name} has the color of #{color} with the mass of #{mass_kg} kgs. This planet is #{distance_from_sun_km} km from the sun. A fun-fact about this planet is: #{fun_fact}" | ||
end | ||
end |
This file contains 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,47 @@ | ||
|
||
class SolarSystem | ||
attr_reader :star_name, :planet | ||
|
||
def initialize(star_name) | ||
@star_name = star_name | ||
@planets = [] | ||
end | ||
|
||
def add_planet(planet_instance) | ||
@planets << planet_instance | ||
end | ||
|
||
def list_planets | ||
conclusion_statement = "Planets orbiting #{star_name}:\n" | ||
|
||
i = 1 | ||
planet_list = "" | ||
@planets.each do |planet| | ||
planet_list += "#{i}.#{planet.name} \n" | ||
i += 1 | ||
end | ||
|
||
return conclusion_statement + planet_list | ||
end | ||
|
||
def find_planet_by_name(planet_name) | ||
@planets.find { |planet| planet.name.downcase == planet_name.downcase } | ||
end | ||
|
||
def user_added_planet | ||
puts "Please enter new planet's name" | ||
planet_name = gets.chomp | ||
puts "Please enter new planet's color" | ||
planet_color = gets.chomp | ||
puts "Please enter new planet's mass in kilograms" | ||
planet_mass = gets.chomp.to_i | ||
puts "Please enter the distance between this new planet and the sun" | ||
planet_distance = gets.chomp.to_i | ||
puts "Please enter new planet's fun_fact" | ||
fun_fact = gets.chomp | ||
|
||
user_planet = Planet.new(planet_name.capitalize, planet_color, planet_mass, planet_distance, fun_fact) | ||
|
||
add_planet(user_planet) | ||
end | ||
end |
This file contains 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,11 @@ | ||
require "minitest/autorun" | ||
require "minitest/reporters" | ||
require_relative "../lib/planet.rb" | ||
Minitest::Reporters.use! | ||
|
||
describe "planet" do | ||
it "Check if a new instance is an instance of class Planet " do | ||
earth = Planet.new("Earth", "blue-green", 5.972e24, 1.496e8, "Only planet known to support life") | ||
expect(earth).must_be_instance_of Planet | ||
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.
I would suggest moving this method to the
main.rb
file so to separate the user interface role from the role of storing and organizing the planets.