https://jeong-pro.tistory.com/m/173 개념참고.
Theater 코딩서 이미 좌석정렬했는데, 가격순 정렬도 보고싶다?
comparable(cmopareTo)를 수정할 순 없고, 가격순 comparator 추가!
Comparable : public class xxxx implement Comparable<>{ ....@override...CompareTo() } 이용
참고)CompareToIgnoreCase() : 대소문자 무시
Comparator : implement 이용 안함.
1. comparator type의 object 생성 -(여러개 comparator 생성가능)
2. 주로 익명 Class이용하여 compare()이용하여 initialization
static final Comparator<Seat> object;
static { object = new Comparator<Seat>() // initialization
{ @override........... compare()........... } }
Main Class
Collections.sort(priceSeats, Theatre.PRICE_ORDER); //collections.sort(list, defined comparator)
예제) Theater Class에 가격순정렬 comparator 추가.
package com.timbuchalka;
import java.util.*;
public class Theatre {
private final String theatreName;
private List<Seat> seats = new ArrayList<>();
static final Comparator<Seat> PRICE_ORDER; //comparator type의 object(가격) 생성 V
//가격순 정렬 정의. V
static { //comparator //anonymous clss 이용해서 특정한 정렬 정의(initialization)
PRICE_ORDER = new Comparator<Seat>()
{ // 요거 initialzation 쓰면 자동으로 @override 나옴.
@Override
public int compare(Seat seat1, Seat seat2) { //정렬방법 내가 정함.
if (seat1.getPrice() < seat2.getPrice()) {
return -1; //위치 앞으로
} else if (seat1.getPrice() > seat2.getPrice()) { //위치 뒤로
return 1;
} else {
return 0; //가격동일
}
}
};
}
//constructor 에 가격설정 추가. V
public Theatre(String theatreName, int numRows, int seatsPerRow) {
this.theatreName = theatreName;
int lastRow = 'A' + (numRows -1);
for (char row = 'A'; row <= lastRow; row++) { //row : A~H
for(int seatNum = 1; seatNum <= seatsPerRow; seatNum++) {
double price = 12.00; //기본가격 =12
if((row < 'D') && (seatNum >=4 && seatNum <=9)) { //<D(4~9) 14 price = 14.00;
price= 14.00;
} else if((row > 'F') || (seatNum < 4 || seatNum > 9)) {
price = 7.00; //F<(123,10,11,12)
}
Seat seat = new Seat(row + String.format("%02d", seatNum), price);
seats.add(seat);
}
}
}
//getter
public String getTheatreName() {
return theatreName;
}
public boolean reserveSeat(String seatNumber) {
Seat requestedSeat = new Seat(seatNumber, 0);
int foundSeat = Collections.binarySearch(seats, requestedSeat, null);
if (foundSeat >= 0) {
return seats.get(foundSeat).reserve();
} else {
System.out.println("There is no seat " + seatNumber);
return false;
}
}
// for testing
public Collection<Seat> getSeats() {
return seats;
}
//InnerClass Seat ......Comparable
public class Seat implements Comparable<Seat> {
private final String seatNumber;
private double price;
private boolean reserved = false;
//constructor
public Seat(String seatNumber, double price) {
this.seatNumber = seatNumber;
this.price = price;
}
@Override
public int compareTo(Seat seat) {
return this.seatNumber.compareToIgnoreCase(seat.getSeatNumber());
}
//method
public boolean reserve() {
if(!this.reserved) {
this.reserved = true;
System.out.println("Seat " + seatNumber + " reserved");
return true;
} else {
return false;
}
}
public boolean cancel() {
if(this.reserved) {
this.reserved = false;
System.out.println("Reservation of seat " + seatNumber + " cancelled");
return true;
} else {
return false;
}
}
public String getSeatNumber() {
return seatNumber;
}
//getter Price V
public double getPrice() {
return price;
}
}
}
Main
package com.timbuchalka;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
Theatre theatre = new Theatre("Olympian", 8, 12);
if(theatre.reserveSeat("D12")) {
System.out.println("Please pay for D12");
} else {
System.out.println("Seat already reserved");
}
if(theatre.reserveSeat("D12")) {
System.out.println("Please pay for D12");
} else {
System.out.println("Seat already reserved");
}
if(theatre.reserveSeat("B13")) {
System.out.println("Please pay for B13");
} else {
System.out.println("Seat already reserved");
}
//separate List, same data 가능 // V
List<Theatre.Seat> reverseSeats = new ArrayList<>(theatre.getSeats());
Collections.reverse(reverseSeats); //좌석 역순
printList(reverseSeats);
List<Theatre.Seat> priceSeats = new ArrayList<>(theatre.getSeats());
priceSeats.add(theatre.new Seat("B00", 13.00)); //innerClass에 추가(constructor).
priceSeats.add(theatre.new Seat("A00", 13.00));
Collections.sort(priceSeats, Theatre.PRICE_ORDER); //가격 순 V
printList(priceSeats);
}
public static void printList(List<Theatre.Seat> list) {
for(Theatre.Seat seat : list) {
System.out.print(" " + seat.getSeatNumber() + " $" + seat.getPrice());
}
System.out.println();
System.out.println("======================================================================");
}
}
//Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=51479:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Comparable-and-Comparator-Source-code\out\production\Collections com.timbuchalka.Main
Seat D12 reserved
Please pay for D12
Seat already reserved
There is no seat B13
Seat already reserved
H12 $7.0 H11 $7.0 H10 $7.0 H09 $7.0 H08 $7.0 H07 $7.0 H06 $7.0 H05 $7.0 H04 $7.0 H03 $7.0 H02 $7.0 H01 $7.0 G12 $7.0 G11 $7.0 G10 $7.0 G09 $7.0 G08 $7.0 G07 $7.0 G06 $7.0 G05 $7.0 G04 $7.0 G03 $7.0 G02 $7.0 G01 $7.0 F12 $7.0 F11 $7.0 F10 $7.0 F09 $12.0 F08 $12.0 F07 $12.0 F06 $12.0 F05 $12.0 F04 $12.0 F03 $7.0 F02 $7.0 F01 $7.0 E12 $7.0 E11 $7.0 E10 $7.0 E09 $12.0 E08 $12.0 E07 $12.0 E06 $12.0 E05 $12.0 E04 $12.0 E03 $7.0 E02 $7.0 E01 $7.0 D12 $7.0 D11 $7.0 D10 $7.0 D09 $12.0 D08 $12.0 D07 $12.0 D06 $12.0 D05 $12.0 D04 $12.0 D03 $7.0 D02 $7.0 D01 $7.0 C12 $7.0 C11 $7.0 C10 $7.0 C09 $14.0 C08 $14.0 C07 $14.0 C06 $14.0 C05 $14.0 C04 $14.0 C03 $7.0 C02 $7.0 C01 $7.0 B12 $7.0 B11 $7.0 B10 $7.0 B09 $14.0 B08 $14.0 B07 $14.0 B06 $14.0 B05 $14.0 B04 $14.0 B03 $7.0 B02 $7.0 B01 $7.0 A12 $7.0 A11 $7.0 A10 $7.0 A09 $14.0 A08 $14.0 A07 $14.0 A06 $14.0 A05 $14.0 A04 $14.0 A03 $7.0 A02 $7.0 A01 $7.0
======================================================================
A01 $7.0 A02 $7.0 A03 $7.0 A10 $7.0 A11 $7.0 A12 $7.0 B01 $7.0 B02 $7.0 B03 $7.0 B10 $7.0 B11 $7.0 B12 $7.0 C01 $7.0 C02 $7.0 C03 $7.0 C10 $7.0 C11 $7.0 C12 $7.0 D01 $7.0 D02 $7.0 D03 $7.0 D10 $7.0 D11 $7.0 D12 $7.0 E01 $7.0 E02 $7.0 E03 $7.0 E10 $7.0 E11 $7.0 E12 $7.0 F01 $7.0 F02 $7.0 F03 $7.0 F10 $7.0 F11 $7.0 F12 $7.0 G01 $7.0 G02 $7.0 G03 $7.0 G04 $7.0 G05 $7.0 G06 $7.0 G07 $7.0 G08 $7.0 G09 $7.0 G10 $7.0 G11 $7.0 G12 $7.0 H01 $7.0 H02 $7.0 H03 $7.0 H04 $7.0 H05 $7.0 H06 $7.0 H07 $7.0 H08 $7.0 H09 $7.0 H10 $7.0 H11 $7.0 H12 $7.0 D04 $12.0 D05 $12.0 D06 $12.0 D07 $12.0 D08 $12.0 D09 $12.0 E04 $12.0 E05 $12.0 E06 $12.0 E07 $12.0 E08 $12.0 E09 $12.0 F04 $12.0 F05 $12.0 F06 $12.0 F07 $12.0 F08 $12.0 F09 $12.0 B00 $13.0 A00 $13.0 A04 $14.0 A05 $14.0 A06 $14.0 A07 $14.0 A08 $14.0 A09 $14.0 B04 $14.0 B05 $14.0 B06 $14.0 B07 $14.0 B08 $14.0 B09 $14.0 C04 $14.0 C05 $14.0 C06 $14.0 C07 $14.0 C08 $14.0 C09 $14.0
======================================================================
Process finished with exit code 0
'JavaCode(review)' 카테고리의 다른 글
Map Interface - Collection - Adventure Game Challenge. (0) | 2020.07.06 |
---|---|
Map Interface - collection (0) | 2020.07.05 |
Collections.binarySearch(); / Theater코딩에 binarySearch 이용 (0) | 2020.07.03 |
Collection 개념코딩 - theater coding / string.format이용. (0) | 2020.07.02 |
자바 용어 사이트 oracle java tutorial (0) | 2020.07.02 |