202 lines
7.0 KiB
Java
202 lines
7.0 KiB
Java
package de.deversmann;
|
|
|
|
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.Duration;
|
|
import java.util.*;
|
|
|
|
public class Parkhaus {
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
|
|
Scanner scanner = new Scanner(System.in);
|
|
private boolean geöffnet;
|
|
private int anzahlParkplätze;
|
|
private final double ANGEFANGENE_STUNDE_PREIS = 1.00;
|
|
private final double TAGES_MAX_PREIS = 15.00;
|
|
private final double NACHT_MAX_PREIS = 5.00;
|
|
private final double E_AUTO_RABATT = 0.8;
|
|
private final int FREIE_MINUTEN = 15;
|
|
|
|
private int belegteParkplätze;
|
|
private double kosten;
|
|
private double ticketPreis;
|
|
private double maxTagessatz;
|
|
private final int geöffnetAb = 6;
|
|
private final int geschlossenAb = 22;
|
|
|
|
public boolean isGeöffnet() {
|
|
|
|
|
|
return geöffnet;
|
|
}
|
|
|
|
public void setGeöffnet(boolean geöffnet) {
|
|
this.geöffnet = geöffnet;
|
|
}
|
|
|
|
public int getAnzahlParkplätze() {
|
|
return anzahlParkplätze;
|
|
}
|
|
|
|
public void setAnzahlParkplätze(int anzahlParkplätze) {
|
|
this.anzahlParkplätze = anzahlParkplätze;
|
|
}
|
|
|
|
public double getKosten() {
|
|
return kosten;
|
|
}
|
|
|
|
public void setKosten(double kosten) {
|
|
this.kosten = kosten;
|
|
}
|
|
|
|
public double getTicketPreis() {
|
|
return ticketPreis;
|
|
}
|
|
|
|
public void setTicketPreis(double ticketPreis) {
|
|
this.ticketPreis = ticketPreis;
|
|
}
|
|
|
|
public double getMaxTagessatz() {
|
|
return maxTagessatz;
|
|
}
|
|
|
|
public void setMaxTagessatz(double maxTagessatz) {
|
|
this.maxTagessatz = maxTagessatz;
|
|
}
|
|
|
|
public int getBelegteParkplätze() {
|
|
return belegteParkplätze;
|
|
}
|
|
|
|
public void setBelegteParkplätze(int belegteParkplätze) {
|
|
this.belegteParkplätze = belegteParkplätze;
|
|
}
|
|
|
|
|
|
public ArrayList berechneParkdauer(String einfahrtZeit, String ausfahrZeit) {
|
|
ArrayList parkdauer = new ArrayList();
|
|
simpleDateFormat.setLenient(false);
|
|
try {
|
|
Date einfahrtZeitDate = simpleDateFormat.parse(einfahrtZeit);
|
|
Date ausfahrZeitDate = simpleDateFormat.parse(ausfahrZeit);
|
|
Duration duration = Duration.ofMillis(Math.abs(ausfahrZeitDate.getTime() - einfahrtZeitDate.getTime()));
|
|
long days = duration.toDays();
|
|
long hours = duration.toHours() % 24;
|
|
long minutes = duration.toMinutes() % 60;
|
|
|
|
Collections.addAll(parkdauer, days, hours, minutes);
|
|
} catch (ParseException e) {
|
|
System.out.println("Fehler beim Parsen der Daten" + e.getMessage());
|
|
}
|
|
|
|
return parkdauer;
|
|
}
|
|
|
|
public double berechnePreis(Auto auto) throws ParseException {
|
|
double preis = 0;
|
|
simpleDateFormat.setLenient(false);
|
|
Date einfahrtZeitDate = simpleDateFormat.parse(auto.getEinfahrtZeit());
|
|
Date ausfahrtZeitDate = simpleDateFormat.parse(auto.getAusfahrtZeit());
|
|
int einfahrtStunde = einfahrtZeitDate.getHours();
|
|
int ausfahrtStunde = ausfahrtZeitDate.getHours();
|
|
long parkdauerInMinuten = (ausfahrtZeitDate.getTime() - einfahrtZeitDate.getTime()) / (60 * 1000);
|
|
|
|
if (parkdauerInMinuten < FREIE_MINUTEN) {
|
|
return 0;
|
|
}
|
|
|
|
long kostenpflichtigeMinuten = parkdauerInMinuten - FREIE_MINUTEN;
|
|
double kostenPflichtigeStunden = Math.ceil(((double)kostenpflichtigeMinuten / 60));
|
|
|
|
long parkdauerInTagen = (long) Math.ceil(parkdauerInMinuten / (60.0 * 24));
|
|
|
|
|
|
if (parkdauerInTagen >= 1) {
|
|
long kostenpflichtigeGanzeTage = (long) Math.floor(kostenPflichtigeStunden / 24);
|
|
if (kostenpflichtigeGanzeTage >= 1) {
|
|
|
|
preis = kostenpflichtigeGanzeTage * TAGES_MAX_PREIS;
|
|
long übrigenStunden = (long) (kostenPflichtigeStunden - kostenpflichtigeGanzeTage * 24);
|
|
if (übrigenStunden <= 5) {
|
|
preis = preis + (übrigenStunden * ANGEFANGENE_STUNDE_PREIS);
|
|
} else if (einfahrtStunde >= 20) {
|
|
long stundenImAltenTag = (einfahrtStunde + übrigenStunden) - 24;
|
|
long stundenImNeuenTag = übrigenStunden - stundenImAltenTag;
|
|
if (stundenImNeuenTag <= 5) {
|
|
preis += 5;
|
|
} else {
|
|
preis += 5 + stundenImNeuenTag - 5;
|
|
}
|
|
|
|
} else if (einfahrtStunde <= 5) {
|
|
long angebrocheneStundenAusserhalbDerNacht = (long) 6 - einfahrtStunde;
|
|
long übrigeStundenAusserhalb = übrigenStunden - einfahrtStunde;
|
|
if (angebrocheneStundenAusserhalbDerNacht == 6) {
|
|
preis += 5;
|
|
} else {
|
|
preis += 5 + übrigeStundenAusserhalb;
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
|
|
long übrigenStunden = (long) (kostenPflichtigeStunden - kostenpflichtigeGanzeTage * 24);
|
|
if (übrigenStunden <= 5) {
|
|
preis = preis + (übrigenStunden * ANGEFANGENE_STUNDE_PREIS);
|
|
} else if (einfahrtStunde >= 20) {
|
|
long stundenImAltenTag = (einfahrtStunde + übrigenStunden) - 24;
|
|
long stundenImNeuenTag = übrigenStunden - stundenImAltenTag;
|
|
if (stundenImNeuenTag <= 5) {
|
|
preis += 5;
|
|
} else {
|
|
preis += 5 + stundenImNeuenTag - 5;
|
|
preis = preis > 15 ? 15 : preis;
|
|
}
|
|
|
|
} else if (einfahrtStunde <= 5) {
|
|
long angebrocheneStundenAusserhalbDerNacht = (long) 6 - einfahrtStunde;
|
|
long übrigeStundenAusserhalb = übrigenStunden ;
|
|
if (angebrocheneStundenAusserhalbDerNacht == 6) {
|
|
preis += 5 + übrigeStundenAusserhalb - 6;
|
|
} else {
|
|
preis += übrigeStundenAusserhalb;
|
|
preis = preis > 15 ? 15 : preis;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (auto.iseAuto()) {
|
|
preis = preis * E_AUTO_RABATT;
|
|
}
|
|
|
|
|
|
return preis;
|
|
}
|
|
|
|
public Auto getAutoFromKennzeichen(HashMap<String, Auto> autos) {
|
|
System.out.println("Bitte gib dein Kennzeichen ein: Format (MA-DL 1234) oder (MA-DL 1234E)");
|
|
String kennzeichen = scanner.nextLine();
|
|
Auto gewünschtesAuto = autos.get(kennzeichen);
|
|
return gewünschtesAuto;
|
|
}
|
|
|
|
public Auto ausfahren(HashMap<String, Auto> autos) {
|
|
|
|
Auto ausfahrendesAuto = getAutoFromKennzeichen(autos);
|
|
System.out.println("Bitte geben Sie ihre Ausfahrtzeit an: Format (dd.MM.yyyy, HH:mm)");
|
|
String ausfahrtZeit = scanner.nextLine();
|
|
ausfahrendesAuto.setAusfahrtZeit(ausfahrtZeit);
|
|
return ausfahrendesAuto;
|
|
|
|
}
|
|
|
|
public Auto bezahlePreisFürKennzeichen(Auto zuBezahlendesAuto) {
|
|
zuBezahlendesAuto.setBezahlt(true);
|
|
return zuBezahlendesAuto;
|
|
}
|
|
}
|
|
|