Description Write a program to compute the charge for mailin
Description:
Write a program to compute the charge for mailing a series of packages. The charge for a package is based on its weight (int) and volume (int). There is a charge of $4.00 per pound for the first 2 pounds of package weight, $3.00 per pound for each pound between 2 and 6 pounds and $2.00 per pound charge for the package weight over 6 pounds. There is no additional charge for a package whose volume is 2000 cubic inches or less. There is an additional charge of $0.01 per cubic inch of package volume over 2000 cubic inches. For example if a package weighs 13 pounds and has a volume of 3000 cubic inches then the charge is 4.0*2.00 + 3.0*4.00 + 2.00*7.00 = $34.00 for the weight. Then because the volume is over 2000 cubic inches an additional 1000*0.01 = $10.00 would be added to the charge giving $44.40. There is a minimum charge of $6.00 so if the charge for a package computes to be less than $6.00 it is still charged as $6.00. The program must also compute the total number, weight, volume and charges for all the packages at the end of the transaction. With the fuel cost fluctuate rapidly; it is possible that you may have to change the rate that you charge the customers. So design your program accordingly.
This program must use a while-controlled input loop. Input the package weight right before the loop and at the end of the loop. The complete interactive I/O for this program should look like the sample below.
. In this program the inputs (package weight and dimensions) are in a file. Your program should read file and perform the necessary calculations. The rules for calculations remain the same as in Assignment 4.
Input file format: (You can create a simple text file called “package.txt” that has the following numbers)
5 10 10 15
13 30 10 10
1 6 12 8
-1
Each line represents a package. First column represents weight; the remaining three columns represent the package dimensions. The \"-1\" ends the program. The program should calculate charge for each package. NOTE that the program doesn’t know HOW MANY PACKAGES are there in the input file.
Output: as shown in the sample below, the Program should print the detailed charges for every package (ON THE SCREEN AS WELL TO AN OUTPUT FILE). Once the program encounters a -1 in the first column of a line in the file, the program must printout a summary for the whole processed packages (see the sample below).
------------------------------------------------------
package weight in pounds: 5
package dimensions in inches: 10 10 15
Weight Charge = $17.00
Final Item Charges = $17.00
------------------------------------------------------
package weight in pounds : 13
package dimensions in inches: 30 10 10
Weight Charge = $34.00
Extra Volume Charge = $10.00
Final Package Charges = $44.00
------------------------------------------------------
package weight in pounds : 1
package dimensions in inches: 6 12 8
Weight Charge = $4.00
Minimum charge = $6.00
Final Package Charges $6.00
------------------------------------------------------
package weight in pounds : -1
Totals for all Packages Processed
Number of Total Total Total
Packages Weight Volume Charges
3 19 5076 67.00
Solution
Code:
a = open(\"input.txt\",\"r\")
totalPackages = 0
totalWeight = 0
totalVolume = 0
totalCharges = 0
b = a.readlines()
for i in b:
if(i != \'-1\'):
c = i.split(\" \")
weight = int(c[0])
totalWeight += weight
volume = int(c[1])*int(c[2])*int(c[3])
totalVolume += volume
print(\"Package weight in pounds: %d\"%weight)
print(\"Package dimension in inches : %d %d %d\"%(int(c[1]),int(c[2]),int(c[3])))
if(2<weight <= 6):
weightCharge = (weight-2)*3 + 2*4;
elif(weight > 6):
weightCharge = 2*4 + 4*3 + (weight-8)*2
else:
weightCharge = weight*4
print(\"Weight Charge=$ %d\"%weightCharge)
if(weightCharge < 6):
print(\"Minimum Charge is=$ %d\"%6)
weightCharge = 6
if(volume > 2000):
volumeCharge = (volume - 2000) * 0.01
print(\"Extra volume charge=$ %d\"%volumeCharge)
finalCharges = volumeCharge + weightCharge
else:
finalCharges = weightCharge
totalCharges += finalCharges
print(\"Final Package Charges=$ %d\ \"%finalCharges)
totalPackages += 1
print(\"Totals for all Packages Processed\")
print(\"Nummber of packages \\tTotal Weight \\tTotal Volume \\tTotal Charges\ \")
print(\"%d\\t\\t\\t%d\\t\\t%d\\t\\t%d\ \"%(totalPackages,totalWeight,totalVolume,totalCharges))
Output:


