Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package group_project;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class Guest extends Admin {
- String userOnline, dateTime;
- public Guest() {
- }
- public String getESDateUpdate() throws Exception {
- String query = "SELECT DateUpdate FROM extra_service WHERE ID = ?";
- PreparedStatement statement = getConnection().prepareStatement(query);
- statement.setInt(1, getId());
- ResultSet rs = statement.executeQuery();
- if (rs.next()) {
- esDateUpdate = rs.getString(1);
- }
- return esDateUpdate;
- }
- public String getRoomDateUpdate() throws Exception {
- String query = "SELECT DateUpdate FROM room WHERE ID = ?";
- PreparedStatement statement = getConnection().prepareStatement(query);
- statement.setInt(1, getId());
- ResultSet rs = statement.executeQuery();
- if (rs.next()) {
- roomDateUpdate = rs.getString(1);
- }
- return roomDateUpdate;
- }
- /*
- * @author Jasmin Bektic
- *
- * Method acting like LogIn form
- */
- public void logIn() throws Exception {
- Statement s = getConnection().createStatement();
- s.executeQuery("SELECT UserName, Password FROM information");
- ResultSet rs = s.getResultSet();
- //username and pass input
- boolean boo = true;
- while (boo) {
- System.out.println("Enter username:");
- userName = input.next();
- System.out.println("Enter password:");
- password = input.next();
- //Processing current data and comparing to data from database
- while (rs.next()) {
- String user = rs.getString("UserName");
- String pass = rs.getString("Password");
- if (userName.equals(user) && password.equals(pass)) {
- boo = false;
- break;
- }
- }
- //Supporting message, in case of input failure
- if (boo == true) System.out.println("Incorrect user name or password, try again:");
- }
- System.out.println();
- //Add username in array
- PreparedStatement statement = getConnection().prepareStatement("SELECT UserName FROM information WHERE information.UserName = ?");
- statement.setString(1, userName);
- userOnline = (rs.getString(1));
- rs.close ();
- s.close ();
- setList(userOnline);
- }
- /* Method acts like LogOut form */
- public void logOut() {
- list.remove(list.size() - 1);
- // ovdje ide metoda za glavni meni
- }
- /*
- * @author Jasmin Bektic
- *
- * Method from which guest can order new extra services
- */
- public void orderExtraService() throws Exception {
- int[] extraService = new int[5];
- int sum = 0;
- //Getting old extra service prices and sum
- String query = "SELECT Gym, Cinema, Restaurant, Pool, Sauna FROM extra_service WHERE ID = ?";
- PreparedStatement statement = getConnection().prepareStatement(query);
- statement.setInt(1, getId());
- ResultSet rs = statement.executeQuery();
- while (rs.next()) {
- for (int i = 0; i < 5; i++) {
- extraService[i] = rs.getInt(i + 1);
- sum += rs.getInt(i + 1);
- }
- }
- //Updating extra service list
- System.out.println("1- Gym\n2- Cinema\n3- Restaurant\n4- Pool\n5- Sauna\n\n0- Finish my order");
- int index = 0;
- while (true) {
- System.out.println("Order service entering adequate number or finish your order:");
- index = input.nextInt();
- if (index == 0) break;
- extraService[index - 1] = this.extraService[index - 1];
- }
- int balance = balance(getESDateUpdate(), sum);
- //Get old balance and add new one
- PreparedStatement st = getConnection().prepareStatement("SELECT Balance FROM extra_service WHERE ID = ?");
- st.setInt(1, getId());
- ResultSet result = st.executeQuery();
- if (result.next()) {
- balance += result.getInt("Balance");
- }
- //Update query with specified ID
- String sql = "UPDATE extra_service SET Gym = ?, Cinema= ?, Restaurant= ?, Pool = ?, Sauna = ?, Balance = ?, DateUpdate = ? WHERE ID = ?";
- PreparedStatement s = getConnection().prepareStatement(sql);
- s.setInt(1, extraService[0]);
- s.setInt(2, extraService[1]);
- s.setInt(3, extraService[2]);
- s.setInt(4, extraService[3]);
- s.setInt(5, extraService[4]);
- s.setInt(6, balance);
- s.setString(7, dateTime);
- s.setInt(8, getId());
- s.executeUpdate();
- }
- /*
- * @author Jasmin Bektic
- *
- * Method used for calculating total balance for certain number of days
- */
- public int balance(String s, int sum) throws Exception {
- //Current date and time
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = new Date();
- String orderDate = df.format(date);
- Date d1 = df.parse(s);
- Date d2 = df.parse(orderDate);
- //Number of days between current date and checkIn date (time is default, 12:00:00)
- long diff = d2.getTime() - d1.getTime();
- long diffDays = diff / (24 * 60 * 60 * 1000);
- dateTime = orderDate.substring(0, 10) + " 12:00:00";
- int balance = 0;
- if (diffDays == 0) {
- balance = sum;
- } else {
- balance = (int) (diffDays * sum);
- }
- return balance;
- }
- /*
- * @author Jasmin Bektic
- *
- * Method from which guest can order new room
- */
- public void orderNewRoom() throws Exception {
- String newRoom = roomType();
- int[] room = new int[3];
- int sum = 0;
- //Get current room price
- String query = "SELECT SingleRoom, DoubleRoom, Apartment FROM room WHERE ID = ?";
- PreparedStatement statement = getConnection().prepareStatement(query);
- statement.setInt(1, getId());
- ResultSet rs = statement.executeQuery();
- while (rs.next()) {
- for (int i = 0; i < 3; i++) {
- room[i] = rs.getInt(i + 1);
- sum += rs.getInt(i + 1);
- }
- }
- int balance = balance(getRoomDateUpdate(), sum);
- //Get old balance and add new one
- PreparedStatement st = getConnection().prepareStatement("SELECT Balance FROM room WHERE ID = ?");
- st.setInt(1, getId());
- ResultSet result = st.executeQuery();
- if (result.next()) {
- balance += result.getInt("Balance");
- }
- switch (newRoom) {
- case "SingleRoom": room[0] = this.room[0];room[1] = 0;room[2] = 0; break;
- case "DoubleRoom": room[1] = this.room[1];room[0] = 0;room[2] = 0; break;
- case "Apartment": room[2] = this.room[2];room[0] = 0;room[1] = 0; break;
- }
- //Update room price which guest use
- String sql = "UPDATE room SET SingleRoom = ?, DoubleRoom = ?, Apartment = ?, Balance = ?, DateUpdate = ? WHERE ID = ?";
- PreparedStatement s = getConnection().prepareStatement(sql);
- s.setInt(1, room[0]);
- s.setInt(2, room[1]);
- s.setInt(3, room[2]);
- s.setInt(4, balance);
- s.setString(5, dateTime);
- s.setInt(6, getId());
- s.executeUpdate();
- }
- /*
- * @author Maja Vasilic
- *
- * Method copying information from "information" table to the "archive"
- * table and removes user from "information"
- * @throws Exception
- */
- public void userCheckOut(String name) throws Exception {
- // establish connection to the database hotel invoking method
- Connection conn = getConnection();
- try {
- // create statement variable
- Statement stSelect = conn.createStatement();
- // query execution (find row with specified name)
- ResultSet result = stSelect.executeQuery("SELECT * FROM information WHERE FirstName = '"+ name + "'");
- System.out.println("Sending data to the archive.");
- // create prepared statement to insert values in archive from information
- PreparedStatement stInsert = (PreparedStatement) conn.prepareStatement("INSERT INTO archive (FirstName, LastName,"+ " Gender, IdentityCard, Age) VALUES (?, ?, ?, ?, ?)");
- // set values (get values from one table to another)
- stInsert.setString(1, result.getString(2));
- stInsert.setString(2, result.getString(3));
- stInsert.setString(3, result.getString(4));
- stInsert.setString(4, result.getString(5));
- stInsert.setInt(5, result.getInt(6));
- // execute query to insert values in the archive to the information
- stInsert.executeQuery();
- // how we can remove user from current table for users
- stSelect.executeQuery("DELETE FROM information WHERE FirstName = '"+ name + "'");
- // print message
- System.out.println("User " + name + " is removed.");
- conn.close();
- } catch (Exception e) {
- System.out.println(e);
- } finally {
- if (conn != null) {
- // close connection with the database
- conn.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement