PR2-Testate/QualifierTeil2/Parkhaus.java

75 lines
1.8 KiB
Java

package QualifierTeil2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Parkhaus {
Map<String, PKW> parkendePkws;
int kapazität;
int belegt;
long parkdauer;
double parkgebühren;
public Parkhaus() {
this.parkendePkws = new HashMap<>();
this.kapazität = randomAnzahlParkplätze();
this.belegt = 0;
this.parkdauer = berechneParkdauer();
this.parkgebühren = berechneParkgebuehr();
}
public static int randomAnzahlParkplätze(){
return (int) (Math.random() * 200) + 1;
}
public static long berechneParkdauer(){
var sdf = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
Date d1 = null;
try {
d1 = sdf.parse("01.10.2024, 9:45");
} catch (ParseException e) {
throw new RuntimeException(e);
}
Date d2 = null;
try {
d2 = sdf.parse("01.10.2024, 11:45");
} catch (ParseException e) {
throw new RuntimeException(e);
}
long minDiff = (d2.getTime() - d1.getTime()) / 60000;
return minDiff;
}
public int berechneParkgebuehr(){
long pd = this.parkdauer;
int gebuehr = 0;
if (pd == 1440){
gebuehr = 1500;
pd = 0;
}
//Wenn zu zahlenden Parkdauer vorhanden ist, sind die ersten 15 min kostenlos
if (pd > 0){
pd = pd - 15;
}
//Wenn noch zu zahlenden Parkdauer vorhanden ist, wird der Rest mit 1€ pro 1h angerechnet.
if (pd > 0){
while (pd > 0) {
gebuehr += 100;
pd -= 60;
}
}
return gebuehr;
}
}