CGVExample.java 4.24 KB
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

class CGVMovieInfo { //CGV 영화 정보를 담는 class
    private String title; //영화 제목
    private int rank; //CGV 내 예매율 순위
    private float score; //예매율
    private String GoldenEgg; //골든에그 지수
    private String movieCode; //CGV 고유 영화코드 - 예매 사이트 연결 시 사용

    public CGVMovieInfo(String title, int rank, float score, String GoldenEgg, String movieCode) {
        this.title = title;
        this.rank = rank;
        this.score = score;
        this.GoldenEgg = GoldenEgg;
        this.movieCode = movieCode;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    public String getGoldenEgg() {
        return GoldenEgg;
    }

    public void setGoldenEgg(String goldenEgg) {
        GoldenEgg = goldenEgg;
    }

    public String getMovieCode() {
        return movieCode;
    }

    public void setMovieCode(String movieCode) {
        this.movieCode = movieCode;
    }

    public String getLink() {
        return String.format("https://www.cgv.co.kr/ticket/?MOVIE_CD=%s&MOVIE_CD_GROUP=%s", this.movieCode, this.movieCode);
    }

    public void printMovieInfo(){
        System.out.println("-------------------------------------------------------");
        System.out.println(this.rank + " : " + this.title);
        System.out.println("예매율 : " + this.score + "%");
        System.out.println("골든에그지수 : " + this.GoldenEgg);
        System.out.println("영화코드 : " + this.movieCode);
        System.out.println("-------------------------------------------------------");
    }

}

public class CGVExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String url = "https://www.cgv.co.kr/movies/?lt=1&ft=1"; //끝의 쿼리 0은 개봉 전 영화도 포함하는 것.
        Document doc = null;
        ArrayList<CGVMovieInfo> Movies = new ArrayList<>();
        try {
            doc = Jsoup.connect(url).get();
            //Top 19 까지
            Elements elements1 = doc.select("div.sect-movie-chart");
            Iterator<Element> rank = elements1.select("strong.rank").iterator();
            Iterator<Element> title = elements1.select("strong.title").iterator();
            Iterator<Element> score = elements1.select("strong.percent").iterator();
            Iterator<Element> GoldenEgg = elements1.select("span.percent").iterator();
            Iterator<Element> link = elements1.select("a.link-reservation").iterator();

            //Top 19 이후
            //Elements elements2 = doc.select("div.sect-movie-chart");

            while(title.hasNext()){
                String newTitle = title.next().text();
                int newRank = Integer.parseInt(rank.next().text().replace("No.",""));
                float newScore = Float.parseFloat(score.next().text().replace("예매율", "").replace("%", ""));
                String newCode = link.next().attr("href").replaceAll("[^0-9]", "").substring(0,8);

                CGVMovieInfo newMovie = new CGVMovieInfo(newTitle, newRank, newScore, GoldenEgg.next().text(), newCode);
                Movies.add(newMovie);
            }
        }catch(IOException e){
            e.printStackTrace();
        }

        for (CGVMovieInfo elem : Movies) {
            System.out.println(elem.getRank() + " : " + elem.getTitle());
        }

        System.out.print("관람하고 싶은 영화의 순위를 입력하세요 : ");
        int inputRank = scanner.nextInt();
        try{
            Desktop.getDesktop().browse(new URI(Movies.get(inputRank - 1).getLink()));
        }
        catch(IndexOutOfBoundsException | URISyntaxException | IOException e){
            System.out.println(e.getClass());
        }
    }
}