From 6057cb8e0363196843ecf168c472725f18b520c8 Mon Sep 17 00:00:00 2001 From: sairagula Date: Tue, 15 Aug 2017 23:37:44 -0700 Subject: [PATCH 1/4] Create Pipes - Sairagul - Solar_System --- Pipes - Sairagul - Solar_System | 130 ++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Pipes - Sairagul - Solar_System diff --git a/Pipes - Sairagul - Solar_System b/Pipes - Sairagul - Solar_System new file mode 100644 index 00000000..10eada28 --- /dev/null +++ b/Pipes - Sairagul - Solar_System @@ -0,0 +1,130 @@ + +# Creates class Planet + +class Planet + attr_reader :name + attr_reader :mass + attr_reader :diameter + attr_reader :year_length + attr_reader :distance_from_the_sun + + def initialize (name, mass, diameter, year_length, distance_from_the_sun) + @name = name + @mass = mass + @diameter = diameter + @year_length = year_length + @distance_from_the_sun = distance_from_the_sun + + end + +# Returns Planet's attributes in an easy to read format + def to_s + return "#{@name}: mass = #{@mass}, diameter = #{@diameter}, year_length = #{@year_length}, distance_from_the_sun = #{@distance_from_the_sun}" + end + + +# Calculates distance between given planet and other planets + def distance_from_other_planet(other_planet) + diff = @distance_from_the_sun - other_planet.distance_from_the_sun + if diff >= 0 + return diff + else + return -1 * diff + end + end + +end + +# Creates class Solarsystem +class Solarsystem + attr_accessor :planets + + def initialize(planet_list) + @planets = planet_list + end + + def add (planet) + @planets.push(planet) + end + +# This method iterates through @planets to find input planet. If finds, returns that planets' name. + def print_planet(planet_name) + found = false + + @planets.each do |pl| + if planet_name == pl.name + puts pl + found = true + break + end + end + + if !found + puts "Such planet doesn't exist." + end + end + +# List of all planets + def list_planets + planet_list = "" + i = 0 + count = @planets.length + while i < count + planet = @planets[i] + planet_list += "#{i+1}.#{planet}\n" # written in an easy to read format using "Planet" class' "to_s" method. + i += 1 + end + return planet_list + end + +end + + +# User input +def get_planet_from_user + puts "Please enter a planet: " + name = gets.chomp.to_s + puts "Enter its mass: " + mass = Float(gets.chomp) + puts "Diameter of the planet: " + diameter = Float(gets.chomp) + puts "Time that takes to go around its star: " + year_length = Float(gets.chomp) + puts "Distance from the sun: " + distance_from_the_sun = Float(gets.chomp) + return Planet.new(name, mass, diameter, year_length, distance_from_the_sun) +end + +# Creates user interface +def user_interface () + p1 = Planet.new("Saturn",11124124, 2226.464, 359.5, 11111111111) + p2 = Planet.new("Mars",112124124, 454546.464, 9.5, 122222222) + p3 = Planet.new("Jupiter",112124124, 1454.464, 736583.5, 333333333) + p4 = Planet.new("Earth",112124124, 116.464, 29.5, 44444444444) + p5 = Planet.new("Venus",55446124, 13236.464, 2323.5, 555555555555) + all_planets = [p1, p2, p3, p4, p5] + s1 = Solarsystem.new(all_planets) + + while true + puts "Enter one of the following options (1 - 3):" + puts "1. Create planet" + puts "2. Query planet" + puts "3. Exit" + command = gets.chomp.to_i + if command == 1 + new_planet = get_planet_from_user + s1.add(new_planet) + elsif command == 2 + puts "Enter a planet name to query:" + planet_name = gets.chomp.to_s + s1.print_planet(planet_name) + elsif command == 3 + puts "You want to exit." + break + else + puts "Invalid command." + end + end +end + +user_interface() From fc4851463b3791d80ea7e88d2d1b7e6f2529d63f Mon Sep 17 00:00:00 2001 From: sairagula Date: Wed, 16 Aug 2017 00:11:14 -0700 Subject: [PATCH 2/4] Create Pipes - Sairagul - Solar_System.rb --- Pipes - Sairagul - Solar_System.rb | 130 +++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Pipes - Sairagul - Solar_System.rb diff --git a/Pipes - Sairagul - Solar_System.rb b/Pipes - Sairagul - Solar_System.rb new file mode 100644 index 00000000..1c9dc908 --- /dev/null +++ b/Pipes - Sairagul - Solar_System.rb @@ -0,0 +1,130 @@ + +# Creates class Planet + +class Planet + attr_reader :name + attr_reader :mass + attr_reader :diameter + attr_reader :year_length + attr_reader :distance_from_the_sun + + def initialize (name, mass, diameter, year_length, distance_from_the_sun) + @name = name + @mass = mass + @diameter = diameter + @year_length = year_length + @distance_from_the_sun = distance_from_the_sun + + end + +# Returns Planet's attributes in an easy to read format + def to_s + return "#{@name}: mass = #{@mass}, diameter = #{@diameter}, year_length = #{@year_length}, distance_from_the_sun = #{@distance_from_the_sun}" + end + + +# Calculates distance between planets. + def distance_from_other_planet(other_planet) + diff = @distance_from_the_sun - other_planet.distance_from_the_sun + if diff >= 0 + return diff + else + return -1 * diff + end + end + +end # End of class Planet + +# Creates class Solarsystem +class Solarsystem + attr_accessor :planets + + def initialize(planet_list) + @planets = planet_list + end + + def add (planet) + @planets.push(planet) + end + +# This method iterates through @planets to find input planet. If finds, returns that planets' name. + def print_planet(planet_name) + found = false + + @planets.each do |pl| + if planet_name == pl.name + puts pl + found = true + break + end + end + + if !found + puts "Such planet doesn't exist." + end + end + +# List of all planets + def list_planets + planet_list = "" + i = 0 + count = @planets.length + while i < count + planet = @planets[i] + planet_list += "#{i+1}.#{planet}\n" # written in an easy to read format using "Planet" class' "to_s" method. + i += 1 + end + return planet_list + end + +end # end of class Solarsystem + + +# User input +def get_planet_from_user + puts "Please enter a planet: " + name = gets.chomp.to_s + puts "Enter its mass: " + mass = Float(gets.chomp) + puts "Diameter of the planet: " + diameter = Float(gets.chomp) + puts "Time that takes to go around its star: " + year_length = Float(gets.chomp) + puts "Distance from the sun: " + distance_from_the_sun = Float(gets.chomp) + return Planet.new(name, mass, diameter, year_length, distance_from_the_sun) +end + +# Creates user interface +def user_interface () + p1 = Planet.new("Saturn",11124124, 2226.464, 359.5, 11111111111) + p2 = Planet.new("Mars",112124124, 454546.464, 9.5, 122222222) + p3 = Planet.new("Jupiter",112124124, 1454.464, 736583.5, 333333333) + p4 = Planet.new("Earth",112124124, 116.464, 29.5, 44444444444) + p5 = Planet.new("Venus",55446124, 13236.464, 2323.5, 555555555555) + all_planets = [p1, p2, p3, p4, p5] + s1 = Solarsystem.new(all_planets) + + while true + puts "Enter one of the following options (1 - 3):" + puts "1. Create planet" + puts "2. Query planet" + puts "3. Exit" + command = gets.chomp.to_i + if command == 1 + new_planet = get_planet_from_user + s1.add(new_planet) + elsif command == 2 + puts "Enter a planet name to query:" + planet_name = gets.chomp.to_s + s1.print_planet(planet_name) + elsif command == 3 + puts "You want to exit." + break + else + puts "Invalid command." + end + end +end + +user_interface() From 8218907adf3af337b30266af3667fd036cb25f5d Mon Sep 17 00:00:00 2001 From: sairagula Date: Wed, 16 Aug 2017 00:15:09 -0700 Subject: [PATCH 3/4] Delete Pipes - Sairagul - Solar_System --- Pipes - Sairagul - Solar_System | 130 -------------------------------- 1 file changed, 130 deletions(-) delete mode 100644 Pipes - Sairagul - Solar_System diff --git a/Pipes - Sairagul - Solar_System b/Pipes - Sairagul - Solar_System deleted file mode 100644 index 10eada28..00000000 --- a/Pipes - Sairagul - Solar_System +++ /dev/null @@ -1,130 +0,0 @@ - -# Creates class Planet - -class Planet - attr_reader :name - attr_reader :mass - attr_reader :diameter - attr_reader :year_length - attr_reader :distance_from_the_sun - - def initialize (name, mass, diameter, year_length, distance_from_the_sun) - @name = name - @mass = mass - @diameter = diameter - @year_length = year_length - @distance_from_the_sun = distance_from_the_sun - - end - -# Returns Planet's attributes in an easy to read format - def to_s - return "#{@name}: mass = #{@mass}, diameter = #{@diameter}, year_length = #{@year_length}, distance_from_the_sun = #{@distance_from_the_sun}" - end - - -# Calculates distance between given planet and other planets - def distance_from_other_planet(other_planet) - diff = @distance_from_the_sun - other_planet.distance_from_the_sun - if diff >= 0 - return diff - else - return -1 * diff - end - end - -end - -# Creates class Solarsystem -class Solarsystem - attr_accessor :planets - - def initialize(planet_list) - @planets = planet_list - end - - def add (planet) - @planets.push(planet) - end - -# This method iterates through @planets to find input planet. If finds, returns that planets' name. - def print_planet(planet_name) - found = false - - @planets.each do |pl| - if planet_name == pl.name - puts pl - found = true - break - end - end - - if !found - puts "Such planet doesn't exist." - end - end - -# List of all planets - def list_planets - planet_list = "" - i = 0 - count = @planets.length - while i < count - planet = @planets[i] - planet_list += "#{i+1}.#{planet}\n" # written in an easy to read format using "Planet" class' "to_s" method. - i += 1 - end - return planet_list - end - -end - - -# User input -def get_planet_from_user - puts "Please enter a planet: " - name = gets.chomp.to_s - puts "Enter its mass: " - mass = Float(gets.chomp) - puts "Diameter of the planet: " - diameter = Float(gets.chomp) - puts "Time that takes to go around its star: " - year_length = Float(gets.chomp) - puts "Distance from the sun: " - distance_from_the_sun = Float(gets.chomp) - return Planet.new(name, mass, diameter, year_length, distance_from_the_sun) -end - -# Creates user interface -def user_interface () - p1 = Planet.new("Saturn",11124124, 2226.464, 359.5, 11111111111) - p2 = Planet.new("Mars",112124124, 454546.464, 9.5, 122222222) - p3 = Planet.new("Jupiter",112124124, 1454.464, 736583.5, 333333333) - p4 = Planet.new("Earth",112124124, 116.464, 29.5, 44444444444) - p5 = Planet.new("Venus",55446124, 13236.464, 2323.5, 555555555555) - all_planets = [p1, p2, p3, p4, p5] - s1 = Solarsystem.new(all_planets) - - while true - puts "Enter one of the following options (1 - 3):" - puts "1. Create planet" - puts "2. Query planet" - puts "3. Exit" - command = gets.chomp.to_i - if command == 1 - new_planet = get_planet_from_user - s1.add(new_planet) - elsif command == 2 - puts "Enter a planet name to query:" - planet_name = gets.chomp.to_s - s1.print_planet(planet_name) - elsif command == 3 - puts "You want to exit." - break - else - puts "Invalid command." - end - end -end - -user_interface() From a0de67f3bd53f4196971c8f805aef6ae384d5633 Mon Sep 17 00:00:00 2001 From: sairagula Date: Wed, 16 Aug 2017 16:15:04 -0700 Subject: [PATCH 4/4] Update Pipes - Sairagul - Solar_System.rb --- Pipes - Sairagul - Solar_System.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Pipes - Sairagul - Solar_System.rb b/Pipes - Sairagul - Solar_System.rb index 1c9dc908..fbe7aa08 100644 --- a/Pipes - Sairagul - Solar_System.rb +++ b/Pipes - Sairagul - Solar_System.rb @@ -23,15 +23,16 @@ def to_s end -# Calculates distance between planets. - def distance_from_other_planet(other_planet) - diff = @distance_from_the_sun - other_planet.distance_from_the_sun - if diff >= 0 - return diff - else - return -1 * diff - end - end +# I couldn't figure out how to call this function, where to call. It should work but, I can't test it. +# # Calculates distance between planets. +# def distance_from_other_planet(other_planet) +# diff = @distance_from_the_sun - other_planet.distance_from_the_sun +# if diff >= 0 +# return diff +# else +# return -1 * diff +# end +# end end # End of class Planet