콘텐츠로 이동

Mini Project Set 2 Answer: Parking Garage Report

Complete Program

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] hours = new int[5];

        for (int i = 0; i < hours.length; i++) {
            System.out.print("Car " + (i + 1) + " hours: ");
            hours[i] = input.nextInt();
        }

        int totalHours = 0;
        int totalFees = 0;
        int longStayCars = 0;

        for (int i = 0; i < hours.length; i++) {
            totalHours += hours[i];

            if (hours[i] > 5) {
                totalFees += 8;
                longStayCars++;
            } else if (hours[i] >= 3) {
                totalFees += 5;
            } else {
                totalFees += 3;
            }
        }

        double average = (double) totalHours / hours.length;

        System.out.println("Total hours: " + totalHours);
        System.out.println("Total fees: $" + totalFees);
        System.out.println("Long-stay cars: " + longStayCars);
        System.out.printf("Average stay: %.2f hours%n", average);

        input.close();
    }
}

Output for the Sample Input

Car 1 hours: Car 2 hours: Car 3 hours: Car 4 hours: Car 5 hours:
Total hours: 20
Total fees: $27
Long-stay cars: 2
Average stay: 4.00 hours

Key Points

  • hours.length avoids hard-coding the loop limit.
  • One traversal of the array updates the total, fee, and long-stay count together.
  • (double) totalHours prevents integer division.
  • The conditions apply the $8 rule before the $5 and $3 rules.