From 17d5c6607692a49474d4c05048405e9f19507e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Bab=C4=8D=C3=A1k?= <boris.babcak@kosickaakademia.sk> Date: Thu, 8 Feb 2024 12:12:33 +0100 Subject: [PATCH] added class database, unit --- .idea/vcs.xml | 6 ++ CountryJSON | 1 + pom.xml | 7 ++ .../java/sk/kasv/babcak/worldx/Country.java | 67 +++++++++++++++++++ .../java/sk/kasv/babcak/worldx/DataBase.java | 63 +++++++++++++++++ src/main/java/sk/kasv/babcak/worldx/Util.java | 12 ++++ .../java/sk/kasv/babcak/worldx/WorldApp.java | 51 ++------------ .../sk/kasv/babcak/worldx/WriterJSON.java | 40 +++++++++++ 8 files changed, 202 insertions(+), 45 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 CountryJSON create mode 100644 src/main/java/sk/kasv/babcak/worldx/Country.java create mode 100644 src/main/java/sk/kasv/babcak/worldx/DataBase.java create mode 100644 src/main/java/sk/kasv/babcak/worldx/Util.java create mode 100644 src/main/java/sk/kasv/babcak/worldx/WriterJSON.java diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/CountryJSON b/CountryJSON new file mode 100644 index 0000000..034779e --- /dev/null +++ b/CountryJSON @@ -0,0 +1 @@ +{"date":"2024-02-08","code3":"TUR","code2":"TR","name":"Turkey","capitalCity":"Ankara","citites":["Istanbul","Ankara","Izmir","Adana","Bursa","Gaziantep","Konya","Mersin (Içel)","Antalya","Diyarbakir","Kayseri","Eskisehir","Sanliurfa","Samsun","Malatya","Gebze","Denizli","Sivas","Erzurum","Tarsus","Kahramanmaras","Elâzig","Van","Sultanbeyli","Izmit (Kocaeli)","Manisa","Batman","Balikesir","Sakarya (Adapazari)","Iskenderun","Osmaniye","Çorum","Kütahya","Hatay (Antakya)","Kirikkale","Adiyaman","Trabzon","Ordu","Aydin","Usak","Edirne","Çorlu","Isparta","Karabük","Kilis","Alanya","Kiziltepe","Zonguldak","Siirt","Viransehir","Tekirdag","Karaman","Afyon","Aksaray","Ceyhan","Erzincan","Bismil","Nazilli","Tokat","Kars","Inegöl","Bandirma"]} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 06fb2d7..5c7b564 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,13 @@ <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> + <dependency> + <groupId>org.json</groupId> + <artifactId>json</artifactId> + <version>20240205</version> + </dependency> + + </dependencies> </project> \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/Country.java b/src/main/java/sk/kasv/babcak/worldx/Country.java new file mode 100644 index 0000000..5b86403 --- /dev/null +++ b/src/main/java/sk/kasv/babcak/worldx/Country.java @@ -0,0 +1,67 @@ +package sk.kasv.babcak.worldx; + +import java.util.ArrayList; +import java.util.List; + +public class Country { + private String name; + private String code2; + private String code3; + private String capital; + List<String> list = new ArrayList<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCode2() { + return code2; + } + + public void setCode2(String code2) { + this.code2 = code2; + } + + public String getCode3() { + return code3; + } + + public void setCode3(String code3) { + this.code3 = code3; + } + + public String getCapital() { + return capital; + } + + public void setCapital(String capital) { + this.capital = capital; + } + + public List<String> getList() { + return list; + } + + public void setList(List<String> list) { + this.list = list; + } + + /** + * @author Boris Babčák + * @date 7.2.2024 + * @description Prints all data from city + */ + public void printCity(){ + System.out.println("<----------------------------------->"); + System.out.println("Country: "+name+" | "+"Code 2: "+code2+" | "+"Code 3: "+code3+" | "+"Capital: "+capital); + System.out.println("List of cities: "+list.size()); + for(String text:list){ + System.out.println("> " + text); + } + System.out.println("<----------------------------------->"); + } +} \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/DataBase.java b/src/main/java/sk/kasv/babcak/worldx/DataBase.java new file mode 100644 index 0000000..df9f7da --- /dev/null +++ b/src/main/java/sk/kasv/babcak/worldx/DataBase.java @@ -0,0 +1,63 @@ +package sk.kasv.babcak.worldx; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + +public class DataBase { + private static final String URL = "jdbc:mysql://localhost:3306/world_x"; + private static final String USER = "root"; + private static final String PASSWORD = ""; + private static final String SELECT_CITIES = "SELECT Country.Name, City.name, Country.Code2" + + " FROM City INNER JOIN country ON City.CountryCode = Country.Code" + + " WHERE Country.Code LIKE ?"; + private static final String SELECT_CAPITAL = "SELECT City.name, Country.Code" + + " FROM City INNER JOIN country ON City.CountryCode = Country.Code" + + " WHERE Country.Code LIKE ? AND country.capital = city.id"; + + public Country getCountry(String code3){ + Connection conn = null; + Country country = new Country(); + country.setCode3(code3); + try { + conn = DriverManager.getConnection(URL, USER, PASSWORD); + printCities(conn, code3, country); + printCapital(conn,code3, country); + } catch (SQLException e) { + System.out.println(e); + } + return country; + } + + private static void printCities(Connection conn, String code, Country country) throws SQLException { + List<String> cities = new ArrayList<>(); + + PreparedStatement ps = conn.prepareStatement(SELECT_CITIES); + ps.setString(1, code); + ResultSet rs = ps.executeQuery(); + String countryName = null; + String code2 = null; + + while (rs.next()) { + countryName = rs.getString("Country.Name"); + String city = rs.getString("City.Name"); + code2 = rs.getString("Country.Code2"); + + cities.add(city); + } + country.setList(cities); + country.setCode2(code2); + country.setName(countryName); + } + + private static void printCapital(Connection conn, String code3, Country country) throws SQLException { + PreparedStatement ps = conn.prepareStatement(SELECT_CAPITAL); + ps.setString(1,code3); + ResultSet rs = ps.executeQuery(); + String city=null; + if(rs.next()){ + city = rs.getString("city.name"); + } + country.setCapital(city); + } +} \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/Util.java b/src/main/java/sk/kasv/babcak/worldx/Util.java new file mode 100644 index 0000000..a8ed097 --- /dev/null +++ b/src/main/java/sk/kasv/babcak/worldx/Util.java @@ -0,0 +1,12 @@ +package sk.kasv.babcak.worldx; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Util { + public static String getDate(){ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String date = sdf.format(new Date()); + return date; + } +} diff --git a/src/main/java/sk/kasv/babcak/worldx/WorldApp.java b/src/main/java/sk/kasv/babcak/worldx/WorldApp.java index 41b15b0..b49cf92 100644 --- a/src/main/java/sk/kasv/babcak/worldx/WorldApp.java +++ b/src/main/java/sk/kasv/babcak/worldx/WorldApp.java @@ -1,50 +1,11 @@ package sk.kasv.babcak.worldx; -import java.sql.*; public class WorldApp { - private static final String URL="jdbc:mysql://localhost:3306/world_x"; - private static final String USER="root"; - private static final String PASSWORD=""; - private static final String SELECT_ALL_SLOVAKIA = "SELECT * FROM city WHERE CountryCode = 'SVK'"; - private static final String SELECT_CITIES = "SELECT Country.Name, City.name"+ - " FROM City INNER JOIN country ON City.CountryCode = Country.Code"+ - " WHERE CountryCode LIKE ?"; - public static void main(String[] args) { - sk.kasv.babcak.worldx.WorldApp app = new sk.kasv.babcak.worldx.WorldApp(); //methods are not static, so we call them through the class object - Connection conn = null; - try { - conn = DriverManager.getConnection(URL, USER, PASSWORD); - app.printAllSlovakia(conn); - System.out.println("<--------------------->"); - app.printCities(conn,"SVK"); - - conn.close(); //connection has to be closed, so no one can access the DB ! - }catch(SQLException e){ - System.out.println(e); - } - } - - private void printCities(Connection conn, String country_code) throws SQLException { - PreparedStatement ps = conn.prepareStatement(SELECT_CITIES); - ps.setString(1,country_code); - ResultSet rs = ps.executeQuery(); - while(rs.next()){ - String name = rs.getString("Country.Name"); - String code = rs.getString("City.Name"); - System.out.println(name+" | "+code); - } - } - - public void printAllSlovakia(Connection conn) throws SQLException{ - Statement statement = conn.createStatement(); - ResultSet rs = statement.executeQuery(SELECT_ALL_SLOVAKIA); - while(rs.next()){ - String name = rs.getString("Name"); - String code = rs.getString("CountryCode"); - String district = rs.getString("District"); - String info = rs.getString("Info"); - - System.out.println(name+" | "+code+" | "+district+" | "+info); - } + public static void main(String[] args) { + DataBase db = new DataBase(); + Country country = db.getCountry("TUR"); + WriterJSON writerJSON = new WriterJSON(); + writerJSON.createJSONdocument("CountryJSON", country); + country.printCity(); } } \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java b/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java new file mode 100644 index 0000000..bddcbfc --- /dev/null +++ b/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java @@ -0,0 +1,40 @@ +package sk.kasv.babcak.worldx; + +import org.json.JSONArray; +import org.json.JSONObject; +import java.io.FileWriter; +import java.io.IOException; + +public class WriterJSON { + public void createJSONdocument(String fileName, Country country){ + JSONObject object = createJsonStructure(country); + writeJsonDataIntoFile(fileName, object); + } + + private void writeJsonDataIntoFile(String fileName, JSONObject object) { + try{ + FileWriter file = new FileWriter(fileName); + file.write(object.toString()); + file.close(); + }catch(IOException e) { + e.printStackTrace(); + } + } + + private static JSONObject createJsonStructure(Country country) { + JSONObject object = new JSONObject(); + object.put("name", country.getName()); + object.put("code2", country.getCode2()); + object.put("code3", country.getCode3()); + object.put("capitalCity", country.getCapital()); + JSONArray jsonArray = new JSONArray(); + for(String city: country.getList()){ + jsonArray.put(city); + } + object.put("citites",jsonArray); + object.put("date", Util.getDate()); + System.out.println(object.toString()); + + return object; + } +} \ No newline at end of file -- GitLab