Skip to content

Instantly share code, notes, and snippets.

@xianny
Forked from donburks/lighthouse-w1d5-poppin-bottles.md
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xianny/1b6ec25be290c6c28bd8 to your computer and use it in GitHub Desktop.
Save xianny/1b6ec25be290c6c28bd8 to your computer and use it in GitHub Desktop.

Poppin' Bottles

You've just been hired at Lighthouse Markets, a reputable (and thoroughly fictional) grocery store chain. One of the primary features of Lighthouse Markets is their recycling program on pop bottles. Here is how the program works:

  • For every two empty bottles, you can get one free (full) bottle of pop
  • For every four bottle caps, you can get one free (full) bottle of pop
  • Each bottle of pop costs $2 to purchase

Given these parameters, write a program so that you can figure out how many total bottles of pop can be redeemed given a customer investment.

Task 1

Figure out the algorithm that will calculate this. For example, given a $20 investment, the customer will get 10 bottles of pop. That gives a supply of both bottles and bottle caps that can be used to redeem for further bottles, which will then produce a further supply for recycling.

Task 2

Write a REPL that will prompt the user for the amount (in dollars) that the customer is going to spend. The REPL will then calculate the total number of bottles that the customer will receive and prompt for the next customer's amount.

Task 3

Enhance the output of your program so that once the amount has been given, it provides a breakdown of how many bottles were purchased, how many were obtained through bottle recycling, and how many were obtained through bottle cap recycling.

Task 4

Add to the output, so that the program will tell the customer how many bottles and bottle caps they will have left over. We have to upsell the customer on buying more pop after all!

TIP! Iterate, Iterate, Iterate

You hear this from us all the time, but build your code incrementally. Build it in small steps, and make sure you are confident and comfortable that the code is functioning correctly before you move on to the next step.

TIP! @instance variables

Your use of instance variables for keeping track of different variables will be very important here.

BONUS!

Write this same algorithm using recursion.

class Account
attr_accessor :bottlecaps
attr_accessor :empties
attr_accessor :current_redemptions
def initialize
@bottlecaps = 0
@empties = 0
@current_redemptions = 0
end
def add_bottles(num)
@bottlecaps += num
@empties += num
end
def lose_bottles(num)
@bottlecaps -= num
@empties -= num
end
## Redeems bottles once
def redeem_bottles
new_bottles = @bottlecaps/4 + @empties/2
@current_redemptions += new_bottles
current_bottlecaps = @bottlecaps
current_empties = @empties
@bottlecaps = @bottlecaps % 4
@empties = @bottlecaps % 2
add_bottles(new_bottles)
end
## Redeems bottles as long as they can be redeemed. Returns total number of redemptions.
def redeem_all
while @bottlecaps >= 4 || @empties >=2
redeem_bottles
end
@current_redemptions
end
end
class RecyclingProgram
def initialize(name)
@name = name
@open = false
@bottlecaps = 0
@empties = 0
end
def open
@open = true
while @open
open_for_business
end
end
## open for business!
def open_for_business
puts "Welcome to #{name}."
puts "BUY -- add new bottles to your account"
puts "REDEEM -- redeem bottles: lose and add bottles to your account"
puts "CHECK -- check bottles in account"
puts "BYE -- close recycling program"
input = gets.chomp.upcase
handle(input)
end
def handle(input)
case input
when "BUY" then ui_buy
when "REDEEM" then ui_redeem
when "CHECK" then ui_check
when "BYE" then shut
else
puts "Input not recognised!"
end
end
def shut
puts "Thanks for recycling!"
@open = false
end
def ui_buy
puts "How much would you like to spend on bottles today? e.g. 20 for $20"
investment = gets.chomp
buy_pop(investment)
display_account ## TODO
end
def buy_pop(dollars)
new_bottles = dollars/2
@bottlecaps += new_bottles
@empties += new_bottles
end
def redeem
redeem_total = 0
new_bottles = 0
new_bottles += @bottlecaps/4
@bottlecaps = @bottlecaps % 4
new_bottles += @empties/2
@empties = @empties % 2
@bottlecaps += new_bottles
@empties += new_bottles
redeem_total += new_bottles
while @bottlecaps > 4 || @empties > 2
redeem
end
redeem_total
end
end
lighthouse = RecyclingProgram.new("Lighthouse Markets")
puts "#{lighthouse.buy_pop(20)}"
puts "#{lighthouse.redeem}"
require_relative 'account.rb'
class SimpleProgram
@running = false
def run
@running = true
while @running
cash_register
end
end
def cash_register
puts "How much $$ would you like to spend?"
investment = gets.chomp.match('\d+')
investment = investment[0].to_i
customer = Account.new
customer.add_bottles(investment/2)
puts "If you spend $#{investment}, you will get a total of #{customer.redeem_all} bottles through our recycling program"
puts "You will then have #{customer.bottlecaps} bottlecaps and #{customer.empties} empty bottle left."
end
end
lighthouse = SimpleProgram.new
lighthouse.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment