Skip to content

Commit b596cb9

Browse files
committed
Optimizes the printReport function
1 parent 22384a9 commit b596cb9

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

ElyyyBelly_A5/a5.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import string
2+
3+
def getChoice():
4+
option = raw_input("Please enter your choice: ");
5+
return option.lower();
6+
7+
def openFile():
8+
fileName = raw_input("What is the name of the file? ");
9+
fileMode = raw_input("Please enter the file mode: ");
10+
return file(fileName, fileMode);
11+
12+
def printReport(myFile):
13+
total = 0
14+
total_pizza_profit = 0;
15+
pizza_type = myFile.readline().rstrip('\n').rstrip("\r")
16+
while pizza_type != '':
17+
pizza_price = float(myFile.readline().rstrip('\n'))
18+
pizza_cost = float(myFile.readline().rstrip('\n'))
19+
pizza_profit = pizza_price - pizza_cost
20+
total += 1
21+
total_pizza_profit += pizza_profit
22+
average_profit = total_pizza_profit / total
23+
print "\n\nPizza: ", pizza_type
24+
print"Price: $", format(pizza_price, '.2f')
25+
print "Cost: $", format(pizza_cost, '.2f')
26+
print "Profit: $", format(pizza_profit, '.2f')
27+
pizza_type = myFile.readline().rstrip('\n').rstrip("\r");
28+
print "\n\nEnd of file!"
29+
print "A total of", total, "pizzas have been processed."
30+
print "The average profit is $", format(average_profit, '.2f')
31+
myFile.close()
32+
return 0;
33+
34+
def updateFile(inputFile, outputFile):
35+
numFileRecords = int(raw_input("How many records? "));
36+
for i in range(0, numFileRecords):
37+
inputPizzaType = inputFile.readline().rstrip("\n");
38+
inputPizzaPrice = float(inputFile.readline().rstrip("\n"));
39+
inputPizzaCost = float(inputFile.readline().rstrip("\n"));
40+
inputPizzaProfit = inputPizzaPrice-inputPizzaCost;
41+
outputFile.write(inputPizzaType+"\n"+str(inputPizzaPrice)+"\n"+str(inputPizzaCost)+"\n"+str(inputPizzaProfit)+"\n");
42+
outputFile.close();
43+
inputFile.close();
44+
45+
46+
def main():
47+
getPrompt = "y";
48+
welcomeMessage = "Welcome to the file Program\n";
49+
welcomeMessage += "=============================\n";
50+
welcomeMessage += "P - Print Report\n";
51+
welcomeMessage += "U - Update File";
52+
while (getPrompt == "y"):
53+
print welcomeMessage;
54+
option = getChoice();
55+
if(option == "u"):
56+
print "Open input file: \n"
57+
inputFileName = raw_input("What is the name of the file? ")
58+
inputFileMode = raw_input("Please enter file mode: ");
59+
inputFile = file(inputFileName, inputFileMode);
60+
print "Open output file: \n"
61+
outputFileName = raw_input("What is the name of the file? ");
62+
outputFileMode = raw_input("Please enter file mode: ");
63+
outputFile = file(outputFileName, outputFileMode);
64+
updateFile(inputFile, outputFile);
65+
elif(option == "p"):
66+
myFile = openFile();
67+
printReport(myFile);
68+
else:
69+
print "Invalid choice!\n\n";
70+
getPrompt = raw_input("Do you want to continue? (Y or y for yes):").lower();
71+
return 0;
72+
73+
main();
74+

0 commit comments

Comments
 (0)