daily pastebin goal
14%
SHARE
TWEET

Admin

a guest Sep 26th, 2016 24 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package group_project;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.ResultSetMetaData;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. public class Admin {
  14.     public int id, age, roomNum;
  15.     public String firstName, lastName, gender, identityCard, userName, password, roomType, checkInDate, esDateUpdate, roomDateUpdate;
  16.     final int[] extraService = {10, 10, 20, 10, 10};
  17.     final int[] room = {20, 40, 60};
  18.     Scanner input = new Scanner(System.in);
  19.     ArrayList<String> list = new ArrayList<>();
  20.    
  21.  
  22.     public Admin() {
  23.        
  24.     }
  25.    
  26.     public String getCheckInDate() throws SQLException {
  27.         String query = "SELECT CheckInDate FROM information WHERE information.UserName = ?";
  28.         PreparedStatement statement = getConnection().prepareStatement(query);
  29.         statement.setString(1, userName);
  30.         ResultSet rs = statement.executeQuery();
  31.         if (rs.next()) {
  32.             checkInDate = rs.getString(1);
  33.         }
  34.         return checkInDate;
  35.     }
  36.    
  37.     public int getId() throws SQLException {
  38.         String query = "SELECT ID FROM information WHERE information.UserName = ?";
  39.         PreparedStatement s = getConnection().prepareStatement(query);
  40.         s.setString(1, userName);
  41.         ResultSet rs = s.executeQuery();
  42.         if (rs.next()) {
  43.             id = rs.getInt(1);
  44.         }
  45.         return id;
  46.     }
  47.    
  48.     public ArrayList<String> getList() {
  49.         return list;
  50.     }
  51.    
  52.     public void setList(String s) {
  53.         list.add(s);
  54.     }
  55.  
  56.    
  57.     /*
  58.      * @author Jasmin Bektic
  59.      *
  60.      * Method confirms connection
  61.      */
  62.     public Connection getConnection() {
  63.         try {
  64.             String driver = "com.mysql.jdbc.Driver";
  65.             String url = "jdbc:mysql://localhost:3306/hotel";
  66.             String user = "root";
  67.             String pass = "";
  68.             Class.forName(driver);
  69.            
  70.             //Connecting with provided url,userName and pass
  71.             Connection con = DriverManager.getConnection(url, user, pass);
  72.             return con;
  73.         } catch (Exception e) {     //Catch errors
  74.             System.out.println(e);
  75.         }
  76.         return null;
  77.     }
  78.    
  79.     public void checkGuest() {
  80.        
  81.     }
  82.    
  83.     public void enterInfo() throws SQLException {
  84.         firstName = "John";
  85.         lastName = "Snow";
  86.         gender = "m";
  87.         identityCard = "123456";
  88.         age = 29;
  89.         roomNum = checkAvailableRoom();
  90.         roomType = roomType();
  91.         checkInDate = "14:05";
  92.         userName = checkAvailableUserName();
  93.         password = "123456";
  94.        
  95.             Connection con = getConnection();
  96.             String sql = "INSERT INTO information (FirstName, LastName, Gender, IdentityCard, Age, RoomNumber, RoomType, CheckInTime, UserName, Password) VALUES ('"+firstName+"', '"+lastName+"', '"+gender+"', '"+identityCard+"', "+age+", "+roomNum+",'"+roomType+"','"+checkInDate+"','"+userName+"','"+password+"')";
  97.             PreparedStatement statement = con.prepareStatement(sql);
  98.            
  99.             statement.executeUpdate();
  100.     }
  101.    
  102.     /*
  103.      * @author Davor Sadikovic
  104.      *
  105.      * Method for checking available rooms
  106.      */
  107.     public int checkAvailableRoom() throws SQLException {
  108.         Statement s = getConnection().createStatement();
  109.         s.executeQuery("SELECT RoomNumber FROM information");
  110.         ResultSet rs = s.getResultSet();
  111.         System.out.print("Occupied room numbers: ");
  112.        
  113.         //Listing occupied rooms
  114.         while (rs.next()) {
  115.             int room = rs.getInt("RoomNumber");
  116.             System.out.print(room + " ");
  117.         }
  118.         System.out.println();
  119.         rs.close ();
  120.         s.close ();
  121.        
  122.         //Assign room number for a guest
  123.         System.out.println("Enter room number for a guest:");
  124.         int num = input.nextInt();
  125.         return num;
  126.     }
  127.    
  128.     /*
  129.      * @author Jasmin Bektic
  130.      *
  131.      * Assign room type for guest
  132.      */
  133.     public String roomType() {
  134.         System.out.println("\nChoose option:\n1- Single Room\n2- Double Room\n3- Apartment");
  135.         int option = input.nextInt();
  136.         String s = "";
  137.         switch (option) {
  138.             case 1: s = "SingleRoom"; break;
  139.             case 2: s = "DoubleRoom"; break;
  140.             case 3: s = "Apartment"; break;
  141.         }
  142.         return s;
  143.     }
  144.    
  145.     /*
  146.      * @author Jasmin Bektic
  147.      *
  148.      * Method checking available user name
  149.      */
  150.     public String checkAvailableUserName() throws SQLException {
  151.         ArrayList<String> li = new ArrayList<>();
  152.         Statement s = getConnection().createStatement();
  153.         s.executeQuery("SELECT UserName FROM information");
  154.         ResultSet rs = s.getResultSet();
  155.         System.out.println("Assign username:");
  156.         String user = input.next();
  157.        
  158.         //Transfering user names from database to array
  159.         while (rs.next()) {
  160.             String str = rs.getString("UserName");
  161.             li.add(str);
  162.         }
  163.         rs.close ();
  164.         s.close ();
  165.        
  166.         //Compare user name to names from array
  167.         for (int i = 0; i < li.size(); i++) {
  168.             if (user.equals(li.get(i))) {
  169.                 System.out.println("User name already exist, try another one:");
  170.                 user = input.next();
  171.                 i = 0;
  172.             }  
  173.         }
  174.         return user;
  175.     }
  176.    
  177.     /*
  178.      * @author Maja Vasilic
  179.      *
  180.      * providing information from database based on entry
  181.      */
  182.     public void searchDatabase(String s) throws Exception {
  183.         try {
  184.             // establish connection to the database invoking method
  185.             Connection con = getConnection();
  186.             // var query with SQL query
  187.             String query = "SELECT * FROM hotel.information WHERE FirstName = ? OR IdentityCard= ? OR UserName = ?";
  188.             // create statement
  189.             PreparedStatement statement = con.prepareStatement(query);
  190.  
  191.             // actual values are set to parameters
  192.             statement.setString(1, s);
  193.             statement.setString(2, s);
  194.             statement.setString(3, s);
  195.             //query execution
  196.             ResultSet result = statement.executeQuery();
  197.             ResultSetMetaData rsmd = result.getMetaData();
  198.             int columnCount = rsmd.getColumnCount();
  199.             String format = "%-20s";
  200.  
  201.             // print column names
  202.             for (int i = 1; i <= columnCount; i++) {
  203.                 System.out.printf(format, rsmd.getColumnLabel(i));
  204.             }
  205.             System.out.println();
  206.             // print underline
  207.             for (int i = 1; i <= columnCount * 20; i++) {
  208.                 System.out.print("_");
  209.             }
  210.             System.out.println();
  211.             // print information from database
  212.             while (result.next()) {
  213.                 for (int i = 1; i <= columnCount; i++) {
  214.                     System.out.printf(format, result.getString(i));
  215.                 }
  216.                 System.out.println();
  217.             }
  218.         } catch (Exception e) {
  219.             System.out.println(e);
  220.         }
  221.     }
  222.  
  223.     /*
  224.      * @author Jasmin Bektic
  225.      *
  226.      * Method checking for online users
  227.      */
  228.     public void checkOnlineStatus() {
  229.         printStatus();
  230.         while (true) {
  231.             System.out.println();
  232.             System.out.println("Log out user choosing No:");
  233.             int num = input.nextInt();
  234.        
  235.             //Option conditions and print status
  236.             if (num > 0 && num < list.size() + 1) {
  237.                 list.remove(num - 1);
  238.                 printStatus();
  239.             } else if (num == 0) {
  240.                 //metoda MENU
  241.             } else if (num == list.size() + 1) {
  242.                 for (int i = list.size() - 1; i >= 0; i--) {
  243.                     list.remove(i);
  244.                 }
  245.                 printStatus();
  246.             } else if (num == list.size() + 2) {
  247.                 System.exit(0);
  248.             }
  249.         }
  250.     }
  251.    
  252.     /* Online status print method */
  253.     public void printStatus() {
  254.         System.out.println("\nNo\tUserName");
  255.         System.out.println("-----------------");
  256.         for (int i = 1; i <= list.size(); i++) {
  257.             System.out.println(i + "\t" + list.get(i - 1));
  258.         }
  259.         if (list.size() == 0) {
  260.             System.out.println("No users online!");
  261.         }
  262.         System.out.println("-----------------");
  263.         System.out.println("0- Return to menu");
  264.         System.out.println((list.size() + 1) + "- Log out all users");
  265.         System.out.println((list.size() + 2) + "- Shut down");
  266.     }
  267. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top