Solution to Write a program to read from a file containing the details of a restaurant order. … - Sikademy

Nov. 14, 2019

Archangel Macsika Sikademy Image

Archangel Macsika

Write a program to read from a file containing the details of a restaurant order. The details in the file include the item number, quantity and price. Store the details of at least five items in the file. Calculate the subtotal by adding the total price for each item which can be obtained by multiplying the quantity and price for each item. Calculate the tax, the tax rate can be assumed as 7%. Find the total price after adding the tax to the subtotal.

Solution in Ruby Programming Language


def restaurant_orders(file_path, total_holder)
	if (File.exist?(file_path))
		File.open(file_path, "r") do |read_all_data|
				while line = read_all_data.gets
					res_order = line.split(" ")
					calc_total = res_order[1].to_i + res_order[2].to_i
					total_holder << calc_total
				end		
				total = total_holder.inject {|x, y| x + y}
				tax = total * (7.0/100)
				puts "Total Price without Tax = %d" % [(total)]
				puts "Total Price with Tax = %d" % [(total + tax.to_f)]
		end
	else
			puts "Sorry, the requested file does not exist."
	end
end
restaurant_orders("restaurant_orders.txt", [])

If you would like to see a different solution or an implementation in a different programming language Kindly let us know