This Java project focused on creating a time calculator for vehicle parking. I had used nested loops and other forms of calculations that allow for a seamless experience and output accurate pricing based on the hours (24 hour format) converted to minutes. I also added an overnight parking for extra credit, ensuring that the last few hours would be calculated with the hours of the following day.
import java.util.Scanner;
public class ChristopherCerrilloTapiaCS160HW2 {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
int startMinute, startHour;
int endMinute, endHour;
int spaceLimit = 40;
int totalMinutes;
int startTotal;
int endTotal;
int minutesParked;
double hoursParked;
double parkingFee;
char inputString = '=';
String anotherCar = "y";
while(anotherCar.equals("y")){
System.out.println("=== Parking Garage Calculator ===");
System.out.println("Enter Start Hour (0 - 23):");
startHour = scnr.nextInt();
while(startHour < 0 || startHour > 23){
System.out.println("Invalid hour. Please enter 0-23.");
startHour = scnr.nextInt();
}
System.out.println("Enter Start Minute (0 - 59):");
startMinute = scnr.nextInt();
while(startMinute < 0 || startMinute > 59){
System.out.println("Invalid minute. Please enter 0-59.");
startMinute = scnr.nextInt();
}
System.out.println("Enter End Hour (0 - 23):");
endHour = scnr.nextInt();
while(endHour < 0 || endHour > 23){
System.out.println("Invalid hour. Please enter 0-23.");
endHour = scnr.nextInt();
}
System.out.println("Enter End Minute (0 - 59):");
endMinute = scnr.nextInt();
while(endMinute < 0 || endMinute > 59){
System.out.println("Invalid minute. Please enter 0-59.");
endMinute = scnr.nextInt();
}
System.out.println(" "); //Calculation Break
startTotal = startHour * 60 + startMinute;
endTotal = endHour * 60 + endMinute;
//extra credit mixed with base code
if(startHour > 12 && endHour < 12){ //Overnight
//Start Time
System.out.print("Start Time: ");
System.out.printf("%02d", startHour);
System.out.print(":");
System.out.printf("%02d\n", startMinute);
//End Time
System.out.print("End Time: ");
System.out.printf("%02d", endHour);
System.out.print(":");
System.out.printf("%02d", endMinute);
System.out.println(" (next day)");
//Parking fee calculations
minutesParked = (24*60 - startTotal) + endTotal;
System.out.println("Minutes Parked: " + minutesParked);
hoursParked = minutesParked / 60.0;
System.out.print("Hours Parked: ");
System.out.printf("%.2f\n", hoursParked);
if(hoursParked <= 1.00){
parkingFee = 3.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
} else if(hoursParked > 1.01 || hoursParked <= 3.00){
parkingFee = 6.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
}else if(hoursParked > 3.01 || hoursParked <= 6.00){
parkingFee = 10.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
}else if(hoursParked < 6.01){
parkingFee = 15.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
}
}else if(endTotal >= startTotal){
//Start Time
System.out.print("Start Time: ");
System.out.printf("%02d", startHour);
System.out.print(":");
System.out.printf("%02d\n", startMinute);
//End Time
System.out.print("End Time: ");
System.out.printf("%02d", endHour);
System.out.print(":");
System.out.printf("%02d\n", endMinute);
//Parking fee calculations
minutesParked = endTotal - startTotal;
System.out.println("Minutes Parked: " + minutesParked);
hoursParked = minutesParked / 60.0;
System.out.print("Hours Parked: ");
System.out.printf("%.2f\n", hoursParked);
if(hoursParked <= 1.00){
parkingFee = 3.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
} else if(hoursParked > 1.01 || hoursParked <= 3.00){
parkingFee = 6.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
}else if(hoursParked > 3.01 || hoursParked <= 6.00){
parkingFee = 10.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
}else if(hoursParked <= 6.01){
parkingFee = 15.00;
System.out.print("Parking Fee: $");
System.out.printf("%.2f\n", parkingFee);
}
}else if(endTotal < startTotal){
System.out.println("Error: End time must be after start time (same day).");
System.out.println("Please re-enter the end time.");
}
for(i = 0; i < spaceLimit; i++){
System.out.print(inputString);
}
System.out.println(" ");
System.out.println("Calculate another car? (y/n)");
anotherCar = scnr.next();
while(!anotherCar.equals("y") && !anotherCar.equals("n") || anotherCar.equals("n")){
if(anotherCar.equals("n")){
System.out.println("Goodbye!");
break;
}else{
System.out.println("Please enter y or n.");
anotherCar = scnr.next();
}
}
}
}
}
This Java project focused on creating a time calculator for vehicle parking. I had used nested loops and other forms of calculations that allow for a seamless experience and output accurate pricing based on the hours (24 hour format) converted to minutes. I also added an overnight parking for extra credit, ensuring that the last few hours would be calculated with the hours of the following day.
import java.util.Scanner;
import java.util.Random;
public class CS160HW3ChristopherCerrilloTapia {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random rand = new Random(3307);
int[] hour = new int[18];
int[] baked = new int[18];
int[] sold = new int[18];
int[] inventory = new int[18];
int startInv = 20;
int maxStor = 40;
int sumBaked = 0;
int sumSold = 0;
int maxHour = 0;
int stockOut = -1;
int lostSales = 0;
int discarded = 0;
for(int i = 0; i < baked.length; i++){
baked[i] = rand.nextInt(11);
}
for(int i = 0; i < sold.length; i++){
sold[i] = rand.nextInt(11);
}
for(int i = 0; i < hour.length; i++){
hour[i] = i + 1;
}
for(int i = 0; i < inventory.length; i++){
inventory[i] = startInv + baked[i] - sold[i];
}
System.out.println("Seed: 3307");
System.out.println();
//first two lists for baked & sold
System.out.print("Baked: ");
for(int i = 0; i < baked.length; i++){
System.out.printf("%02d ", baked[i]);
}
System.out.println();
System.out.print("Sold: ");
for(int i = 0; i < sold.length; i++){
System.out.printf("%02d ", sold[i]);
}
System.out.println();
for(int i: baked){
sumBaked += i;
}
for(int i: sold){
sumSold += i;
}
//verification claculation and print
System.out.println();
int verification = (sumBaked * 100) + sumSold;
System.out.println("Verification: " + verification);
System.out.println();
//Hour, baked, sold, and inventory columns and labels
System.out.printf("%-7s%-7s%-7s%-6s\n", "Hour", "Baked", "Sold", "Inventory");
for(int i = 0; i < 18; i++){
System.out.printf("%02d %02d %02d %02d\n" ,
hour[i], baked[i], sold[i], inventory[i]);
}
System.out.println();
//Highest inventory loop
for(int i = 0; i < inventory.length; i++){
if(inventory[i] > startInv){
startInv = inventory[i];
maxHour = i + 1;
}
}
System.out.println("Highest inventory occured at hour " + maxHour);
//stockout loop (lists never reached 0)
for(int i = 0; i < inventory.length; i++){
if(inventory[i] <= 0 ){
stockOut = i + 1;
break;
}
}
if(stockOut != -1){
System.out.println("First stockout occured at " + stockOut);
} else {
System.out.println("Inventory never hit 0");
}
System.out.println();
//Lost sales loop and calculations (never hit 0)
for(int i = 0; i < sold.length; i++){
if(sold[i] > inventory[i]){
lostSales += sold[i] - inventory[i];
}
}
System.out.println("Total Lost Sales: " + lostSales);
//total discarded pastries (never went above inventory limit)
inventory[0] = startInv + baked[0] - sold[0];
for(int i = 0; i < inventory.length; i++){
inventory[i] = inventory[i] + baked[i] - sold[i];
if(inventory[i] < 0){
inventory[i] = 0;
}
}
System.out.println("Total discarded pastries: " + discarded);
}
}
From sketch to inbox, your passions can be realized in more ways than one!Â