/ JAVA

객체지향 프로그래밍 복습 (4) Car Rental 클래스에 Customer 클래스 추가하기

[JAVA] 객체지향 프로그래밍 복습 (4) Car Rental 클래스에 Customer 클래스 추가하기

자바

자바(Java)는 C언어에 객체 지향적 기능을 추가하여 만든 C++과는 달리, 처음부터 객체 지향 언어로 개발된 프로그래밍 언어입니다. 자바는 자바 가상 머신(JVM, Java Virtual Machine)을 사용하여, 운영체제와는 독립적으로 동작할 수 있습니다. 따라서 자바는 어느 운영체제에서나 같은 형태로 실행될 수 있습니다. 바로 이러한 점이 수많은 개발자로 하여금 자바를 사용하게 하는 원동력이 되고 있습니다. 현재 자바는 전 세계에서 가장 많이 사용하는 프로그래밍 언어 중 하나입니다.

Customer 클래스

앞서 구현한 자동차 렌탈 클래스에 고객 정보를 다루는 Customer 클래스를 구현하여 적용합니다. 고객 정보에는 이름, ID, 운전면허 코드, 이용 금액, 고객 등급이 있습니다. 자동차와 마찬가지로 ID는 중복 검사를 한 랜덤 값이 적용되고, 지불한 금액에 따라 고객 등급이 Silver, Gold, Diamond로 재설정 됩니다.

Car.java

package LAB04;

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 LAB04;

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() {
		if (this.creditPoints > 500000)
			this.cStatus = CustomerStatusType.Diamond;
		else if (this.creditPoints > 100000)
			this.cStatus = CustomerStatusType.Gold;
		else
			this.cStatus = CustomerStatusType.Silver;
			
	}
	
	//고객 이용 금액 반환 함수
	public int getPoints() {
		return this.creditPoints;
	}
	
	//고객 이용 금액 추가 및 등급 재설정
	public void addPoints(int rentalFee) {
		this.creditPoints += rentalFee;
		this.promote();
	}
	
	//고객 이용 금액 감소 및 등급 재설정
	public void reducePoints(int points) {
		this.creditPoints -= points;
		this.promote();
	}
	
	//고객 등급 반환 함수
	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();
	}
	
	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");
		
		//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[] carArray = new Car[5];
		carArray[0] = new Car(date1, 735);
		carArray[1] = new Car(date2, 1075);
		carArray[2] = new Car(date3, 4825);
		
		//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;		//중복 시 재설정.
				}
			}		
		}
		
		//CarID 중복 검사.
				for(int i = 0; i < 3; i++) {
					for(int j = i + 1; j < 3; j++) {
						if (carArray[i].carID == carArray[j].carID) {
							carArray[i].carID = random.nextInt(9000) + 1000;		//중복 시 재설정.
						}
					}
				}
		
		System.out.println("== Printing Customer Information ==");
		for(int i = 0; i < 2; i++)
			customerList[i].printinfo();	
		
		System.out.println("== Printing Car Information ==");
		for(int i = 0; i < 3; i++)
			carArray[i].printinfo();	
		
		System.out.println("== Adding 300,000 Points to Customer #1 ==");
		customerList[0].addPoints(300000);
		for(int i = 0; i < 2; i++)
			customerList[i].printinfo();	
		
	}
}

결과