Answer to Question #142678 in Java | JSP | JSF for ahmar

Question #142678
A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write an application that calculates and displays the parking charges for each customer who parked in the garage yesterday. You should enter the hours parked for each customer. The program should display the charge for the current customer and should calculate and display the running total of yesterday’s receipts. It should use the method calculateCharges to determine the charge for each customer.
1
Expert's answer
2020-11-05T17:27:35-0500
package com.quest;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static class CustomerParking {
        public String customerName;
        public int hoursParked;
        public double charges;
    }

    private static List<CustomerParking> customerParkings = new ArrayList<>();

    public static void main(String[] args) {

        readCustomers();

        calculateCustomers();

        printCustomers();

    }

    private static void calculateCustomers() {
        for (CustomerParking parking : customerParkings) {
            parking.charges = calculateCharges(parking.hoursParked);
        }
    }

    private static void printCustomers() {
        System.out.println("name        charges        running total");
        double total = 0f;
        for (CustomerParking customerParking : customerParkings) {
            total = total + customerParking.charges;
            System.out.println(customerParking.customerName + "        " + customerParking.charges + "          " + total);
        }
    }

    private static void readCustomers() {
        System.out.println("Please input customers and parked hours or print \"end\" to finish");
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        while (!("end".equals(str))) {
            CustomerParking customerParking = new CustomerParking();
            String[] input = str.split(" ");
            customerParking.customerName = input[0];
            customerParking.hoursParked = Integer.valueOf(input[1]);
            customerParkings.add(customerParking);
            System.out.println("input next customer or print \"end\" to finish");
            str = in.nextLine();
        }
    }

    private static double calculateCharges(int hoursParked) {
        double charges = 2;
        if (hoursParked > 3) {
            charges = charges + (hoursParked-3)*0.5;
        }
        if (charges > 10) {
            charges = 10;
        }

        return charges;
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS