객체지향 프로그래밍 복습 (5) Car Rental 클래스 마무리
[JAVA] 객체지향 프로그래밍 복습 (5) Car Rental 클래스 마무리
자바
자바(Java)는 C언어에 객체 지향적 기능을 추가하여 만든 C++과는 달리, 처음부터 객체 지향 언어로 개발된 프로그래밍 언어입니다. 자바는 자바 가상 머신(JVM, Java Virtual Machine)을 사용하여, 운영체제와는 독립적으로 동작할 수 있습니다. 따라서 자바는 어느 운영체제에서나 같은 형태로 실행될 수 있습니다. 바로 이러한 점이 수많은 개발자로 하여금 자바를 사용하게 하는 원동력이 되고 있습니다. 현재 자바는 전 세계에서 가장 많이 사용하는 프로그래밍 언어 중 하나입니다.
Rental 클래스
앞서 구현한 Car와 Customer 클래스를 다루는 Rental 클래스를 구현합니다. Rental 클래스의 멤버는 고객, 자동차 클래스, 렌탈 id, 체크아웃, 체크인 날짜, 요금, 렌트한 자동차 수 가 있습니다. 렌탈은 생성자를 통해 작동하며 기능으로는 자동차 반환이 있습니다.
Rental.java
package LAB05;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import LAB03.Customer.CustomerStatusType;
public class Rental {
int rentalID; //rentalID
Customer customer; //렌탈 클래스에 들어갈 고객
Car car; //렌탈 클래스에 들어갈 자동차
Date dateOut, dateIn; //자동차 체크아웃, 체크인 날짜
int fee; //요금
static int totalRentals; //렌트한 자동차 대 수
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Rental(Customer cust, Car car, Date out, int fee)
{
Random random = new Random();
this.rentalID = random.nextInt(900000) + 100000;
this.customer = cust;
this.car = car;
this.dateOut = out;
this.fee = fee;
int points = 0;
Scanner sc = new Scanner(System.in);
if(cust.creditPoints > 0)
{
System.out.println("현재 포인트: " + cust.creditPoints);
System.out.println("사용할 포인트를 입력해주세요: ");
points = sc.nextInt();
cust.reducePoints(points);
System.out.println("차감 후 포인트: " + cust.creditPoints);
}
totalRentals++;
}
void returnCar(Date in, int mileage)
{
this.dateIn = in;
this.car.mileage = mileage;
if(this.customer.cStatus == CustomerStatusType.Gold)
{
this.customer.addPoints(this.fee/10);
}
else if(this.customer.cStatus == CustomerStatusType.Diamond)
{
this.customer.addPoints(this.fee/5);
}
totalRentals--;
}
Customer getCustomer()
{
return this.customer;
}
int getFee()
{
return this.fee;
}
void printInfo()
{
System.out.println("Rental ID: " + this.rentalID +
"\nDate Out: " + dateFormat.format(this.dateOut) +
"\nFee: " + this.fee +
"\nTotal Rentals: " + totalRentals);
System.out.println();
}
public static void main(String[] args) throws ParseException {
Random random = new Random();
//고객 2명 생성
Customer[] customerList = new Customer[2];
customerList[0] = new Customer("Harry", "LDNGO48294HJDF");
customerList[1] = new Customer("Hermione", "LHFGO48754HJDO");
//CustomerID 중복 검사.
for(int i = 0; i < 2; i++) {
for(int j = i + 1; j < 2; j++) {
if (customerList[i].customerID == customerList[j].customerID) {
customerList[i].customerID = random.nextInt(90000) + 10000; //중복 시 재설정.
}
}
}
System.out.println("== Printing Customer Information ==");
for(int i = 0; i < 2; i++)
customerList[i].printinfo();
//Date 포맷 변경
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse("2021-03-21");
Date date2 = dateFormat.parse("2021-03-22");
Date date3 = dateFormat.parse("2021-03-23");
//자동차 3대 생성
Car[] carList = new Car[3];
carList[0] = new Car(date1, 735);
carList[1] = new Car(date2, 1075);
carList[2] = new Car(date3, 4825);
//CarID 중복 검사.
for(int i = 0; i < 3; i++) {
for(int j = i + 1; j < 3; j++) {
if (carList[i].carID == carList[j].carID) {
carList[i].carID = random.nextInt(9000) + 1000; //중복 시 재설정.
}
}
}
System.out.println("== Printing Car Information ==");
for(int i = 0; i < 3; i++)
carList[i].printinfo();
//Rental 2개 생성
Rental[] rentalList = new Rental[2];
rentalList[0] = new Rental(customerList[0], carList[0], date1, 500000);
rentalList[1] = new Rental(customerList[1], carList[1], date2, 2000000);
//RenatlID 중복 검사.
for(int i = 0; i < 2; i++) {
for(int j = i + 1; j < 2; j++) {
if (rentalList[i].rentalID == rentalList[j].rentalID) {
rentalList[i].rentalID = random.nextInt(900000) + 100000; //중복 시 재설정.
}
}
}
System.out.println("== Printing Rental Information ==");
for(int i = 0; i < 2; i++)
rentalList[i].printInfo();
//returnCar로 자동차 반납 및 반납 일, 거리 재설정
rentalList[0].returnCar(dateFormat.parse("2021-04-09"), 1930);
rentalList[1].returnCar(dateFormat.parse("2021-04-09"), 3895);
//promote로 등급 변경
rentalList[0].customer.promote(rentalList[0].fee);
rentalList[1].customer.promote(rentalList[1].fee);
//등급 변경 확인
System.out.println("== Printing Customer Information ==");
for(int i = 0; i < 2; i++)
customerList[i].printinfo();
//1번 고객 재이용 및 반납
rentalList[0] = new Rental(customerList[0], carList[2], dateFormat.parse("2021-04-10"), 5000000);
rentalList[0].returnCar(dateFormat.parse("2021-04-09"), 6937);
//적립 확인
System.out.println("== Printing Customer Information ==");
customerList[0].printinfo();
}
}
Car.java
package LAB05;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Car {
//자동차 상태 enum
public enum StatusType {
available, checkedOut, inService, discarded, sold
}
int carID; //자동차 ID
StatusType status; //자동차 상태
Date datePurchased; //자동차 렌트 일자
int mileage; //자동차 주행 거리
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//Car 생성 함수
Car(Date d, int m) {
Random random = new Random();
this.carID = random.nextInt(9000) + 1000;
this.status = StatusType.available;
this.datePurchased = d;
this.mileage = m;
}
//마일리지 설정 함수
public void setMileage(int x) {
this.mileage = x;
}
//마일리지 추가 함수
public void addMileage(int x) {
this.mileage += x;
}
//상태 설정 함수
public void setStatus(StatusType s) {
this.status = s;
}
//정보 출력 함수
public void printinfo() {
System.out.println("CarID: " + this.carID + "\nCar Status: " + this.status +
"\nPurchased Date: " + dateFormat.format(this.datePurchased) + "\nCar Mileage: " + this.mileage + " km");
System.out.println();
}
}
Customer. java
package LAB05;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Customer {
//고객 등급 enum
public enum CustomerStatusType {
Silver, Gold, Diamond
}
int customerID; //고객 ID
String name; //고객 이름
String driverLicense; //고객 운전면허 코드
int creditPoints; //이용 금액
CustomerStatusType cStatus; //고객 등급
//Customer 생성 함수
Customer(String n, String d) {
Random random = new Random();
this.customerID = random.nextInt(90000) + 10000;
this.creditPoints = 0;
this.cStatus = CustomerStatusType.Silver;
this.name = n;
this.driverLicense = d;
}
//고객 등급 재설정 함수
public void promote(int fee) {
if (fee > 500000)
this.cStatus = CustomerStatusType.Diamond;
else if (fee > 100000)
this.cStatus = CustomerStatusType.Gold;
else
this.cStatus = CustomerStatusType.Silver;
}
//고객 이용 금액 반환 함수
public int getPoints() {
return this.creditPoints;
}
//고객 이용 금액 추가
public void addPoints(int rentalFee) {
this.creditPoints += rentalFee;
}
//고객 이용 금액 감소
public void reducePoints(int points) {
this.creditPoints -= points;
}
//고객 등급 반환 함수
public CustomerStatusType getCustomerStatus() {
return this.cStatus;
}
//고객 정보 출력 함수
public void printinfo() {
System.out.println("Customer ID: " + this.customerID + "\nName: " + this.name +
"\nDriver License: " + this.driverLicense + "\nCustomer Status: " + this.cStatus +
"\nCredit Points: " + this.creditPoints);
System.out.println();
}
}