Facebook
From Crimson Lechwe, 6 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 269
  1. package Database;
  2.  
  3. import java.sql.*;
  4.  
  5. import Model.Card;
  6. import Model.Colour;
  7. import Model.Function;
  8. import Model.Type;
  9.  
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.stream.Collectors;
  13.  
  14. /**
  15.  * Created by Rafal Byczek on 01.01.2018.
  16.  */
  17.  
  18. public class SetOfCards implements Database {
  19.     public static final String DRIVER = "org.sqlite.JDBC";
  20.     public static final String DB_URL = "jdbc:sqlite:animals.db";
  21.     private Connection conn;
  22.     private Statement stat;
  23.  
  24.     public SetOfCards() {
  25.         try {
  26.             Class.forName(SetOfCards.DRIVER);
  27.         } catch (ClassNotFoundException e) {
  28.             System.err.println("Brak sterownika JDBC");
  29.             e.printStackTrace();
  30.         }
  31.         try {
  32.             conn = DriverManager.getConnection(DB_URL);
  33.             stat = conn.createStatement();
  34.         } catch (SQLException e) {
  35.             System.err.println("Problem z otwarciem polaczenia");
  36.             e.printStackTrace();
  37.         }
  38.         System.out.println("Otwarto baze danych.");
  39.         createTables();
  40.     }
  41.  
  42.     public boolean createTables() {
  43.         String createTableWithAnimals = "CREATE TABLE IF NOT EXISTS animals (nr INTEGER PRIMARY KEY, quantity INTEGER, naame varchar(255), colour varchar(255), tyype varchar(255), function varchar(255))";
  44.         try {
  45.             stat.execute(createTableWithAnimals);
  46.             this.insertCard(5,5, "Sokół Wędrowny", "LB", "bir", "None");
  47.             this.insertCard(8,5, "Igłosternik Białogardły", "LB", "bir", "None");
  48.             this.insertCard(16,5, "Pingwin Cesarski", "LG", "bir", "None");
  49.             this.insertCard(18,5, "Szlamnik Zwyczajny", "LG", "bir", "None");
  50.             this.insertCard(24,5, "Dzięcioł Czarny", "LG", "bir", "Dzi");
  51.             this.insertCard(33,5, "Kazuar Hełmiasty", "R", "bir", "Kaz");
  52.             this.insertCard(38,5, "Kłowacz Kaktusowy", "DB", "bir", "None");
  53.             this.insertCard(41,5, "Wrona Brodata", "DB", "bir", "None");
  54.             this.insertCard(44,5, "Ścierwnik", "DB", "bir", "None");
  55.             this.insertCard(46,5, "Dzięciur Żołędziowy", "DB", "bir", "Dzi");
  56.             this.insertCard(55,5, "Małpożer", "V", "bir", "None");
  57.             this.insertCard(62,2, "Trzewikodziób", "O", "bir", "None");
  58.             this.insertCard(0,2, "Marabut Afrykański", "O", "bir", "None");
  59.             this.insertCard(73,5, "Czapla Zielona", "DG", "bir", "None");
  60.             this.insertCard(75,5, "Kukiel Wielki", "DG", "bir", "None");
  61.             this.insertCard(77,5, "Paszczak Australijski", "DG", "bir", "None");
  62.             this.insertCard(79,5, "Fregata Wielka", "DG", "bir", "None");
  63.             this.insertCard(84,5, "Płomykówka Zwyczajna", "DG", "bir", "None");
  64.             this.insertCard(87,5, "Modroara Hiacyntowa", "Y", "bir", "None");
  65.             this.insertCard(94,5, "Tłuszczak", "Y", "bir", "None");
  66.             this.insertCard(97,5, "Żuraw Krzykliwy", "P", "bir", "None");
  67.             this.insertCard(100,5, "Pazik Czarnosterny", "P", "bir", "None");
  68.             this.insertCard(102,5, "Paw Niebieski", "P", "bir", "None");
  69.             this.insertCard(103,5, "Gorzyk Czerwonogłowy", "P", "bir", "None");
  70.             this.insertCard(104,5, "Cudowronka Błękitna", "P", "bir", "None");
  71.             this.insertCard(105,5, "Głuszec", "P", "bir", "None");
  72.             this.insertCard(107,5, "Cyraneczka zwyczajna", "P", "bir", "Cyr");
  73.             this.insertCard(34,5, "Hipopotam Nilowy", "R", "hip", "Red");
  74.             this.insertCard(6,5, "Rawka Błazen", "LB", "ins", "None");
  75.             this.insertCard(9,5, "Ważka", "LB", "ins", "None");
  76.             this.insertCard(22,5, "Danaid Wędrowny", "LG", "ins", "Stp");
  77.             this.insertCard(35,5, "Komar", "R", "ins", "Red");
  78.             this.insertCard(59,5, "Pszczoła Miodna Afrykańska", "V", "ins", "Stp");
  79.             this.insertCard(83,5, "Koczownica", "DG", "ins", "Stp");
  80.             this.insertCard(88,5, "Cykada Cystosoma Saundersii", "Y", "ins", "None");
  81.             this.insertCard(90,5, "Świerszcz Arachnoscelis Arachnoides", "Y", "ins", "None");
  82.             this.insertCard(93,5, "Wioślak", "Y", "ins", "Mch");
  83.             this.insertCard(96,5, "Krewetka Pistoletowa", "Y", "ins", "None");
  84.             this.insertCard(63,2, "Tenrekowiec Pręgowany", "O", "hed", "None");
  85.             this.insertCard(1,5, "Mamba Czarna", "LB", "rep", "None");
  86.             this.insertCard(25,5, "Grzechotnik Preriowy", "R", "rep", "Red");
  87.             this.insertCard(30,5, "Efa Piaskowa", "R", "rep", "Red");
  88.             this.insertCard(32,5, "Krokodyl Różańcowy", "R", "rep", "Red");
  89.             this.insertCard(43,5, "Aligator Amerykański", "DB", "rep", "None");
  90.             this.insertCard(53,5, "Waran z Komodo", "V", "rep", "War");
  91.             this.insertCard(74,5, "Żmija Gabońska", "DG", "rep", "None");
  92.             this.insertCard(80,5, "Toke", "DG", "rep", "None");
  93.             this.insertCard(82,5, "Żółw Sępi", "DG", "rep", "None");
  94.             this.insertCard(29,5, "Tau", "R", "fis", "Red");
  95.             this.insertCard(21,5, "Żaba Chórzystka", "LG", "fro", "None");
  96.             this.insertCard(27,5, "Żaba Psedophryne Corroboree", "R", "fro", "Red");
  97.             this.insertCard(31,5, "Drzewołaz Karłowaty", "R", "fro", "Red");
  98.             this.insertCard(39,5, "Rzekotka Zielona", "DB", "fro", "None");
  99.             this.insertCard(70,2, "Żółwinka Podziemna", "O", "fro", "None");
  100.             this.insertCard(81,5, "Nosorożnica Nosata", "DG", "fro", "None");
  101.             this.insertCard(91,5, "Żaba Coqui", "Y", "fro", "None");
  102.             this.insertCard(108,5, "Chwytnica Czerwonooka", "P", "fro", "None");
  103.             this.insertCard(42,5, "Wydra Morska", "DB", "sml", "None");
  104.             this.insertCard(48,5, "Bóbr Kanadyjski", "DB", "sml", "None");
  105.             this.insertCard(37,5, "Orangutan Borneański", "DB", "mky", "Oth");
  106.             this.insertCard(47,5, "Szympans", "DB", "mky", "Oth");
  107.             this.insertCard(89,5, "Siamang", "Y", "mky", "None");
  108.             this.insertCard(98,5, "Mandryl", "P", "mky", "None");
  109.             this.insertCard(101,5, "Goryl Nizinny", "P", "mky", "None");
  110.             this.insertCard(67,2, "Pancernik Dziewięciopaskowy", "O", "aml", "None");
  111.             this.insertCard(2,5, "Gepard", "LB", "cat", "Giv");
  112.             this.insertCard(36,5, "Likaon", "R", "cat", "Red");
  113.             this.insertCard(52,5, "Tygrys Bengalski", "V", "cat", "None");
  114.             this.insertCard(56,5, "Wilk Europejski", "V", "cat", "None");
  115.             this.insertCard(58,5, "Hiena Centkowana", "V", "cat", "None");
  116.             this.insertCard(76,5, "Pantera Śnieżna", "DG", "cat", "None");
  117.             this.insertCard(12,5, "Guziec Zwyczajny", "LB", "pig", "None");
  118.             this.insertCard(61,2, "Babirussa", "O", "pig", "None");
  119.             this.insertCard(7,5, "Antylopa Skoczek", "LB", "hor", "Giv");
  120.             this.insertCard(10,5, "Gnu Pręgowane", "LB", "hor", "None");
  121.             this.insertCard(20,5, "Wielbłąd Dwugarbny", "LG", "hor", "None");
  122.             this.insertCard(23,5, "Antylopa Widłoroga", "LG", "hor", "None");
  123.             this.insertCard(99,5, "Jeleń Kanadyjski", "P", "hor", "Cch");
  124.             this.insertCard(11,5, "Ogoniak Duży", "LB", "bat", "None");
  125.             this.insertCard(64,2, "Rurkonos Żółtoplamy", "O", "bat", "None");
  126.             this.insertCard(26,5, "Lampart Morski", "R", "sea", "Red");
  127.             this.insertCard(51,5, "Orka Oceaniczna", "V", "sea", "Orc");
  128.             this.insertCard(68,2, "Narwal Jednozębny", "O", "sea", "None");
  129.             this.insertCard(71,2, "Słoń Morski Południowy", "O", "sea", "None");
  130.             this.insertCard(92,5, "Foka Wedela", "Y", "sea", "None");
  131.             this.insertCard(95,5, "Wieloryb Biskajski", "Y", "sea", "None");
  132.             this.insertCard(3,5, "Zając Szarak", "LB", "rod", "None");
  133.             this.insertCard(40,5, "Szczur Wędrowny", "DB", "rod", "None");
  134.             this.insertCard(45,5, "Pręgowiec", "DB", "rod", "None");
  135.             this.insertCard(50,5, "Diabeł Tasmański", "V", "rod", "None");
  136.             this.insertCard(19,5, "Koala", "LG", "tre", "None");
  137.             this.insertCard(28,5, "Kukang Ciemnolicy", "R", "tre", "Red");
  138.             this.insertCard(65,2, "Karłowaty Leniwiec Trójpalczasty", "O", "tre", "None");
  139.             this.insertCard(66,2, "Nosacz Sundyjski", "O", "tre", "Pol");
  140.             this.insertCard(85,5, "Wyjec Czarny", "Y", "tre", "None");
  141.             this.insertCard(106,5, "Lemu Kata", "P", "tre", "None");
  142.             this.insertCard(49,5, "Niedźwiedź Grizzly", "V", "bea", "None");
  143.             this.insertCard(57,5, "Niedźwiedź Polarny", "V", "bea", "None");
  144.             this.insertCard(14,5, "Ratel Miodożerny", "LG", "sml", "Dem");
  145.             this.insertCard(17,5, "Mangusta Pręgowana", "LG", "sml", "Dem");
  146.             this.insertCard(54,5, "Fossa Madagaskarska", "V", "sml", "None");
  147.             this.insertCard(60,5, "Rosomak", "V", "sml", "None");
  148.             this.insertCard(4,4, "Lew Afrykański", "ALL", "all", "All");
  149.             this.insertCard(13,4, "Bawół Afrykański", "ALL", "all", "All");
  150.             this.insertCard(15,4, "Nosorożec Czarny", "ALL", "all", "All");
  151.             this.insertCard(78,4, "Lampart", "ALL", "all", "All");
  152.             this.insertCard(86,4, "Słoń Afrykański", "ALL", "all", "All");
  153.             this.insertCard(109,5, "Alpaka", "LG", "hor", "None");
  154.             this.insertCard(110,5, "Kangur Szary", "LB", "koo", "None");
  155.             this.insertCard(111,5, "Bielik Zwyczajny", "V", "bir", "None");
  156.             this.insertCard(112,5, "Panda Wielka", "P", "tre", "None");
  157.             this.insertCard(113,5, "Żarłacz Biały", "R", "sea", "Red");
  158.             this.insertCard(114,5, "Lis Rudy", "DG", "sml", "None");
  159.             this.insertCard(115,5, "Koń Polski", "Y", "hor", "None");
  160.             this.insertCard(116,5, "Świnia Domowa", "DB", "pig", "None");
  161.         } catch (SQLException e) {
  162.             System.err.println("Blad przy tworzeniu tabeli");
  163.             e.printStackTrace();
  164.             return false;
  165.         }
  166.         return true;
  167.     }
  168.  
  169.     public boolean insertCard(Integer nr, Integer quantity, String name, String colour, String type, String function) {
  170.         try {
  171.             PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO animals VALUES(?, ?, ?, ?, ?, ?);");
  172.             preparedStatement.setInt(1, nr);
  173.             preparedStatement.setInt(2, quantity);
  174.             preparedStatement.setString(3, name);
  175.             preparedStatement.setString(4, colour);
  176.             preparedStatement.setString(5, type);
  177.             preparedStatement.setString(6, function);
  178.             preparedStatement.execute();
  179.         } catch (SQLException e) {
  180.             System.err.println("Blad przy dodawaniu zwierzęcia do bazy."+nr+name);
  181.             e.printStackTrace();
  182.             return false;
  183.         }
  184.         return true;
  185.     }
  186.  
  187.     public boolean clearTable() {
  188.         try {
  189.             PreparedStatement preparedStatement = conn.prepareStatement("DELETE FROM animals;");
  190.             preparedStatement.execute();
  191.         } catch (SQLException e) {
  192.             System.err.println("Blad przy usuwaniu tabeli");
  193.             e.printStackTrace();
  194.             return false;
  195.         }
  196.         return true;
  197.     }
  198.  
  199.     public void closeConnection() {
  200.         try {
  201.             conn.close();
  202.             System.out.println("Zamknieto baze danych.");
  203.         } catch (SQLException e) {
  204.             System.err.println("Problem z zamknieciem polaczenia");
  205.             e.printStackTrace();
  206.         }
  207.     }
  208.  
  209.     @Override
  210.     public List<Card> getAllCards() {
  211.         List<Card> cardList = new LinkedList<>();
  212.         try {
  213.             ResultSet resultSet = stat.executeQuery("SELECT * FROM animals;");
  214.             while (resultSet.next()) {
  215.                 Integer number = resultSet.getInt("nr");
  216.                 Integer quantity = resultSet.getInt("quantity");
  217.                 String name = resultSet.getString("naame");
  218.  
  219.                 String colour = resultSet.getString("colour");
  220.                 Colour colour2 = this.switchStringIntoColour(colour);
  221.  
  222.                 String type = resultSet.getString("tyype");
  223.                 Type type2 = this.switchStringIntoType(type);
  224.  
  225.                 String function = resultSet.getString("function");
  226.                 Function function2 = this.switchStringIntoFunction(function);
  227.  
  228.                 Card card = new Card(number, name, colour2, type2, function2);
  229.                 for(int i = 0; i < quantity; i++)
  230.                     cardList.add(card);
  231.             }
  232.         } catch (SQLException e) {
  233.             e.printStackTrace();
  234.             return null;
  235.         }
  236.         return cardList;
  237.     }
  238.  
  239.     @Override
  240.     public List<Card> getCardsWithColour(Colour colour) {
  241.         List<Card> listCard = this.getAllCards();
  242.         return listCard.stream().filter(item -> item.getColour() == colour).collect(Collectors.toList());
  243.     }
  244.  
  245.     @Override
  246.     public List<Card> getCardsWithType(Type type) {
  247.         List<Card> listCard = this.getAllCards();
  248.         return listCard.stream().filter(item -> item.getType() == type).collect(Collectors.toList());
  249.     }
  250.  
  251.     @Override
  252.     public List<Card> getCardsWithFunction(Function function) {
  253.         List<Card> listCard = this.getAllCards();
  254.         return listCard.stream().filter(item -> item.getFunction() == function).collect(Collectors.toList());
  255.     }
  256.  
  257.     private Colour switchStringIntoColour(String colour) {
  258.         Colour colour2 = null;
  259.         switch (colour) {
  260.             case "LB":
  261.                 colour2 = Colour.LB;
  262.                 break;
  263.             case "LG":
  264.                 colour2 = Colour.LG;
  265.                 break;
  266.             case "R":
  267.                 colour2 = Colour.R;
  268.                 break;
  269.             case "DB":
  270.                 colour2 = Colour.DB;
  271.                 break;
  272.             case "V":
  273.                 colour2 = Colour.V;
  274.                 break;
  275.             case "O":
  276.                 colour2 = Colour.O;
  277.                 break;
  278.             case "DG":
  279.                 colour2 = Colour.DG;
  280.                 break;
  281.             case "Y":
  282.                 colour2 = Colour.Y;
  283.                 break;
  284.             case "P":
  285.                 colour2 = Colour.P;
  286.                 break;
  287.             case "ALL":
  288.                 colour2 = Colour.ALL;
  289.                 break;
  290.         }
  291.         return colour2;
  292.     }
  293.  
  294.     private Type switchStringIntoType(String type) {
  295.         Type type2 = null;
  296.         switch (type) {
  297.             case "bir":
  298.                 type2 = Type.bir;
  299.                 break;
  300.             case "hip":
  301.                 type2 = Type.hip;
  302.                 break;
  303.             case "ins":
  304.                 type2 = Type.ins;
  305.                 break;
  306.             case "hed":
  307.                 type2 = Type.hed;
  308.                 break;
  309.             case "rep":
  310.                 type2 = Type.rep;
  311.                 break;
  312.             case "fis":
  313.                 type2 = Type.fis;
  314.                 break;
  315.             case "fro":
  316.                 type2 = Type.fro;
  317.                 break;
  318.             case "bew":
  319.                 type2 = Type.bew;
  320.                 break;
  321.             case "mky":
  322.                 type2 = Type.mky;
  323.                 break;
  324.             case "aml":
  325.                 type2 = Type.aml;
  326.                 break;
  327.             case "cat":
  328.                 type2 = Type.cat;
  329.                 break;
  330.             case "pig":
  331.                 type2 = Type.pig;
  332.                 break;
  333.             case "hor":
  334.                 type2 = Type.hor;
  335.                 break;
  336.             case "bat":
  337.                 type2 = Type.bat;
  338.                 break;
  339.             case "see":
  340.                 type2 = Type.see;
  341.                 break;
  342.             case "rod":
  343.                 type2 = Type.rod;
  344.                 break;
  345.             case "tre":
  346.                 type2 = Type.tre;
  347.                 break;
  348.             case "bea":
  349.                 type2 = Type.bea;
  350.                 break;
  351.             case "sml":
  352.                 type2 = Type.sml;
  353.                 break;
  354.             case "all":
  355.                 type2 = Type.all;
  356.                 break;
  357.         }
  358.         return type2;
  359.     }
  360.  
  361.     private Function switchStringIntoFunction(String function) {
  362.         Function function2 = null;
  363.         switch (function) {
  364.             case "Dzi":
  365.                 function2 = Function.Dzi;
  366.                 break;
  367.             case "Kaz":
  368.                 function2 = Function.Kaz;
  369.                 break;
  370.             case "Cyr":
  371.                 function2 = Function.Cyr;
  372.                 break;
  373.             case "Red":
  374.                 function2 = Function.Red;
  375.                 break;
  376.             case "Stp":
  377.                 function2 = Function.Stp;
  378.                 break;
  379.             case "Mch":
  380.                 function2 = Function.Mch;
  381.                 break;
  382.             case "War":
  383.                 function2 = Function.War;
  384.                 break;
  385.             case "Oth":
  386.                 function2 = Function.Oth;
  387.                 break;
  388.             case "Giv":
  389.                 function2 = Function.Giv;
  390.                 break;
  391.             case "Cch":
  392.                 function2 = Function.Cch;
  393.                 break;
  394.             case "Orc":
  395.                 function2 = Function.Orc;
  396.                 break;
  397.             case "Pol":
  398.                 function2 = Function.Pol;
  399.                 break;
  400.             case "Dem":
  401.                 function2 = Function.Dem;
  402.                 break;
  403.             case "All":
  404.                 function2 = Function.All;
  405.                 break;
  406.             case "None":
  407.                 function2 = Function.None;
  408.                 break;
  409.         }
  410.         return function2;
  411.     }
  412. }
  413.