Skip to content
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

Sockets - Grace #40

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a08d1d2
created planet.rb file with planet class, reader attribute, and initi…
gracemshea Feb 26, 2019
3fe3937
added main.rb file with empty main method and summary method to plane…
gracemshea Feb 26, 2019
800a9af
added argument errors to planet.rb for mass_kg and distance_from_sun_km
gracemshea Feb 26, 2019
b7e2e8b
added country music planets to my main.rb
gracemshea Feb 26, 2019
eb24312
created solar_system.rb with class and ininitialize method
gracemshea Feb 26, 2019
bd10d6f
added solar_system.new to main.rb, added current planets.
gracemshea Feb 26, 2019
55c2de0
attr_reader and add_planet method updated on solar_system.rb
gracemshea Feb 26, 2019
2d129b4
get_info_new_planet method written...this should help.
gracemshea Feb 26, 2019
c54eb55
added list_planets to solar_system.rb
gracemshea Feb 26, 2019
90ffcd9
working on list of options for the user. planning to call each method…
gracemshea Feb 26, 2019
d35a93e
find planet method, not sure it works yet
gracemshea Feb 26, 2019
c82f4dd
working on loop do and solar system methods. added get_planet_name an…
gracemshea Feb 26, 2019
139928d
added way to reject invalid input to my menu - thanks, google! finish…
gracemshea Feb 26, 2019
8b56841
tidying up, removed require pry on files
gracemshea Feb 26, 2019
4fb796c
tidying
gracemshea Feb 26, 2019
2937959
fixed Planet.new syntax
gracemshea Feb 27, 2019
4d47d7e
gave loop greeting
gracemshea Feb 27, 2019
640474d
making tweaks, searching for source of error within spelling or syntax
gracemshea Feb 27, 2019
c4aa48f
fixed reba planet to have enough arguments
gracemshea Feb 27, 2019
a106899
added line 33 on main to set local variable input
gracemshea Feb 27, 2019
c214304
fixed error in retrieving planet info
gracemshea Mar 12, 2019
a96b0f7
fixed error in creating new planet
gracemshea Mar 12, 2019
aa35e37
debugging the distance calculator method, stopping point
gracemshea Mar 12, 2019
aa4ca3f
saving files helps
gracemshea Mar 12, 2019
f625e3b
fixed the return raise argument syntax errors
gracemshea Mar 12, 2019
c884b3c
removed awkward variable
gracemshea Mar 12, 2019
ef5a972
fixed distance_between error
gracemshea Mar 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative "planet"
require_relative "solar_system"

def main
solar_system = SolarSystem.new("Sol")

elvis = Planet.new("Elvis", "shiny", 159, 20, "recorded more than 600 songs, but did not write any of them", 3.5)
hank_sr = Planet.new("Hank Sr", "white as hell", 72, 30, "recorded 14 songs as his alter ego, Luke the Drifter", 4.5)
dolly = Planet.new("Dolly", "canary yellow", 52, 18, "once entered a Dolly look-alike drag queen contest and lost", 4.8)
patsy = Planet.new("Patsy", "chesnut", 70, 40, "enjoys walking after midnight", 3.4)
garth = Planet.new("Garth", "off-white", 83, 45, "is responsible for the the most insufferable variety of country boi", 4.6)
johnny = Planet.new("Johnny", "coal black", 86, 5, "was a campaigner for Native American rights", 3.6)
reba = Planet.new("Reba", "red", 60, 10, "is the only country female solo act to have a No. 1 hit in four straight decades", 5.0)
willie = Planet.new("Willie", "old", 70, 7, "ran into a burning house to save his pound of Columbian grass", 3.8)

solar_system.add_planet(elvis)
solar_system.add_planet(hank_sr)
solar_system.add_planet(dolly)
solar_system.add_planet(patsy)
solar_system.add_planet(garth)
solar_system.add_planet(johnny)
solar_system.add_planet(reba)

puts "Country Music Planetary Menu\n"
puts "1. List Planets\n"
puts "2. Planet Information\n"
puts "3. Add a New Planet\n"
puts "4. Distance Calculator\n"
puts "5. Exit\n"

loop do
"Please enter your selection from the above options."
input = gets.chomp.to_s
case
when input == "1"
puts solar_system.list_planets
when input == "2"
planet = solar_system.get_planet_name
puts planet.summary
when input == "3"
solar_system.get_info_new_planet
puts "\n...Entered into system. Thank You."
when input == "4"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option 3 does not work - the method on SolarSystem is get_info_new_planet, not add_a_planet.

solar_system.find_distance_between
when input == "5"
exit
when input =~ /[[:alpha:]]/
puts "\nYour response was invalid"
end
end
end

main
25 changes: 25 additions & 0 deletions planet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


class Planet
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact, :twang_level, :likelihood_to_be_loved_by_yanks

def initialize(name, color, mass, distance_from_sun_km, fun_fact, twang_level)
@name = name.capitalize
@color = color
@mass_kg = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
@twang_level = twang_level
if mass <= 0
raise ArgumentError, "Planet mass must be larger than 0"
end
if distance_from_sun_km <= 0
raise ArgumentError, "Planet distance must be larger than 0"
end
end

def summary
puts "The planet #{@name} is the color #{@color} has a mass of #{@mass} kg, and is #{@distance_from_sun_km} km from the sun.
It #{@fun_fact}, and is know to have a twang level of #{@twang_level} out of 5."
end
end
74 changes: 74 additions & 0 deletions solar_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@


class SolarSystem
attr_reader :solar_system_name, :planets

def initialize(solar_system_name)
@solar_system_name = solar_system_name
@planets = []
end

def add_planet(new_planet)
@planets << new_planet
end

def get_info_new_planet
print "\nWhat is your new Planet's name?"
planet_name = gets.chomp.to_s
print "\nWhat is #{planet_name}'s color?"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a lot of code that interacts with the user here, which makes me think this method might fit better in main.rb.

color = gets.chomp.to_s
print "\nWhat is the distance of planet from #{solar_system_name}(km): "
distance = gets.chomp.to_f
print "\nMass of #{planet_name}(kg): "
mass = gets.chomp.to_f
print "\nInteresting fact about #{planet_name}: "
fun_fact = gets.chomp.to_s
print "\nPlease rate the planet's twang on a scale of 0-5.0 #{planet_name}: "
twang_level = gets.chomp.to_f

new_planet = Planet.new(planet_name, color, distance, mass, fun_fact, twang_level)

add_planet(new_planet)
print "\nGenerating new data"
end

def list_planets
list = "\nPlanets in the Country Music Universe:\n"
@planets.each_with_index do |planet, index|
index += 1
list += "#{index}. #{planet.name}\n"
end
return list
end

def find_planet_by_name(planet_name)
@planets.each do |planet|
if planet.name == planet_name.capitalize
return planet

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code has a bug! Your parameter (which should be the name of a planet) is called planet, but you override this on line 47 by using the same name for your iteration parameter. The on line 48, you look for the variable name, which doesn't exist.

Probably the parameter should be called name, not planet. Making this change seems to fix the error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dan, thank you for your feedback! I will take a look at this over the weekend and refactor.

end
end
raise ArgumentError, "Planet does not exist in the Country Music Hall of Planets"
end

def get_planet_name
print "\nEnter planet name: "
planet = gets.chomp.to_s
return find_planet_by_name(planet)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method also might fit better in main.rb

end

def get_planet_distance(name)
@planets.each do |planet|
if planet.name == name.capitalize
return planet.distance
end
end
raise ArgumentError, "Planet does not exist in the Country Music Hall of Planets"
end

def find_distance_between
planet1 = get_planet_name
planet2 = get_planet_name
distance = (planet1.distance_from_sun_km) - (planet2.distance_from_sun_km)
puts "\nDistance between #{planet1.name} and #{planet2.name}: #{distance.abs} km."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should return a number here instead of putsing the result. Then whoever called this method could use that number. For example, if someone wanted to write a method to find the two planets that are closest together, a number would be much more valuable than this string.

end
end