A hotel sales person enters sales within a text file Each li
A hotel sales person enters sales within a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as dinner, conference, lodging, etc.), the amount of the sale, and the date of the event. Write a program that reads such a file and displays the total amount for each service category. Display an error if the file does not exist or the format is incorrect.
This is needed for Java
Book \"Java for everyone late objects second edition\" page 356 problem 7.16 Thank you very much in advanced.
Solution
Below is the code and please let me know if any errors occurs.
def main():
file_name = input(\"Input file name: \")
try:
amount_by_category = process_file(file_name)
print(\'Totals: \', amount_by_category)
except Exception, e:
print(\'Error processing file:\', e)
def process_file(file_name):
infile = open(file_name, \'r\')
amount_by_catgeory = {}
for line in infile:
fields = line.split(\';\')
if len(fields) != 4:
raise Exception(\'Expected 4 fields but found %s\' % len(fields))
value = float(fields[2])
category = fields[1]
if not category in amount_by_category:
amount_by_category[category] = 0.0f
amount_by_category[category] += value
return amount_by_category
