This file contains 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
CIS 282 | |
Files Assignment | |
To turn in: | |
Attach a file files_lastname.rb as an attachment to the assignment in Blackboard | |
Subject line: CIS 282, Name, Files | |
To do: | |
Take your program from the Hash Assignment and modify it to use files for storing the data instead of initializing the data at run-time. |
This file contains 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
def print_out(string) | |
STDOUT << string | |
end | |
def products | |
@products ||= { 827 => {'name' => "ARM", 'price' => 349.99}, | |
199 => {'name' => "LEG", 'price' => 224.75}, | |
776 => {'name' => "FIRSTBORN", 'price' => 1499.95}, | |
222 => {'name' => "LIFESAVINGS", 'price' => 49.99}, | |
811 => {'name' => "RETIREMENT", 'price' => 49.99} |
This file contains 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
when 3 | |
print "Enter the name of the product you want to delete: " | |
$stdout.flush | |
name = gets.chomp.upcase | |
findkey = product.index("name") | |
product.delete(key) | |
puts product.inspect.upcase | |
if ! product.has_key?[name, price] | |
print "This product is not in the list.\n" | |
end |
This file contains 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
def update_product | |
product.each{ |key, value| | |
puts "#{key} - #{value[0]}, #{value[1]}" | |
} | |
print "Enter the key number of the product you wish to update: " | |
$stdout.flush | |
keynumber = gets.chomp.upcase | |
print "Enter 9 to change the product name, or 10 to change the price: " | |
$stdout.flush |
This file contains 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
def products | |
@products ||= { 827 => ["ARM",349.99], | |
199 => ["LEG", 224.75], | |
776 => ["FIRSTBORN",1499.95], | |
222 => ["LIFESAVINGS",49.99], | |
811 => ["RETIREMENT",49.99]} | |
end | |
puts | |
print "1. Add a product |
This file contains 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
product = {827 => ["ARM",349.99], | |
199 => ["LEG", 224.75], | |
776 => ["FIRSTBORN",1499.95], | |
222 => ["LIFESAVINGS",49.99], | |
811 => ["RETIREMENT",49.99]} | |
choice = 0 | |
if choice <= 7 | |
puts |