Guest User

Untitled

a guest
Nov 25th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.65 KB | None | 0 0
  1. public class Application {
  2.  
  3.     public static void main(String[] args) {
  4.        
  5.         UserDao uDao = new UserDao();
  6.         BookDao bDao = new BookDao();
  7.         RentDao rDao = new RentDao();
  8.         Scanner sc = new Scanner(System.in);
  9.         System.out.println("Witamy w Bibliotece!");
  10.         System.out.println("Wybierz co chcesz zrobić:");
  11.         System.out.println("    ____UŻYTKOWNICY____");
  12.         System.out.println("1. Dodaj nowego użytkownika");
  13.         System.out.println("2. Edytuj dane użytkownika");
  14.         System.out.println("3. Usuń użytkownika");
  15.         System.out.println("4. Wyświetl wszystkich użytkowników");
  16.         System.out.println("    ____KSIĄŻKI____");
  17.         System.out.println("5. Dodaj nową książkę");
  18.         System.out.println("6. Wyświetl wszystkie książki");
  19.         System.out.println("    ____WYPOŻYCZENIA____");
  20.         System.out.println("7. Dodaj nowe wypożyczenie");
  21.         System.out.println("8. Wyświetl wszystkie wypożyczenia");
  22.         int choose = sc.nextInt();
  23.         switch (choose){
  24.         case 1:{
  25.             //addUser
  26.             User u = new User();
  27.             System.out.print("Podaj imię: ");
  28.             u.setName(sc.next());
  29.             System.out.print("Podaj nazwisko: ");
  30.             u.setLastname(sc.next());
  31.             System.out.print("Podaj numer telefonu: ");
  32.             u.setTelephoneNumber(sc.nextInt());
  33.             uDao.addUser(u);
  34.             break;
  35.         }
  36.         case 2: {
  37.             //editUser
  38.             System.out.print("Podaj ID użytkownika: ");
  39.             uDao.editUser(sc.nextInt());
  40.             break;
  41.         }
  42.         case 3: {
  43.             //removeUser
  44.             System.out.print("Podaj ID użytkownika: ");
  45.             uDao.removeUser(sc.nextInt());
  46.             break;
  47.         }
  48.         case 4: {
  49.             //showAllUsers
  50.             uDao.showAll();
  51.         }
  52.         case 5: {
  53.             //addBook
  54.             Book b = new Book();
  55.             System.out.print("Podaj tytuł: ");
  56.             b.setTitle(sc.next());
  57.             System.out.print("Podaj autora: ");
  58.             b.setAuthor(sc.next());
  59.             System.out.print("Podaj rok publikacji: ");
  60.             b.setPublishedYear(sc.nextInt());
  61.             bDao.addBook(b);
  62.             break;
  63.         }
  64.         case 6: {
  65.             //showAllBooks
  66.             bDao.showAll();
  67.             break;
  68.         }
  69.         case 7: {
  70.             //addRent
  71.             System.out.print("Podaj id użytkownika: ");
  72.             int userId = sc.nextInt();
  73.             System.out.print("Podaj id książki: ");
  74.             int bookId = sc.nextInt();
  75.             rDao.addRent(userId, bookId);
  76.             break;
  77.         }
  78.         case 8: {
  79.             //showAllRents
  80.             rDao.showAll();
  81.             break;
  82.         }
  83.         default: {
  84.             System.out.println("Nieprawidłowy wybór!");
  85.         }
  86.         }
  87.         sc.close();
  88.     }
  89. }
  90.  
  91. public class Book {
  92.    
  93.     private String title;
  94.     private String author;
  95.     private int publishedYear;
  96.    
  97.    
  98.    
  99.     public String getTitle() {
  100.         return title;
  101.     }
  102.  
  103.     public void setTitle(String title) {
  104.         this.title = title;
  105.     }
  106.  
  107.     public String getAuthor() {
  108.         return author;
  109.     }
  110.  
  111.     public void setAuthor(String author) {
  112.         this.author = author;
  113.     }
  114.  
  115.     public int getPublishedYear() {
  116.         return publishedYear;
  117.     }
  118.  
  119.     public void setPublishedYear(int publishedYear) {
  120.         this.publishedYear = publishedYear;
  121.     }
  122.  
  123.     public Book(String title, String author, int publishedYear) {
  124.         super();
  125.         this.title = title;
  126.         this.author = author;
  127.         this.publishedYear = publishedYear;
  128.     }
  129.  
  130.     public Book() {
  131.         super();
  132.     }  
  133. }
  134. public class BookDao {
  135.  
  136.     private MySQLConnector connect;
  137.      
  138.     public BookDao() {
  139.         connect = MySQLConnector.getINSTANCE();
  140.     }
  141.      
  142.     public void addBook(Book book)  {
  143.         String sql = "INSERT INTO book VALUES(?,?,?,?)";
  144.         PreparedStatement state = connect.getPreparedStatement(sql);
  145.         try {
  146.             state.setNull(1, 0);
  147.             state.setString(2, book.getTitle());
  148.             state.setString(3, book.getAuthor());
  149.             state.setInt(4, book.getPublishedYear());
  150.             state.execute();
  151.             state.isCloseOnCompletion();
  152.             System.out.println("Książka została dodana.");
  153.         } catch (SQLException e) {
  154.             e.printStackTrace();
  155.         }
  156.     }
  157.    
  158.    
  159.      public void showAll(){
  160.          Statement state = connect.getStatement();
  161.          ResultSet rs;
  162.         try {
  163.             rs = state.executeQuery("SELECT * FROM book");
  164.              while(rs.next()){
  165.                     System.out.println("");
  166.                     System.out.println("ID: " + rs.getInt("id"));
  167.                     System.out.println("Tytuł: " + rs.getString("title"));
  168.                     System.out.println("Autor: " + rs.getString("author"));
  169.                     System.out.println("Rok wydania: " + rs.getInt("publishedYear"));
  170.             }
  171.         } catch (SQLException e) {
  172.             e.printStackTrace();
  173.         }        
  174.      }
  175. }
  176.  
  177. public class Rent {
  178.     private User user;
  179.     private Book book;
  180.    
  181.    
  182.     public Rent(User user, Book book) {
  183.         super();
  184.         this.user = user;
  185.         this.book = book;
  186.     }
  187.  
  188.     public User getUser() {
  189.         return user;
  190.     }
  191.     public void setUser(User user) {
  192.         this.user = user;
  193.     }
  194.     public Book getBook() {
  195.         return book;
  196.     }
  197.     public void setBook(Book book) {
  198.         this.book = book;
  199.     }
  200. }
  201.  
  202. public class RentDao {
  203.     private MySQLConnector connect;
  204.      
  205.      public RentDao() {
  206.          connect = MySQLConnector.getINSTANCE();
  207.      }
  208.      
  209.      public void addRent(int userId, int bookId)  {
  210.          String sql = "INSERT INTO rent VALUES(?,?,?,?)";
  211.          PreparedStatement state = connect.getPreparedStatement(sql);
  212.          try {
  213.             state.setNull(1, 0);
  214.             state.setInt(2, userId);
  215.             state.setInt(3, bookId);
  216.             state.setNull(4, 0);
  217.             state.execute();
  218.             state.isCloseOnCompletion();
  219.             System.out.println("Wypożyczenie zostało dodane.");
  220.         } catch (SQLException e) {
  221.             e.printStackTrace();
  222.         }
  223.          
  224.      }
  225.      
  226.      public void showAll(){
  227.         Statement state = connect.getStatement();
  228.         ResultSet rs;
  229.         try {
  230.             rs = state.executeQuery("SELECT * FROM rent");
  231.             while(rs.next()){
  232.                     System.out.println("");
  233.                     System.out.println("Id użytkownika: " + rs.getInt("user"));
  234.                     System.out.println("Id książki: " + rs.getInt("book"));
  235.             }
  236.         }
  237.         catch (SQLException e) {
  238.             e.printStackTrace();
  239.         }        
  240.      }
  241. }
  242.  
  243. public class User {
  244.  
  245.     private String name;
  246.     private String lastname;
  247.     private int telephoneNumber;
  248.    
  249.    
  250.    
  251.     public String getName() {
  252.         return name;
  253.     }
  254.  
  255.     public void setName(String name) {
  256.         this.name = name;
  257.     }
  258.  
  259.     public String getLastname() {
  260.         return lastname;
  261.     }
  262.  
  263.     public void setLastname(String lastname) {
  264.         this.lastname = lastname;
  265.     }
  266.  
  267.     public int getTelephoneNumber() {
  268.         return telephoneNumber;
  269.     }
  270.  
  271.     public void setTelephoneNumber(int telephoneNumber) {
  272.         this.telephoneNumber = telephoneNumber;
  273.     }
  274.  
  275.     public User(String name, String lastname, int telephoneNumber) {
  276.         super();
  277.         this.name = name;
  278.         this.lastname = lastname;
  279.         this.telephoneNumber = telephoneNumber;
  280.     }
  281.  
  282.     public User() {
  283.         super();
  284.     }
  285. }
  286.  
  287. public class UserDao {
  288.     private MySQLConnector connect;
  289.      
  290.      public UserDao() {
  291.          connect = MySQLConnector.getINSTANCE();
  292.      }
  293.      
  294.      public void addUser(User user)  {
  295.          String sql = "INSERT INTO user VALUES(?,?,?,?)";
  296.          PreparedStatement state = connect.getPreparedStatement(sql);
  297.          try {
  298.             state.setNull(1, 0);
  299.             state.setString(2, user.getName());
  300.             state.setString(3, user.getLastname());
  301.             state.setInt(4, user.getTelephoneNumber());
  302.             state.execute();
  303.             state.isCloseOnCompletion();
  304.             System.out.println("Użytkownik został dodany.");
  305.         } catch (SQLException e) {
  306.             e.printStackTrace();
  307.         }
  308.          
  309.      }
  310.      
  311.      public void removeUser(int id)  {
  312.          String sql = "DELETE FROM user WHERE id = ?";
  313.          PreparedStatement state = connect.getPreparedStatement(sql);
  314.          try {
  315.                 state.setInt(1, id);
  316.                 state.execute();
  317.                 state.isCloseOnCompletion();
  318.                 System.out.println("Użytkownik został usunięty.");
  319.             } catch (SQLException e) {
  320.                 e.printStackTrace();
  321.             }
  322.      }
  323.      
  324.      public void editUser(int id)  {
  325.          Scanner sc = new Scanner(System.in);
  326.          System.out.println("Wybierz co chcesz edytować:");
  327.          System.out.println("1. Imię");
  328.          System.out.println("2. Nazwisko");
  329.          System.out.println("3. Numer telefonu");
  330.          int choice = sc.nextInt();
  331.          switch(choice){
  332.          case 1:
  333.             {
  334.                 System.out.print("Podaj nowe imię: ");
  335.                 String name = sc.next();
  336.                 String sql = "UPDATE user SET name = '" + name + "' WHERE id=" + id + "";
  337.                 PreparedStatement state = connect.getPreparedStatement(sql);
  338.                 try {
  339.                     state.execute();
  340.                     state.isCloseOnCompletion();
  341.                     System.out.println("Imię zostało zmienione.");
  342.                 } catch (SQLException e) {
  343.                     e.printStackTrace();
  344.                 }
  345.                 break;
  346.             }
  347.          case 2:
  348.             {
  349.                 System.out.print("Podaj nowe nazwisko: ");
  350.                 String lastname = sc.next();
  351.                 String sql = "UPDATE user SET lastname = '" + lastname + "' WHERE id=" + id + "";
  352.                 PreparedStatement state = connect.getPreparedStatement(sql);
  353.                 try {
  354.                     state.execute();
  355.                     state.isCloseOnCompletion();
  356.                     System.out.println("Nazwisko zostało zmienione.");
  357.                 } catch (SQLException e) {
  358.                     e.printStackTrace();
  359.                 }
  360.                 break;
  361.             }
  362.          case 3:
  363.             {
  364.                 System.out.print("Podaj nowy numer telefonu: ");
  365.                 int telephoneNumber = sc.nextInt();
  366.                 String sql = "UPDATE user SET telephoneNumber = '" + telephoneNumber + "' WHERE id=" + id + "";
  367.                 PreparedStatement state = connect.getPreparedStatement(sql);
  368.                 try {
  369.                     state.execute();
  370.                     state.isCloseOnCompletion();
  371.                     System.out.println("Numer telefonu został zmieniony.");
  372.                 } catch (SQLException e) {
  373.                     e.printStackTrace();
  374.                 }
  375.                 break;
  376.             }
  377.          default:
  378.             {
  379.                 System.out.println("Nieprawidłowy wybór!");
  380.             }
  381.          }
  382.         sc.close();
  383.      }
  384.    
  385.      public void showAll(){
  386.         Statement state = connect.getStatement();
  387.         ResultSet rs;
  388.         try {
  389.             rs = state.executeQuery("SELECT * FROM user");
  390.              while(rs.next()){
  391.                     System.out.println("");
  392.                     System.out.println("ID: " + rs.getInt("id"));
  393.                     System.out.println("Imię i nazwisko: " + rs.getString("name") + " " + rs.getString("lastname"));
  394.                     System.out.println("Numer telefonu: " + rs.getInt("telephoneNumber"));
  395.             }
  396.         } catch (SQLException e) {
  397.             e.printStackTrace();
  398.         }        
  399.      }
  400. }
  401.  
  402. public class MySQLConnector {
  403.     private static MySQLConnector instance;
  404.     private final static String DBURL = "jdbc:mysql://5.135.218.27/Dorota?characterEncoding=utf8";
  405.     private final static String DBLOGIN = "root";
  406.     private final static String DBPASSWORD = "10135886";
  407.    
  408.     private Connection conn;
  409.    
  410.    
  411.     private MySQLConnector(){
  412.         try {
  413.             Class.forName("com.mysql.jdbc.Driver");
  414.             conn = DriverManager.getConnection(DBURL, DBLOGIN, DBPASSWORD);
  415.            
  416.         } catch (ClassNotFoundException e) {
  417.             e.printStackTrace();
  418.         } catch (SQLException e) {
  419.             e.printStackTrace();
  420.         }
  421.         System.out.println("Połączono!");
  422.        
  423.     }
  424.     public static MySQLConnector getINSTANCE(){
  425.         if (instance == null){
  426.             instance = new MySQLConnector();
  427.         }
  428.         return instance;
  429.     }
  430.    
  431.     public Statement getStatement(){
  432.         try {
  433.             return conn.createStatement();
  434.         } catch (SQLException e) {
  435.             e.printStackTrace();
  436.         }
  437.         return null;
  438.     }
  439.    
  440.     public PreparedStatement getPreparedStatement(String sql){
  441.         try {
  442.             return conn.prepareStatement(sql);
  443.         } catch (SQLException e) {
  444.             e.printStackTrace();
  445.         }
  446.         return null;
  447.     }
  448. }
Advertisement
Add Comment
Please, Sign In to add comment