Created
          November 20, 2013 00:45 
        
      - 
      
 - 
        
Save sdanko11/7555441 to your computer and use it in GitHub Desktop.  
    Cashier Calculator V2
  
        
  
    
      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
    
  
  
    
  | class Register | |
| attr_reader :complete | |
| def initialize | |
| @coffees = [ | |
| {type: "Light", price: 5, ordered: 0}, | |
| {type: "Medium", price: 7.5, ordered: 0}, | |
| {type: "Bold", price: 9.75, ordered: 0}] | |
| @subtotal = 0 | |
| @complete = false | |
| end | |
| def good_coffe_choice?(input) | |
| (1..3).include?(input.to_i) && !(input =~ /\D/) | |
| end | |
| def good_number?(input) | |
| return false if input =~ /\D/ | |
| true | |
| end | |
| def good_tendered?(input) | |
| if !(input =~ /^(\d*)\.\d{2}$/) | |
| return false | |
| end | |
| true | |
| end | |
| def show_menu | |
| puts "Welcome to James' coffee emporium!\n\n" | |
| puts "1) Add item - $5.00 - Light Bag" | |
| puts "2) Add item - $7.50 - Medium Bag" | |
| puts "3) Add item - $9.75 - Bold Bag" | |
| puts "4) Complete Sale" | |
| end | |
| def select_coffee | |
| puts "\nMake a selection:" | |
| input = gets.chomp | |
| if good_coffe_choice?(input) | |
| input = input.to_i - 1 | |
| bags_purchased = number_of_bags | |
| @coffees[input][:ordered] = bags_purchased | |
| @subtotal += @coffees[input][:price] * bags_purchased | |
| show_subtotal | |
| elsif input == '4' | |
| @complete = true | |
| else | |
| puts "Invalid input detected." | |
| end | |
| end | |
| def number_of_bags | |
| puts 'How many bags?' | |
| bags_purchased = gets.chomp | |
| while !good_number?(bags_purchased) | |
| puts "Invalid number. How many bags?" | |
| bags_purchased = gets.chomp | |
| end | |
| bags_purchased.to_i | |
| end | |
| def show_subtotal | |
| puts "Subtotal: $#{sprintf( "%0.02f", @subtotal)}" | |
| end | |
| def sale_complete | |
| puts "===Sale Complete===\n\n" | |
| @coffees.each do |coffee| | |
| if coffee[:ordered] > 0 | |
| puts "$#{sprintf( "%0.02f", coffee[:ordered] * coffee[:price])} - #{coffee[:ordered]} #{coffee[:type]}" | |
| end | |
| end | |
| puts "\nTotal: $#{sprintf( "%0.02f", @subtotal.to_f)}" | |
| amount_tendered | |
| end | |
| def amount_tendered | |
| puts "\nWhat is the amount tendered?" | |
| tendered = gets.chomp | |
| while !good_tendered?(tendered) | |
| puts "WARNING: Invalid currency detected!" | |
| puts "What is the amount tendered?" | |
| tendered = gets.chomp | |
| end | |
| if tendered.to_f < @subtotal.to_f | |
| dif = (subtotal.to_f - tendered.to_f) | |
| puts "WARNING: Customer still owes $#{sprintf( "%0.02f", dif)}!" | |
| amount_tendered | |
| end | |
| give_change(tendered) | |
| end | |
| def give_change(tendered) | |
| puts "\n===Thank You!===" | |
| puts "The total change due is $#{sprintf( "%0.02f", (tendered.to_f - @subtotal.to_f))}" | |
| puts "\n" + Time.now.strftime("%m/%d/%Y %I:%M%p") | |
| puts "================" | |
| 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
    
  
  
    
  | # User story 1: | |
| # As James | |
| # I want to be able to enter a kind of coffee and get the price | |
| # so he does not have to enter the price | |
| # Acceptance criteria: | |
| # -When entering a transaction | |
| # -Display a list of coffees with a corresponding number | |
| # -Show the price corresponding to each type of coffee | |
| # -Allow James to select a coffee by its number | |
| # User story 2: | |
| # As James | |
| # I want to choose a coffee and enter how many were purchased | |
| # So that they can be added to the transaction | |
| # Acceptance criteria: | |
| # -james chooses a coffee | |
| # -he is asked how many of the coffees were purchased | |
| # -he gets the total amount of the transaction based on the coffee that was entered | |
| # -the total is added to the transaction | |
| require_relative 'register' | |
| register = Register.new | |
| register.show_menu | |
| while !register.complete | |
| register.select_coffee | |
| end | |
| register.sale_complete | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment