추상 클래스 Vehicle 선언. 대중 교통(Bus, Taxi)가 가지는 공통적인 field
및 getter, setter
를 포함.
package domain;
public abstract class Vehicle {
// field
private int number;
private int fuel;
private int speed;
private Boolean status;
private int maxPassenger;
private int presentPassenger;
// constructor
public Vehicle(){
this.fuel = 100;
this.speed = 0;
this.presentPassenger = 0;
}
// getter and setter
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getFuel() {
return fuel;
}
public void setFuel(int fuel) {
this.fuel = fuel;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public int getMaxPassenger() {
return maxPassenger;
}
public void setMaxPassenger(int maxPassenger) {
this.maxPassenger = maxPassenger;
}
public int getPresentPassenger() {
return presentPassenger;
}
public void setPresentPassenger(int presentPassenger) {
this.presentPassenger = presentPassenger;
}
}
fare
선언.BusAgePolicy
와 연동하여 연령에 따른 버스 요금이 산정되도록 설계. package domain;
public class Bus extends Vehicle{
// field
private int fare;
// constructor
public Bus(int number){
super();
this.setNumber(number);
this.setFare(1000);
this.setStatus(true);
this.setMaxPassenger(30);
}
public int getFare() {
return fare;
}
public void setFare(int fare) {
this.fare = fare;
}
}
Stirng destination
, int distanceToDestination
, int basicFare
, int basicDistance
, int distanceFare
, int AccumulatedFare
선언. package domain;
public class Taxi extends Vehicle{
// field
private String destination;
private int distanceToDestination;
private int basicFare;
private int basicDistance;
private int distanceFare;
private int AccumulatedFare;
// constructor
public Taxi(int number){
super();
this.setMaxPassenger(4);
this.setBasicFare(3000);
this.setDistanceFare(1000);
this.setBasicDistance(1);
this.setNumber(number);
this.setAccumulatedFare(0);
this.setStatus(true);
}
// getter and setter
public int getAccumulatedFare() {
return AccumulatedFare;
}
public void setAccumulatedFare(int accumulatedFare) {
AccumulatedFare = accumulatedFare;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public int getDistanceToDestination() {
return distanceToDestination;
}
public void setDistanceToDestination(int distanceToDestination) {
this.distanceToDestination = distanceToDestination;
}
public int getBasicFare() {
return basicFare;
}
public void setBasicFare(int basicFare) {
this.basicFare = basicFare;
}
public int getBasicDistance() {
return basicDistance;
}
public void setBasicDistance(int basicDistance) {
this.basicDistance = basicDistance;
}
public int getDistanceFare() {
return distanceFare;
}
public void setDistanceFare(int distanceFare) {
this.distanceFare = distanceFare;
}
}
대중 교통이 공통적으로 필요한 기능을 default
로 정의. 요금 산정 method의 경우 각 대중 교통마다 요금을 산정하는 방식이 상이하므로, 선언만 하였음.
package service;
import domain.Vehicle;
public interface RunService {
default void runStart(Vehicle vehicle){
vehicle.setStatus(true);
}
default void changeSpeed(Vehicle vehicle, int speed){
if(vehicle.getFuel() < 10){
System.out.println("주유 필요");
return;
}
if(speed < 0){
speed = 0;
}
vehicle.setSpeed(speed);
}
default void changeStatus(Vehicle vehicle, boolean status){
if(status){
vehicle.setStatus(status);
return;
}
vehicle.setStatus(status);
vehicle.setPresentPassenger(0);
}
default void passengerRide(Vehicle vehicle, int num){
if (vehicle.getPresentPassenger() + num > vehicle.getMaxPassenger()){
System.out.println("최대 승객 수를 초과하여 탑승할 수 없습니다.");
return;
}
vehicle.setPresentPassenger(vehicle.getPresentPassenger()+num);
}
default void changeFuel(Vehicle vehicle, int num){
int tmp = vehicle.getFuel() + num;
if (tmp < 0){
tmp = 0;
}
vehicle.setFuel(tmp);
if (tmp < 10){
vehicle.setStatus(false); // 연료가 없으면 상태 변경(운행 종료)
System.out.println("주유 필요");
}
}
String passengerInfo(Vehicle vehicle);
}
passengerInfo(Vehicle vehicle)
선언int fareCalc(Bus bus, int[] arr)
선언package service;
import domain.Bus;
import domain.BusAgePolicy;
import domain.Vehicle;
public class BusRunService implements RunService{
@Override
public String passengerInfo(Vehicle vehicle) {
double tmp = (double) vehicle.getPresentPassenger() / vehicle.getMaxPassenger();
if (tmp > 0.8){
return "혼잡";
}
if (tmp < 0.2){
return "여유";
}
return "보통";
}
public int fareCalc(Bus bus, int[] arr){
int result = 0;
for (int i = 0; i < arr.length; i++) {
BusAgePolicy busAgePolicy = BusAgePolicy.of(arr[i]);
result = result + busAgePolicy.calculateFareAppliedAgePolicy(bus.getFare());
}
return result;
}
}
String passengerInfo(Vehicle vehicle)
선언int fareCalc(Taxi taxi)
선언빈 차
로 바꾸고, 매출 총액을 계산하는 method void payment(Taxi taxi, int fare)
선언package service;
import domain.Taxi;
import domain.Vehicle;
public class TaxiRunService implements RunService{
@Override
public String passengerInfo(Vehicle vehicle) {
if (vehicle.getPresentPassenger() > 0){
return "LED Off";
}
return "빈 차";
}
public int fareCalc(Taxi taxi){
int result = taxi.getBasicFare();
// 택시 추가 요금이 버림인가 올림인가 모르겠음... 일단 버림으로 계산
result = result + (taxi.getDistanceToDestination() - taxi.getBasicDistance()) * taxi.getDistanceFare();
return result;
}
public void payment(Taxi taxi, int fare){
taxi.setAccumulatedFare(taxi.getAccumulatedFare() + fare);
taxi.setPresentPassenger(0);
}
}