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;
}
}
Comments
Leave a comment