基本情報技術者試験 平成14年度・春期・午後 問12 ソースプログラム
public class MileageTest implements MileageServices {
public static void main(String args[]) {
NormalPassenger taro = new NormalPassenger("Taro", 0);
GoldPassenger mark = new GoldPassenger("Mark", 100000);
NormalPassenger june = new NormalPassenger("June", 0);
GoldPassenger jiro = new GoldPassenger("Jiro", 50000);
new MileageTest(taro, NARITA, PARIS,
DOMESTIC_ROUND_TRIP);
new MileageTest(mark, LOSANGELES, PARIS,
ASIA_PACIFIC_ROUND_TRIP);
new MileageTest(june, PARIS, KANSAI, US_ROUND_TRIP);
new MileageTest(jiro, KANSAI, NARITA, 0);
new MileageTest(taro, PARIS, NARITA, DOMESTIC_ROUND_TRIP);
}
public MileageTest(Passenger passenger, int from, int to,
int awardTrip) {
passenger.addMileage(from, to);
System.out.println("\n" + passenger.getName() +
"'s mileage: " + passenger.getMileage());
if (awardTrip != 0)
try {
passenger.awardTravel(awardTrip);
System.out.println("You get an award trip.\n" +
"Your mileage is now " +
passenger.getMileage() + ".");
} catch (NotEnoughMileageException e) {
System.out.println(e);
}
}
}
interface MileageServices {
final static int NARITA = 0;
final static int KANSAI = 1;
final static int LOSANGELES = 2;
final static int PARIS = 3;
final static int[][] MILEAGE = {{ 0, 300, 5400, 6200},
{ 300, 0, 5700, 6100},
{5400, 5700, 0, 4000},
{6200, 6100, 4000, 0}
};
final static int DOMESTIC_ROUND_TRIP = 15000;
final static int ASIA_PACIFIC_ROUND_TRIP = 20000;
final static int US_ROUND_TRIP = 40000;
final static double NORMAL = 1.00;
final static double GOLD = 1.25;
}
abstract class Passenger implements MileageServices {
int totalMileage;
String name;
Passenger(String name, int totalMileage) {
this.name = name;
this.totalMileage = totalMileage;
}
public abstract void addMileage(int from, int to);
public void awardTravel(int award) throws
NotEnoughMileageException {
if (award > totalMileage)
throw new NotEnoughMileageException(name);
else
totalMileage -= award;
}
public int getMileage() {
return totalMileage;
}
public String getName() {
return name;
}
}
class NormalPassenger extends Passenger {
NormalPassenger(String name, int totalMileage) {
super(name, totalMileage);
}
public void addMileage(int from, int to) {
totalMileage += (int)(MILEAGE[from][to] * NORMAL);
}
}
class GoldPassenger extends Passenger {
GoldPassenger(String name, int totalMileage) {
super(name, totalMileage);
}
public void addMileage(int from, int to) {
totalMileage += (int)(MILEAGE[from][to] * GOLD);
}
}
class NotEnoughMileageException extends Exception {
String name;
public NotEnoughMileageException(String name) {
this.name = name;
}
public String toString() {
return "Sorry, your mileage isn't enough for " +
"your award trip, " + name + ".";
}
}