[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerScoreApp.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker;
18
19 import java.text.SimpleDateFormat;
20
21 import org.hedgecode.snooker.api.APIException;
22 import org.hedgecode.snooker.api.Event;
23 import org.hedgecode.snooker.api.Events;
24 import org.hedgecode.snooker.api.Season;
25
26 /**
27  * Snooker Score Main Application.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public final class SnookerScoreApp {
32
33     private static final String EMPTY = "";
34     private static final String DELIMITER =
35             "********************************************************************************";
36     private static final String[] WELCOME = {
37             "                    Welcome to Hedgecode Snooker Score API!                     ",
38             "                -----------------------------------------------                 ",
39             "         It is an API library for portal snooker.org, which contains            ",
40             "      the results of snooker competitions and other snooker information.        ",
41             "    This library provides a set of entity objects that can be used in client    ",
42             "    applications (to inform about the results of snooker), developed in Java.   "
43     };
44     private static final String CURRENT_EVENTS = "  Current Snooker Events:  ";
45     private static final String UPCOMING_EVENTS = "  Upcoming Snooker Events:  ";
46     private static final String INDENT = "     ";
47
48     private static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
49
50     private static final int UPCOMING_COUNT = 5;
51
52     public static void main(String[] args) throws APIException
53     {
54         System.out.println(EMPTY);
55         System.out.println(DELIMITER);
56         for (String line : WELCOME) {
57             System.out.println(line);
58         }
59
60         Events events = Snooker.uncachedAPI().getSeasonEvents(
61                 Season.CURRENT_SEASON
62         );
63         events.sortByDate();
64
65         Events currentEvents = events.current();
66         if (!currentEvents.isEmpty()) {
67             System.out.println(DELIMITER);
68             System.out.println(CURRENT_EVENTS);
69         }
70         for (Event currentEvent : currentEvents) {
71             printEvent(currentEvent);
72         }
73
74         System.out.println(DELIMITER);
75         System.out.println(UPCOMING_EVENTS);
76
77         int count = 0;
78         Events upcomingEvents = events.upcoming();
79         for (Event upcomingEvent : upcomingEvents) {
80             printEvent(upcomingEvent);
81             ++count;
82             if (count >= UPCOMING_COUNT) break;
83         }
84         System.out.println(DELIMITER);
85         System.out.println(EMPTY);
86     }
87
88     private static void printEvent(Event event) {
89         System.out.println(
90                 INDENT  + event.name()
91                         + " [" + DATE_FORMAT.format(event.startDate())
92                         + " - " + DATE_FORMAT.format(event.endDate()) + "]"
93                         + " (" + event.country() + ", " + event.city() + ")"
94         );
95     }
96
97 }