Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A hotel sales person enters sales within a text file. Each line contains the fol

ID: 3932521 • Letter: A

Question

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.

Explanation / Answer

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote