2013-10-08 75 views
-1

我正在寫一個程序,允許用戶輸入日期並輸入他們在特定日期所需的股票信息的類型。所有的股票信息和日期都在一個單獨的CSV文件中。我的程序不會運行,我覺得我好像缺少了一些東西。這是一個類,我不想使用Ruby的CSV類。我如何查看GE股票信息?

下面是這個文件看起來什麼樣,如:

date,open,high,low,close,volume,changed,changep,adjclose,tradeval,tradevol 
2013-10-07,23.84,23.90,23.80,23.89,3522559,-0.16,-0.67%,23.89,83992937.36,8462 
2013-10-04,24.18,24.18,23.90,24.05,33274615,-0.05,-0.21%,24.05,800232596.05,74361 
2013-10-03,24.22,24.25,23.84,24.10,37466161,-0.23,-0.95%,24.10,902130194.02,95122 

這是我的計劃:

#open the file 
data = File.open("data.csv","r+") 

#make an empty hash 
stocks = {} 

contents = data.readlines 
data.close 

#this add quotes between each line 
contents.collect! do |x| 
    x.chomp 
end 
#this splits up each in into its own array 

contents.collect! do |x| 
    x.split(',') 
end 



contents.each do |x| 
    stocks[x[0]] = x 
end 



puts "This program has all the General Electic stock information from November 27, 1960 to October 8 2013. Please enter the date you would like to find the stock information of like this: 1997-10-30 (year-month-day)." 
#prompt user for the date of the stock info they would like to find 
date = gets.chomp 

data = stocks[date] 


puts "Please enter what information about the stock you would like to know: open, high, low, close, volume, changed, percent change,adjusted closing, trade value, or trade volume. Please put a underscore in place of all spaces." 
#get an input for what type of stock information the user would like 
input = gets.chomp 
#elsif statement to give the user the info they need based on what stock info they want 


if input == open 
    puts "The open of your stock is: #{data[1]}" 
    elsif input == high 
      puts "The high of your stock is: #{data[2]}" 
    elsif input == low 
      puts "The low of your stock is: #{data[3]}" 
    elsif input == close 
      puts "The close of your stock is: #{data[4]}" 
    elsif input == volume 
      puts "The volume of your stock is: #{data[5]}" 
    elsif input == changed 
      puts "The volume of your stock is: #{data[6]}" 
    elsif input == percent_change 
      puts "The percent change of your stock is: #{data[7]}" 
    elsif input == adjusted_closing 
      puts "The open adjusted closing of your stock is: #{data[8]}" 
    elsif input == trade_value 
      puts "The trade value of your stock is: #{data[9]}" 
    else input == trade_volume 
      puts "The trade volume of your stock is: #{data[10]}" 

    end 
+0

這是對您的問題代碼進行的徹底編輯,使答案看起來不相關。 –

回答

0

這裏是你的代碼清理了一下:

require 'csv' 

puts "This program has all the General Electric stock information from November 
27, 1960 to October 8 2013. Please enter the date you would like to find the 
stock information of like this: 1997-10-30 (year-month-day)." 
date = gets.chomp 

puts "Please enter what information about the stock you would like to know: 
open, high, low, close, volume, changed, percent change,adjusted closing, trade 
value, or trade volume. Please put a underscore in place of all spaces." 
input = gets.chomp 

CSV.foreach(
    'test.csv', 
    :headers => true, 
    :return_headers => false 
) do |row| 

    next unless row['date'] == date 

    if input == 'open' 
    puts "The open of your stock is: #{ row['open'] }" 
    elsif input == 'high' 
    puts "The high of your stock is: #{ row['high'] }" 
    elsif input == 'low' 
    puts "The low of your stock is: #{ row['low'] }" 
    elsif input == 'close' 
    puts "The close of your stock is: #{ row['close'] }" 
    elsif input == 'volume' 
    puts "The volume of your stock is: #{ row['volume'] }" 
    elsif input == 'changed' 
    puts "The volume of your stock is: #{ row['changed'] }" 
    elsif input == 'percent_change' 
    puts "The percent change of your stock is: #{ row['percent_change'] }" 
    elsif input == 'adjusted_closing' 
    puts "The open adjusted closing of your stock is: #{ row['adjusted_closing'] }" 
    elsif input == 'trade_value' 
    puts "The trade value of your stock is: #{ row['trade_value'] }" 
    elseif input == 'trade_volume' 
    puts "The trade volume of your stock is: #{ row['trade_volume'] }" 
    else 
    puts "An unknown option was entered." 
    end 

end 

隨着這些輸入:

1997-10-30 
open 

返回:

The open of your stock is: 23.84 
The open of your stock is: 24.18 
The open of your stock is: 24.22 

使用Ruby的CSV類。它已經寫好了,並且已經過調試和測試,所以你不必重新發明這個輪子。您可以告訴它使用CSV文件的第一行作爲標題,以及是否應該返回這些文件。此外,它可以將一行數據作爲數組或散列返回。把它作爲哈希函數返回對你想要做的事情非常有用。


這裏有一些重構向您展示如何簡化和刪除冗餘代碼:

require 'csv' 

puts "This program has all the General Electric stock information from November 
27, 1960 to October 8 2013. Please enter the date you would like to find the 
stock information of like this: 1997-10-30 (year-month-day)." 
date = gets.chomp 

puts "Please enter what information about the stock you would like to know: 
open, high, low, close, volume, changed, percent change,adjusted closing, trade 
value, or trade volume. Please put a underscore in place of all spaces." 
input = gets.chomp 

CSV.foreach(
    'test.csv', 
    :headers => true, 
    :return_headers => false 
) do |row| 

    next unless row['date'] == date 

    case input 
    when 'open' 
    puts "The open of your stock is: #{ row['open'] }" 
    when 'high' 
    puts "The high of your stock is: #{ row['high'] }" 
    when 'low' 
    puts "The low of your stock is: #{ row['low'] }" 
    when 'close' 
    puts "The close of your stock is: #{ row['close'] }" 
    when 'volume' 
    puts "The volume of your stock is: #{ row['volume'] }" 
    when 'changed' 
    puts "The volume of your stock is: #{ row['changed'] }" 
    when 'percent_change' 
    puts "The percent change of your stock is: #{ row['percent_change'] }" 
    when 'adjusted_closing' 
    puts "The open adjusted closing of your stock is: #{ row['adjusted_closing'] }" 
    when 'trade_value' 
    puts "The trade value of your stock is: #{ row['trade_value'] }" 
    when 'trade_volume' 
    puts "The trade volume of your stock is: #{ row['trade_volume'] }" 
    else 
    puts "An unknown option was entered." 
    end 

end 

這個版本替換if/elseif/elsecase/when這有助於簡化邏輯,並返回相同的事情了相同的輸入。


require 'csv' 

puts "This program has all the General Electric stock information from November 
27, 1960 to October 8 2013. Please enter the date you would like to find the 
stock information of like this: 1997-10-30 (year-month-day)." 
date = gets.chomp 

puts "Please enter what information about the stock you would like to know: 
open, high, low, close, volume, changed, percent change,adjusted closing, trade 
value, or trade volume. Please put a underscore in place of all spaces." 
input = gets.chomp 

CSV.foreach(
    'test.csv', 
    :headers => true, 
    :return_headers => false 
) do |row| 

    next unless row['date'] == date 

    val = case input 
     when 'open' 
      'open' 
     when 'high' 
      'high' 
     when 'low' 
      'low' 
     when 'close' 
      'close' 
     when 'volume' 
      'volume' 
     when 'changed' 
      'changed' 
     when 'percent_change' 
      'percent_change' 
     when 'adjusted_closing' 
      'adjusted_closing' 
     when 'trade_value' 
      'trade_value' 
     when 'trade_volume' 
      'trade_volume' 
     else 
      puts "An unknown option was entered." 
      next 
     end 

    puts "The %s of your stock is: %s" % [val.gsub('_', ' '), row[val] ] 

end 

這建立在case聲明,只返回什麼樣的變化,同時也向我們展示了大量的冗餘。


require 'csv' 

puts "This program has all the General Electric stock information from November 
27, 1960 to October 8 2013. Please enter the date you would like to find the 
stock information of like this: 1997-10-30 (year-month-day)." 
date = gets.chomp 

puts "Please enter what information about the stock you would like to know: 
open, high, low, close, volume, changed, percent change, adjusted closing, 
trade value, or trade volume. Please put a underscore in place of all spaces." 
input = gets.chomp 

CSV.foreach(
    'test.csv', 
    :headers => true, 
    :return_headers => false 
) do |row| 
    puts "The %s of your stock is: %s" % [input.gsub('_', ' '), row[input] ] if (row['date'] == date) 
end 

剔除在此代碼冗餘結果。

+0

您可以告訴我如何在不使用Ruby的csv類的情況下執行此操作嗎? – user2759592

+1

雖然可以重寫CSV模塊,但我不會去嘗試;過去我曾經去過那麼多次。 CSV是一種嚴重濫用的數據格式,對於簡單的任務,編寫簡單的解析器並不難。我可以告訴你如何,但我會收取你的費用;你可以使用'IO.foreach'和'String.split'來找出一個簡單的問題,只要你不處理嵌入的逗號,你應該沒問題。但是,*爲什麼*你不想使用內置工具來做到這一點?忽略內置功能是一個不好的決定。 –

+0

我更新了我的問題 – user2759592