Last active
August 29, 2015 14:16
-
-
Save Andsbf/c95b9051d27b6d008716 to your computer and use it in GitHub Desktop.
Pop Bottle Recycler
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
require 'pry' | |
class Exchange | |
def initialize x_client | |
bottles_earned = {by_empty: 0, by_tips: 0} | |
bottles_earned[:by_empty] += x_client.bottles_empty/2 | |
x_client.bottles_empty = x_client.bottles_empty%2 | |
bottles_earned[:by_tips] += x_client.bottles_tips/4 | |
x_client.bottles_tips = x_client.bottles_tips%4 | |
x_client.free_bottles[:by_empty] += bottles_earned[:by_empty] | |
x_client.free_bottles[:by_tips] += bottles_earned[:by_tips] | |
x_client.full_bottles = bottles_earned[:by_empty] + bottles_earned[:by_tips] | |
end | |
end | |
class Person | |
attr_accessor :investiment, :full_bottles, :bottles_empty, :bottles_tips, :free_bottles | |
def initialize investiment=0, bottles_empty=0 ,bottles_tips=0 | |
@investiment = investiment | |
@full_bottles = investiment/2 | |
@bottles_empty = bottles_empty | |
@bottles_tips = bottles_tips | |
@free_bottles = {by_empty: 0, by_tips: 0} | |
end | |
def drink_all | |
@bottles_empty += full_bottles | |
@bottles_tips += full_bottles | |
@full_bottles = 0 | |
end | |
end | |
class Promotion_cycle | |
attr_accessor :client | |
def initialize client | |
client.drink_all if client.full_bottles != 0 | |
while client.bottles_empty > 1 || client.bottles_tips > 4 | |
Exchange.new(client) | |
client.drink_all | |
end | |
end | |
end | |
user_input = nil | |
while user_input != "quit" | |
puts "How much you will spend on pops?(type quit to exit)" | |
user_input = gets.chomp.downcase | |
user = Person.new(user_input.to_i) | |
Promotion_cycle.new(user) | |
simulation_1 = Person.new(user.investiment + 1 ) | |
Promotion_cycle.new(simulation_1) | |
simulation_2 = Person.new(user.investiment + 2 ) | |
Promotion_cycle.new(simulation_2) | |
puts "Dear client,\nWith $#{user.investiment} you can get up to #{user.free_bottles.values.inject(:+)} free bootles taking full advantage of our promotion!" | |
case user.investiment | |
when 0 | |
puts "There ain't no such thing as a free lunch!!" | |
else | |
if user.investiment.odd? | |
puts "But with just $1 more you can get #{simulation_1.free_bottles.values.inject(:+)} free bottles" | |
else | |
puts "But with just $2 more you can get #{simulation_2.free_bottles.values.inject(:+)} free bottles" | |
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
#v0.2 - using recursion | |
require 'pry' | |
class Exchange | |
def initialize x_client | |
x_client.drink_all | |
return if (x_client.bottles_empty < 2 && x_client.bottles_tips < 4) | |
#calculates earns | |
bottles_earned = {by_empty: 0, by_tips: 0} | |
bottles_earned[:by_empty] += x_client.bottles_empty/2 | |
bottles_earned[:by_tips] += x_client.bottles_tips/4 | |
#update client's inventory | |
x_client.bottles_empty = x_client.bottles_empty%2 | |
x_client.bottles_tips = x_client.bottles_tips%4 | |
x_client.free_bottles[:by_empty] += bottles_earned[:by_empty] | |
x_client.free_bottles[:by_tips] += bottles_earned[:by_tips] | |
x_client.full_bottles = bottles_earned[:by_empty] + bottles_earned[:by_tips] | |
Exchange.new(x_client) | |
end | |
end | |
class Person | |
attr_accessor :investiment, :full_bottles, :bottles_empty, :bottles_tips, :free_bottles | |
def initialize investiment=0, bottles_empty=0 ,bottles_tips=0 | |
@investiment = investiment | |
@full_bottles = investiment/2 | |
@bottles_empty = bottles_empty | |
@bottles_tips = bottles_tips | |
@free_bottles = {by_empty: 0, by_tips: 0} | |
end | |
def drink_all | |
@bottles_empty += full_bottles | |
@bottles_tips += full_bottles | |
@full_bottles = 0 | |
end | |
end | |
user_input = nil | |
while user_input != "quit" | |
puts "\nHow much dollars you will spend on pops?(type quit to exit)" | |
user_input = gets.chomp.downcase | |
user_input = nil if user_input == "" | |
user = Person.new(user_input.to_i) | |
Exchange.new(user) | |
simulation_1 = Person.new(user.investiment + 1 ) | |
Exchange.new(simulation_1) | |
simulation_2 = Person.new(user.investiment + 2 ) | |
Exchange.new(simulation_2) | |
unless user_input.nil? | |
puts "Dear client,\nWith $#{user.investiment} you can get up to #{user.free_bottles.values.inject(:+)} free bootles taking full advantage of our promotion!" | |
end | |
if user.investiment.odd? | |
puts "But with $#{simulation_1.investiment} just $1 more you can get #{simulation_1.free_bottles.values.inject(:+)} free bottles" | |
elsif user.investiment != 0 | |
puts "But with $#{simulation_2.investiment} just $2 more you can get #{simulation_2.free_bottles.values.inject(:+)} free bottles" | |
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
#pop_bottles_promotion_v0.3 | |
require 'pry' | |
module Transcations | |
def exchange | |
self.drink_all | |
return if (self.bottles_empty < 2 && self.bottles_tips < 4) | |
bottles_earned = {by_empty: 0, by_tips: 0} | |
#calculates earns | |
bottles_earned[:by_empty] += self.bottles_empty/2 | |
bottles_earned[:by_tips] += self.bottles_tips/4 | |
#update client's inventory | |
self.bottles_empty = self.bottles_empty%2 | |
self.bottles_tips = self.bottles_tips%4 | |
self.free_bottles[:by_empty] += bottles_earned[:by_empty] | |
self.free_bottles[:by_tips] += bottles_earned[:by_tips] | |
self.full_bottles = bottles_earned[:by_empty] + bottles_earned[:by_tips] | |
self.exchange | |
end | |
end | |
class Person | |
include Transcations | |
attr_accessor :investiment, :full_bottles, :bottles_empty, :bottles_tips, :free_bottles | |
def initialize investiment=0, bottles_empty=0 ,bottles_tips=0 | |
@investiment = investiment | |
@full_bottles = investiment/2 | |
@bottles_empty = bottles_empty | |
@bottles_tips = bottles_tips | |
@free_bottles = {by_empty: 0, by_tips: 0} | |
end | |
def drink_all | |
@bottles_empty += full_bottles | |
@bottles_tips += full_bottles | |
@full_bottles = 0 | |
end | |
end | |
user_input = nil | |
while user_input != "quit" | |
puts "\nHow much dollars you will spend on pops?(type quit to exit)" | |
user_input = gets.chomp.downcase | |
user_input = nil if user_input == "" | |
user = Person.new(user_input.to_i) | |
user.exchange | |
simulation_1 = Person.new(user.investiment + 1 ) | |
simulation_1.exchange | |
simulation_2 = Person.new(user.investiment + 2 ) | |
simulation_2.exchange | |
unless user_input.nil? | |
puts "Dear client,\nWith $#{user.investiment} you can get up to #{user.free_bottles.values.inject(:+)} free bootles taking full advantage of our promotion!" | |
end | |
if user.investiment.odd? | |
puts "But with $#{simulation_1.investiment} just $1 more you can get #{simulation_1.free_bottles.values.inject(:+)} free bottles" | |
elsif user.investiment != 0 | |
puts "But with $#{simulation_2.investiment} just $2 more you can get #{simulation_2.free_bottles.values.inject(:+)} free bottles" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment