Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First - I love Python! solution in Clear category for I Love Python! by AQiccl135
def i_love_python():
"""
Because 38 lines becomes 8. (or 6 with two long ones)
(http://codereview.stackexchange.com/questions/97384/calculating-parking-fees):
PYTHON
def Hwk4B():
cars, total_hrs = int(input("Enter the number of cars parked yesterday: ")), []
for car in range(cars):
hrs = float(input("Enter hours parked for car {}: ".format(car + 1)))
total_hrs.append(min(max(2 + 0.5 * (hrs - 3.0), 2.0), 10.0))
print("Most recent customer paid: ${}".format(total_hrs[-1]))
print("Running total of receipts: ${}\n".format(sum(total_hrs)))
return
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JAVA
import java.text.DecimalFormat;
import java.util.Scanner;
public class Hwk4B {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("$##.00");
System.out.print("Enter the number of cars parked yesterday: ");
final int NUMBER_OF_CARS = keyboard.nextInt();
double hoursParked = 0;
double currentCost = 0;
double totalCost = 0;
for(int count = 1; count <= NUMBER_OF_CARS; count++) {
System.out.print("Enter hours parked for car " + count + ": ");
hoursParked = keyboard.nextDouble();
currentCost = calculateCharges(hoursParked); //most recent customer
totalCost += currentCost; //Running total
}
System.out.println("Most recent customer paid: " + formatter.format(currentCost));
System.out.println("Running total of receipts: " + formatter.format(totalCost));
}
public static double calculateCharges (double numHours) {
double garageCost = 0;
if(numHours <= 3)
garageCost = 2;
else if(numHours > 3 && numHours <= 19)
garageCost = 2.0 + 0.5 * (numHours - 3);
else if (numHours > 19)
garageCost = 10;
return garageCost;
}
}
"""
return "I love Python!"
July 20, 2015
Comments: