Etwas überarbeitet
[CODE lang="java" highlight="7-20"]import java.util.Scanner;
public class Kino {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Movie movie = new Movie();
System.out.print("Alter eingeben: (FSK: " + movie.fsk + ") > ");
movie.age = input.nextInt();
System.out.print("Reihe eingeben (1-12): > ");
movie.row = input.nextInt();
if (movie.age < movie.fsk)
System.out.println("zu jung...");
else {
try {
System.out.println(String.format("Preis: %.2f", getPrice(movie)));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
private static double getPrice(Movie kino) throws IllegalArgumentException {
if (kino.row < 1 || kino.row > 12)
throw new IllegalArgumentException("Ungueltige Sitzreihe '" + kino.row + "'");
double basicPrice = getAgeDiscount(kino.basicPrice, kino.age);
if (kino.row < 5)
return basicPrice * .7;
if (kino.row < 9)
return basicPrice;
if (kino.row < 12)
return basicPrice * 1.3;
return basicPrice * 1.5;
}
private static double getAgeDiscount(double basicPrice, int age) {
if (age < 6)
return basicPrice * .5;
if (age <= 12)
return basicPrice * .75;
if (age <= 16)
return basicPrice * .9;
return basicPrice;
}
}[/CODE]
[CODE lang="java" title="Containerklasse"]public class Movie {
public int age;
public int fsk = 2;
public double basicPrice = 6.30;
public int row;
}[/CODE]