Skip to content

Instantly share code, notes, and snippets.

@Lara-zz
Created June 9, 2009 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Lara-zz/126174 to your computer and use it in GitHub Desktop.
Save Lara-zz/126174 to your computer and use it in GitHub Desktop.
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.
Write a program that will read a datafile to initialize its Hash on startup.
When the user selects Exit, your program should then write the Hash into the same data file so that it can be read the next time it is run. Any new items that are created by the user need to be written to the file at the same time.
The format of the file should be:
123,shirt,9.99
234,pants,19.50
456,socks,3.99
# Now that the loop is gone, the program exits after each option. I choose an option, it exits. Not supposed to exit until I pick the Quit option!
# Since I exit after each option, it doesn't save any new added items past the "Add" option exit. Writing new added items to the file -- as well as current products.txt items -- is one of the main points of the assignment.
# The new item also has to be saved and included when I run option 5, lowest and highest-priced products, which is not possible when it isn't saved past the end of the Add block.
# Still need to figure out how to reconfigure my #5 to display in a user-friendly way. "472hat1597" is right, but not clear.
# Now that the loop is gone, the program exits after each option. I choose an option, it exits. Not supposed to exit until I pick the Quit option!
# Since I exit after each option, it doesn't save any new added items past the "Add" option exit. Writing new added items to the file -- as well as current products.txt items -- is one of the main points of the assignment.
# The new item also has to be saved and included when I run option 5, lowest and highest-priced products, which is not possible when it isn't saved past the end of the Add block.
# Still need to figure out how to reconfigure my #5 to display in a user-friendly way. "472hat1597" is right, but not clear.
hash_hartman_startover = {}
files_assignment = File.open( "products.txt", "r")
files_assignment.each_line do |line|
id, name, price = line.chomp.split(',')
# same as:
# items = line.chomp.split(',')
# id = items[0]
# name = items[1]
# price = items[2]
hash_hartman_startover[id] = [name, price]
end
$stdout << hash_hartman_startover.inspect
while true
print "\n1. View all products
2. Add a new product
3. Delete a product
4. Update a product
5. View highest-priced product and lowest-priced product
6. View sum of all product prices
7. Quit \n
Choose an option: "
$stdout.flush
choice = gets.chomp.to_i
if choice > 7
print "That\'s not one of the choices. Try again: \n"
end
if choice == 1
hash_hartman_startover.each{ |key, value|
puts "#{key} - #{value[0]}, #{value[1]}"
}
end
if choice == 2
print "Enter the product name: "
$stdout.flush
name = gets.chomp.upcase
print "Enter the product price using a decimal (as in 44.99), with no dollar sign: "
$stdout.flush
price = gets.chomp.to_f
#generate random key, make sure it's unique
key = rand(899) + 100
while hash_hartman_startover.has_key?(key)
key = rand(899) + 100
end
puts hash_hartman_startover[key] = [name, price]
puts "You have entered a new product called #{name} with an assigned key of #{key}."
puts hash_hartman_startover.inspect.upcase
end
if choice == 3
hash_hartman_startover.each{ |key, value|
puts "#{key} - #{value[0]}, #{value[1]}"
}
print "Enter the key number of the product you wish to delete: "
$stdout.flush
keynum = gets.chomp.to_i
if hash_hartman_startover.has_key?(keynum)
puts hash_hartman_startover[keynum]
hash_hartman_startover.delete(keynum)
hash_hartman_startover.each{ |key, value|
puts "#{key} - #{value[0]}, #{value[1]}"
}
elsif ! hash_hartman_startover.has_key?(keynum)
print "This product is not in the list.\n"
end
end
if choice == 4
hash_hartman_startover.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.to_i
end
if hash_hartman_startover.has_key?(keynumber)
puts hash_hartman_startover[keynumber]
print "Enter the name you want assigned to this product (it can be the same name as before): "
$stdout.flush
new_name = gets.chomp.upcase
puts new_name.upcase
print "Enter the price you want assigned to this product (it can be the same price as before): "
$stdout.flush
new_price = gets.chomp.upcase
hash_hartman_startover.delete(keynumber)
hash_hartman_startover[keynumber] = new_name, new_price
hash_hartman_startover.each{ |key, value|
puts "#{key} - #{value[0]} - #{value[1]}"
}
elsif ! hash_hartman_startover.has_key?(keynumber)
print "This product is not in the list.\n"
end
if choice == 5
hash_hartman_startover.each{ |key, value|
puts "#{key} - #{value[0]}, #{value[1]}"
}
sorted = hash_hartman_startover.sort_by{|product| product[1] }
puts "The cheapest product is #{sorted.last}"
puts "The most expensive product is #{sorted.first}"
end
if choice == 6
sum = 0
hash_hartman_startover.each {|key, value|
puts "#{value[1]}"
sum += value[1]
}
puts sum
end
if choice == 7
#~ files_assignment= File.open("products.txt", "w")
#~ item_array = files_assignment.readlines
#~ item_array.each{ |i| i.chomp!.upcase! }
#~ files_assignment.puts item_array.join("\n")
#~ files_assignment.close
#~ exit
formatted_products = hash_hartman_startover.map do |key, value|
"#{key},#{value.join(",")}"
end
formatted_products = formatted_products.join("\n")
# $stdout << formatted_products
files_assignment = File.open("products.txt", "w")
files_assignment << formatted_products
files_assignment.close
exit
end
#This is a debugging exit, get rid of it when finished programming
if choice == 8
exit
end
end
243,tie,19.95
714,shoes,27.95
108,socks,99.99
472,hat,15.97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment