6 Replies - 857 Views - Last Post: 17 February 2017 - 08:04 PM Rate Topic: -----

#1 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Reformatting Question

Posted 15 February 2017 - 06:24 PM

I'm trying to reformat this code
I have no clue if there is a way to pull out the specific key which is the rand 3 digit numbers that get put in there automatically and than the value which would be the item and price
while ! products_file.eof?
  products = products_file.gets.chomp
    puts products
  end
  end


What is in that file is
123,shirt,9.99
234,pants,19.50
456,socks,3.99

but I dont want those commas in there and I want it spaced out some. I am confused on if it is even possible to reformat that.

Is This A Good Question/Topic? 0
  • +

Replies To: Reformatting Question

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Reformatting Question

Posted 16 February 2017 - 03:06 AM

You found the chomp method, there is also split to divide "into substrings based on a delimiter, returning an array of these substrings".

By spacing out you are talking about the more general topic of string formatting.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: Reformatting Question

Posted 16 February 2017 - 08:45 AM

You should also take a look at IO#each_line or even File.foreach if you don't need the file handle for anything else.
Was This Post Helpful? 0
  • +
  • -

#4 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Reformatting Question

Posted 16 February 2017 - 07:35 PM

Ok thanks for all the input. I have fixed the first part by using the split method and now I am stuck on the second part of it. The rest of the program I'm fairly sure I can do and yes I know my code is sloppy at the moment, it is a rough draft for now, I just need to get the options working and than I can clean it up and maybe use case statements instead, but here is what I got so far..
new_num = 0
product = {}
products_file=File.open("products.txt")
while ! products_file.eof?
  items = products_file.gets.chomp
  item =items.split(',')
  product[ item[0].to_i ] = [item[1].to_s, item[2].to_f]
end
products_file.close


def viewMenu
  puts "Item" + " " + "Description" + " " + "Price "
  puts "-" * 4 + " " + "-" * 11 + " " + "-" * 5
end


menu = [     "1. View all products ",
              "2. Add a new product ",
              "3. Delete a product ",
              "4. Update a product ",
              "5. View highest priced product ",
              "6. View lowest priced product ",
              "7. View sum of all product prices ",
              "8. Exit "
]
user_input = 0
while user_input != 8
puts menu

puts "Please Enter a Choice from 1-8 "
user_input = gets.chomp.to_i

if user_input == 1
   viewMenu
   product.each do |k, v|
     puts "#{k}    " + " " + "#{v[0]}" + "   " + " " + "#{v[1]}"
   end
elsif user_input == 2
  puts "Please Add a Description"
  user_description = gets.chomp

  if products_file[user_description].nil? == false
    puts "Product is already in file. "
  else
    puts "Please Enter a Price for new Item. "
    user_price = gets.chomp.to_f

    while product.has_key? (new_num)
      new_num = rand(100..999)
      product[new_num] = [[user_description], [user_price]]
    end
  end
product = products_file.gets.chomp
elsif user_input == 3
  puts "Deleted Product"
elsif user_input == 4
  puts "Update a Product"
elsif user_input == 5
  puts "View Highest Price Product"
elsif user_input == 6
  puts "View Lowsest Price product"
elsif user_input == 7
  puts "View sum of all product prices"
else
  "Good Bye"
end
  end



I mean second as in the number 2 option, adding a product, incase I was unclear, thanks in advance
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Reformatting Question

Posted 17 February 2017 - 01:20 AM

Stuck how? What is the problem with your code? Are there errors?
Was This Post Helpful? 0
  • +
  • -

#6 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Reformatting Question

Posted 17 February 2017 - 12:13 PM

There wasn't any errors but it wasn't reading this line
if products_file[user_description].nil? == false
i took it out and made it just read to the array for now until I get the rest of the program functionable, I'm working on Viewing the highest priced product now though.
Was This Post Helpful? 0
  • +
  • -

#7 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Reformatting Question

Posted 17 February 2017 - 08:04 PM

k i got all these issues worked out in this topic now, thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1