Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.57 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.PrintWriter;
  4. import java.net.Socket;
  5.  
  6. //Server client
  7. public class Client {
  8. private static BufferedReader input;
  9. private static PrintWriter output;
  10. private boolean connected = false;
  11.  
  12. public void clientOptions(int choice) {
  13. int userInput = choice;//scanner.nextInt();
  14. try(Socket socket = new Socket("212.159.23.21",4444/*"localhost", 40450*/)){
  15. input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  16. output = new PrintWriter(socket.getOutputStream(), true);
  17. connected = true;
  18. switch (userInput) {
  19. case 1:
  20. String cabinNo = "57";
  21. String email = "George@live.com";
  22. String userpw = "Georgina";
  23. String cruise = "50";
  24. output.println("Hello there, Mr.Ross. Would you like some tea?");
  25. //output.println(1+","+cabinNo+","+email+","+userpw+","+cruise);
  26. break;
  27. case 2:
  28. cabinNo = "57";
  29. userpw = "Georgina";
  30. output.println(2+","+cabinNo+","+userpw);
  31. break;
  32. case 3:
  33. cabinNo = "57";
  34. output.println(3+","+cabinNo);
  35. break;
  36. case 4:
  37. cabinNo = "57";
  38. output.println(4+","+cabinNo);
  39. break;
  40. case 5:
  41. cabinNo = "57";
  42. String excursionId = "971156";
  43. String numberOfPeople = "3";
  44. output.println(5+","+cabinNo+","+excursionId+","+numberOfPeople);
  45. break;
  46. }
  47. }
  48. catch(Exception e){
  49. System.out.println("Exception: "+e);
  50. }
  51. /*catch(SocketTimeoutException e){
  52. System.out.println("The socket has timed out");
  53. }
  54. catch(IOException e) {
  55. System.out.println("Client error: " + e.getMessage());
  56. }*/
  57. }
  58.  
  59.  
  60. public void registerUser(){
  61. System.out.println("Made it");
  62. String cabinNo = "57";
  63. String email = "George@live.com";
  64. String userpw = "Georgina";
  65. output.println(1+","+cabinNo+","+email+","+userpw);
  66. }
  67.  
  68. public void logIn(){
  69. String cabinNo = "57";
  70. String userpw = "Georgina";
  71. output.println(2+","+cabinNo+","+userpw);
  72. }
  73.  
  74. public void logOut(){
  75. String rndm = "random";
  76. output.println(3+","+"57");
  77. }
  78.  
  79. public static void bookings(){
  80.  
  81. }
  82.  
  83. public void excursions(){
  84. System.out.println("yay");
  85. }
  86. }
  87. ========================================================================
  88.  
  89. import javax.swing.*;
  90. import java.awt.*;
  91. import java.awt.event.ActionEvent;
  92. import java.awt.event.ActionListener;
  93. import java.awt.event.WindowAdapter;
  94. import java.awt.event.WindowEvent;
  95.  
  96. public class Test extends JFrame {
  97. JButton regButton;
  98. JButton logButton;
  99.  
  100. public Test() {
  101. Client client = new Client();
  102.  
  103. regButton = new JButton("Register");
  104. logButton = new JButton("Login");
  105.  
  106. JPanel panel = new JPanel();
  107. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  108. addWindowListener(new WindowAdapter() {
  109. @Override
  110. public void windowClosing(WindowEvent e) {
  111. super.windowClosing(e);
  112. client.excursions();
  113. System.exit(0);
  114. }
  115. });
  116. panel.add(regButton);
  117. panel.add(logButton);
  118. getContentPane().add(panel);
  119. regButton.addActionListener(new ActionListener() {
  120. @Override
  121. public void actionPerformed(ActionEvent e) {
  122. client.clientOptions(1);
  123. }
  124. });
  125. logButton.addActionListener(new ActionListener() {
  126. @Override
  127. public void actionPerformed(ActionEvent e) {
  128. client.clientOptions(5);
  129. }
  130. });
  131.  
  132. setSize(new Dimension(500, 400));
  133. setTitle("Welcome!");
  134. setVisible(true);
  135.  
  136.  
  137. }
  138.  
  139. public static void main(String[] args) {
  140. Test t = new Test();
  141. }
  142. }
  143.  
  144. ========================================================================
  145.  
  146. import java.io.IOException;
  147. import java.net.ServerSocket;
  148. import java.net.Socket;
  149. public class Main {
  150. public static void main(String[] args) throws IOException {
  151. try {
  152. int portNumber = 40450;
  153. ServerSocket serverSocket = new ServerSocket(40450);
  154. while (true) {
  155. Socket clientSocket = serverSocket.accept();
  156. DataServer dataserver = new DataServer(clientSocket);
  157. dataserver.start();
  158. }
  159. }
  160. catch(Exception e){
  161. System.out.println("Exception: "+e.getMessage());
  162. }
  163. }
  164. }
  165. =======================================================================
  166.  
  167. import java.io.BufferedReader;
  168. import java.io.IOException;
  169. import java.io.InputStreamReader;
  170. import java.io.PrintWriter;
  171. import java.net.Socket;
  172. import java.sql.*;
  173. import java.util.ArrayList;
  174.  
  175. //Database server
  176. public class DataServer extends Thread {
  177. private String cabinNo;
  178. private Socket socket;
  179. private BufferedReader readInput;
  180. private PrintWriter writeOutput;
  181. private static ArrayList <String> usersOnline = new ArrayList<>();
  182. private static ArrayList <ArrayList> cruises = new ArrayList<>();
  183. private static ArrayList <String> cruise10 = new ArrayList<>();
  184. private static ArrayList <String> cruise20 = new ArrayList<>();
  185.  
  186. public DataServer(Socket socket) {
  187. this.socket = socket;
  188. }
  189. @Override
  190. public void run() {
  191. boolean b = true;
  192. while (b) {
  193. try {
  194.  
  195. //Declare new reader and writer to communicate with the client
  196. readInput = new BufferedReader((new InputStreamReader(socket.getInputStream())));
  197. writeOutput = new PrintWriter(socket.getOutputStream(), true);
  198. String storeInput = readInput.readLine();
  199. if(storeInput != null) {
  200. connectToDb(storeInput);
  201. }
  202. socket.getInputStream().read();
  203. //b = false;
  204. }
  205. catch (IOException e) {
  206. System.out.println("IO exception: "+e.getMessage());
  207. }
  208. catch (NullPointerException e){
  209. System.out.println("nullpointer exception: "+e.getMessage());
  210. }
  211. catch (SQLException e){
  212. System.out.println("SQL Exception: "+e.getMessage());
  213. }
  214. }
  215. }
  216.  
  217. //Method that takes the readInput string and sorts it into a String array in String chunks
  218. public String [] processInput(String input){
  219. String [] processedInput = input.split(",");
  220. return processedInput;
  221. }
  222. //Method that checks if a user account is logged in
  223. public boolean checkUsersOnline(String input){
  224. for(int i = 0; i < usersOnline.size(); i++){
  225. //System.out.println(usersOnline.get(i));
  226. if(usersOnline.get(i).equals(input)){//readInput.equals(usersOnline.get(i))
  227. System.out.println("input is: "+input);
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. //Connection method that connects to a DB and performs tasks on behalf of a client
  234. public void connectToDb(String input) throws SQLException{
  235. //First line takes a String and cuts it into chunks using processInput method
  236. //The first chunk in every message is a key that is later used by a switch statement
  237. //The key is essential for the program to know which action to perform
  238.  
  239. String [] storeInput = processInput(input);
  240.  
  241. String username = "Client";
  242. String password = "ClientAccess";
  243. String databaseName = "Cruise";
  244. String databasePath = "jdbc:mysql://localhost:3306/"+databaseName+"?autoReconnect=true&useSSL=false";
  245.  
  246. //String schoolDataBase = studentnet.cst.beds.ac.uk; //school DB
  247. //localhost:3306 //local DB
  248.  
  249. //Try to make a connection to DB
  250. java.sql.Connection connection = null;
  251. Statement statement = null;
  252. ResultSet results = null;
  253. ResultSet results2=null;
  254.  
  255. try {
  256. Class.forName("com.mysql.jdbc.Driver");
  257. connection = DriverManager.getConnection(databasePath, username, password);
  258. statement = connection.createStatement();
  259.  
  260. //Creating a switch statement that processes the incoming message from the client
  261. //Using the value of the first element of the Array, the switch statement takes appropriate action
  262. switch (storeInput[0]) {
  263. case "1":
  264. //Register user
  265. //statement.executeUpdate("INSERT INTO customer (cabinNo, email, userpw) VALUE ("+storeInput[1]+",'"+storeInput[2]+"','"+storeInput[3]+"');");
  266. boolean exists = false;
  267. results = statement.executeQuery("SELECT * FROM customers;");
  268. while(results.next()) {
  269. if (storeInput[1].equals(results.getString("cabinNo"))) {
  270. System.out.println("Cabin number has already been used");
  271. exists = true;
  272. }
  273. }
  274. if(!exists) {
  275. PreparedStatement prepStatementRegister = connection.prepareStatement
  276. ("INSERT INTO customers (cabinNo, email, userpw, selectedCruise) VALUE (?, ?, ?, ?)");
  277. prepStatementRegister.setString(1, storeInput[1]);
  278. prepStatementRegister.setString(2, storeInput[2]);
  279. prepStatementRegister.setString(3, storeInput[3]);
  280. prepStatementRegister.setString(4, storeInput[4]);
  281. prepStatementRegister.executeUpdate();
  282. System.out.println("Cabin registered successfully.");
  283. }
  284. break;
  285. case "2":
  286. //Log in
  287. //PreparedStatement prepStatementLogIn = connection.prepareStatement("SELECT * FROM customer;");
  288. results = statement.executeQuery("SELECT * FROM customer;");
  289. while(results.next()){
  290. if(storeInput[1].equals(results.getString("cabinNo"))){
  291. if(storeInput[2].equals(results.getString("userpw")) && checkUsersOnline(storeInput[1])==false){
  292. System.out.println("val: "+storeInput[1]);
  293. usersOnline.add(storeInput[1]);
  294. for(int x= 0; x < usersOnline.size();x++){
  295. System.out.println(usersOnline.get(x));
  296. }
  297. System.out.println("logged in");
  298. }
  299. else{
  300. System.out.println("user already online");
  301. }
  302. }
  303. }
  304. break;
  305. case "3":
  306. //Log out
  307. cabinNo = storeInput[1];
  308. usersOnline.remove(cabinNo);
  309. System.out.println("User logged out");
  310. break;
  311. case "4":
  312. //View available excursions
  313. String cruiseId;
  314. results = statement.executeQuery("SELECT * FROM customers WHERE cabinNo = "+storeInput[1]);
  315. while(results.next()) {
  316. cruiseId = results.getString(5);
  317. results = statement.executeQuery("SELECT * FROM excursions WHERE cruiseId = " + cruiseId);
  318. while (results.next()) {
  319. System.out.println("Excursion ID - " + results.getString(1) + " | Excursion name - " + results.getString(4));
  320. }
  321. }
  322. break;
  323. case "5":
  324. //Book excursions
  325. String excursionId;
  326. String excursionNames;
  327. int excursionBookings;
  328. results = statement.executeQuery("SELECT * FROM excursions WHERE excursionId = "+storeInput[2]);
  329. while(results.next()){
  330. excursionId = results.getString(1);
  331. excursionNames = results.getString(4);
  332. excursionBookings = results.getInt(5);
  333. if(excursionId.equals(storeInput[2])) {
  334. System.out.println(excursionId);
  335. PreparedStatement updateBookings = connection.prepareStatement("UPDATE excursions SET excursionBookings = ? WHERE excursionId = ?");
  336. updateBookings.setString(1, String.valueOf((excursionBookings + Integer.parseInt(storeInput[3]))));
  337. updateBookings.setString(2, excursionId);
  338. updateBookings.executeUpdate();
  339. PreparedStatement updateCustomerBookings = connection.prepareStatement("UPDATE customers SET bookings = ? WHERE cabinNo = ?");
  340. updateCustomerBookings.setString(1,(excursionId));
  341. updateCustomerBookings.setString(2, storeInput[1]);
  342. System.out.println("updated");
  343. }
  344. else{
  345. System.out.println("Invalid Excursion ID");
  346. }
  347.  
  348. }
  349. //Booking excursions
  350. break;
  351. case "6":
  352. //Update bookings
  353. System.out.println("case 6");
  354. //Updating excursions
  355. }
  356. }
  357. catch(SQLException e){
  358. System.out.println("SQL exception"+e);
  359. }
  360. catch(ClassNotFoundException e){
  361. System.out.println("Class not found exception");
  362. }
  363. catch(ArrayIndexOutOfBoundsException e){
  364. System.out.println("Array Index out of bounds");
  365. }
  366. finally{
  367. if(statement!=null){
  368. try{
  369. statement.close();
  370. }
  371. catch (SQLException e){
  372. System.out.println("Exception at statemnet.close()");
  373. }
  374. }
  375. if(results!=null){
  376. try{
  377. results.close();
  378. }
  379. catch(SQLException e){
  380. System.out.println("Exception at results.close()");
  381. }
  382. }
  383. if(connection!=null){
  384. try{
  385. connection.close();
  386. }
  387. catch(SQLException e){
  388. System.out.println("Exception at connection.close()");
  389. }
  390. }
  391. }
  392. }
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement