Name: Anonymous 2012-10-05 14:43
I have a ruby problem. I have to read a file which contains the name of an item and the price without VAT. So far I have 2 classes shop and item and a main script.
The main reads the data in the file and creates an item datatype and pushes it into an array.
The item class has 2 variables name and price_without_VAT. It also has a method real_price which calculates the price with VAT added.
shop class
In my main script I read the data from file using a loop, into an Item and push it into array.
I want to pass specific array entry and be able to call real_price to calculate price with VAT.
How do? I've tried
The main reads the data in the file and creates an item datatype and pushes it into an array.
The item class has 2 variables name and price_without_VAT. It also has a method real_price which calculates the price with VAT added.
VAT = 0.2
class Item
attr_accessor :name,:price
def initialize name,price
@name = name
@price = price
end
def real_price
price_with_vat = (@price + (@price * VAT))
return price_with_vat
end
endshop class
class Shop
def print (itemsArray)#print all items in shop, and post VAT price
itemsArray.length.times do |i|
puts itemsArray.at(i).name + " " + itemsArray.at(i).real_price.to_s
end
end
def total_value(itemsArray) #return total value of items
total = 0
itemsArray.length.times do |i|
total = total + itemsArray.at(i).real_price.to_f
end
return total
end
def average_price(itemsArray) #return average price incl VAT of items in show
average = total_value(itemsArray)/itemsArray.length
return average
end
endIn my main script I read the data from file using a loop, into an Item and push it into array.
I want to pass specific array entry and be able to call real_price to calculate price with VAT.
How do? I've tried
itemsArray.at(i).real_price but it just prints out item price without vat. also would it be best to put main script in either of the classes like shop or item class