Guest User

Untitled

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.14 KB | None | 0 0
  1. public class DataBase {
  2. private Connection connection;
  3. private Statement statement;
  4. private PreparedStatement preparedStatement;
  5. private ResultSet result;
  6.  
  7. public DataBase() throws SQLException {
  8. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentalcar?autoReconnect=true&serverTimezone=" + TimeZone.getDefault().getID(), "root", "...");
  9. statement = connection.createStatement();
  10. }
  11.  
  12. public void insertNewCustomer(Client client) throws SQLException {
  13. preparedStatement = connection.prepareStatement("insert into client" + "(namee, surname, street,houseNumber,city,peselNumber,rentDate, clientNumber)" + "values(?,?,?,?,?,?,?,?)");
  14.  
  15. preparedStatement.setString(1, client.getName());
  16. preparedStatement.setString(2, client.getSurname());
  17. preparedStatement.setString(3, client.getStreet());
  18. preparedStatement.setInt(4, client.getHouseNumber());
  19. preparedStatement.setString(5, client.getCity());
  20. preparedStatement.setLong(6, client.getPeselNumber());
  21. preparedStatement.setString(7, client.getRentDate());
  22. preparedStatement.setInt(8, client.getClientNumber());
  23.  
  24. preparedStatement.executeUpdate();
  25. }
  26.  
  27. public void insertNewCar(Car car) throws SQLException {
  28. preparedStatement = connection.prepareStatement("insert into car" + "(brand, productionYear, engineCapacity,dayPrice,available)" + "values(?,?,?,?,?)");
  29.  
  30. preparedStatement.setString(1, car.getBrand());
  31. preparedStatement.setString(2, car.getProductionYear());
  32. preparedStatement.setString(3, car.getEngineCapacity());
  33. preparedStatement.setInt(4, car.getDayPrice());
  34. preparedStatement.setString(5, car.getAvailable());
  35.  
  36. preparedStatement.executeUpdate();
  37. }
  38.  
  39. public void rentACar(RentingACar rentingACar) throws SQLException {
  40. int count = 0;
  41. boolean isAvailable = true;
  42. {
  43. preparedStatement = connection.prepareStatement("SELECT COUNT(0) FROM car WHERE available='1' AND brand=?");
  44. preparedStatement.setString(1, rentingACar.getBrand());
  45. result = preparedStatement.executeQuery();
  46. }
  47. while (result.next()) {
  48. count = result.getInt(1);
  49. }
  50. if (count < 1)
  51. isAvailable = false;
  52.  
  53. if (isAvailable) {
  54. preparedStatement = connection.prepareStatement("insert into rentcar" + "(brand,namee,surname,rentDate,clientNumber)" + "values(?,?,?,?,?)");
  55. preparedStatement.setString(1, rentingACar.getBrand());
  56. preparedStatement.setString(2, rentingACar.getName());
  57. preparedStatement.setString(3, rentingACar.getSurname());
  58. preparedStatement.setString(4, rentingACar.getRentDate());
  59. preparedStatement.setInt(5, rentingACar.getClientNumber());
  60. preparedStatement.executeUpdate();
  61.  
  62. preparedStatement = connection.prepareStatement("update car " + " set available='0'" + " where brand= ? ");
  63. preparedStatement.setString(1, rentingACar.getBrand());
  64. preparedStatement.executeUpdate();
  65. System.out.println("Car was rented!");
  66. } else {
  67. System.out.println("There is no " + rentingACar.getBrand() + " in our car or all types of this car are rented!");
  68. }
  69. }
  70.  
  71. public void returnACar(Car car) throws SQLException {
  72. preparedStatement = connection.prepareStatement("DELETE from rentcar WHERE brand=? AND clientNumber=?");
  73. preparedStatement.setString(1, car.getBrand());
  74. preparedStatement.setInt(2, car.getClientNumber());
  75. preparedStatement.executeUpdate();
  76.  
  77. preparedStatement = connection.prepareStatement("update car " + " set available='1'" + " where brand=?");
  78. preparedStatement.setString(1, car.getBrand());
  79. preparedStatement.executeUpdate();
  80. }
  81.  
  82. public void makeCarUnavailable(Car car) throws SQLException {
  83. int count = 0;
  84. boolean isAvailable = true;
  85. {
  86. preparedStatement = connection.prepareStatement("SELECT COUNT(0) FROM car WHERE brand=? AND productionYear=? ");
  87. preparedStatement.setString(1, car.getBrand());
  88. preparedStatement.setString(2, car.getProductionYear());
  89. result = preparedStatement.executeQuery();
  90. }
  91. while (result.next()) {
  92. count = result.getInt(1);
  93. }
  94. if (count < 1)
  95. isAvailable = false;
  96.  
  97. if (isAvailable) {
  98. preparedStatement = connection.prepareStatement("update car " + " set available='0'" + " where brand=? AND productionYear=?");
  99. preparedStatement.setString(1, car.getBrand());
  100. preparedStatement.setString(2, car.getProductionYear());
  101. preparedStatement.executeUpdate();
  102. System.out.println(car.getBrand() + " was made unavailable");
  103. } else {
  104. System.out.println("No " + car.getBrand() + " in system!");
  105. }
  106. }
  107.  
  108. public void makeCarAvailable(Car car) throws SQLException {
  109. int count = 0;
  110. boolean isAvailable = true;
  111. {
  112. preparedStatement = connection.prepareStatement("SELECT COUNT(0) FROM car WHERE brand=? AND productionYear=? ");
  113. preparedStatement.setString(1, car.getBrand());
  114. preparedStatement.setString(2, car.getProductionYear());
  115. result = preparedStatement.executeQuery();
  116. }
  117. while (result.next()) {
  118. count = result.getInt(1);
  119. }
  120. if (count < 1)
  121. isAvailable = false;
  122.  
  123. if (isAvailable) {
  124. preparedStatement = connection.prepareStatement("update car " + " set available='1'" + " where brand=? AND productionYear=?");
  125. preparedStatement.setString(1, car.getBrand());
  126. preparedStatement.setString(2, car.getProductionYear());
  127. preparedStatement.executeUpdate();
  128. System.out.println(car.getBrand() + " was made unavailable");
  129. } else {
  130. System.out.println("No " + car.getBrand() + " in system!");
  131. }
  132. }
  133.  
  134. public void populateTableViewCars(Car car) throws SQLException {
  135. preparedStatement = connection.prepareStatement("SELECT * FROM car WHERE dayPrice > ?");
  136. preparedStatement.setDouble(1, car.getDayPrice());
  137. result = preparedStatement.executeQuery();
  138.  
  139. while (result.next()) {
  140.  
  141. String brand = result.getString("brand");
  142. String productionYear = result.getString("productionYear");
  143. String engineCapacity = result.getString("engineCapacity");
  144. String dayPrice = result.getString("dayPrice");
  145. String available = result.getString("available");
  146. System.out.println("----------------------------");
  147. System.out.printf("Brand:" + brand + "nEngine Capacity:" + engineCapacity + "nDayPrice:" + dayPrice + "nProduction Year:" + productionYear + "navailable:" + available + "n");
  148. System.out.println("----------------------------");
  149. }
  150. }
  151.  
  152.  
  153. public void populateTableRent(Client client) throws SQLException {
  154. preparedStatement = connection.prepareStatement("SELECT * FROM rentcar WHERE clientNumber=?");
  155. preparedStatement.setInt(1, client.getClientNumber());
  156. result = preparedStatement.executeQuery();
  157.  
  158. while (result.next()) {
  159.  
  160. String brand = result.getString("brand");
  161. String name = result.getString("namee");
  162. String surname = result.getString("surname");
  163. String rentDate = result.getString("rentDate");
  164. System.out.println("----------------------------");
  165. System.out.printf("Brand:" + brand + "nName:" + name + "nSurname:" + surname + "nDate of rental:" + rentDate + "n");
  166. System.out.println("----------------------------");
  167. }
  168. }
  169.  
  170. public void populateTableViewClients() throws SQLException {
  171. String sql = "SELECT * FROM `client`";
  172. result = statement.executeQuery(sql);
  173.  
  174. while (result.next()) {
  175.  
  176. String namee = result.getString("namee");
  177. String surname = result.getString("surname");
  178. String street = result.getString("street");
  179. int houseNumber = result.getInt("houseNumber");
  180. long peselNumber = result.getLong("peselNumber");
  181. String rentDate = result.getString("rentDate");
  182. System.out.println("----------------------------");
  183. System.out.printf("Name:" + namee + "nSurname:" + surname + "nStreet:" + street + "nNumber of house:" + houseNumber + "nPesel number:" + peselNumber + "nDate of rental:" + rentDate + "n");
  184. System.out.println("----------------------------");
  185. }
  186. }
  187. }
  188.  
  189. public class WorkerDataGetter {
  190. private Scanner input = new Scanner(System.in);
  191.  
  192. public Car createCar() {
  193. Car car = new Car();
  194.  
  195. System.out.print("Brand: ");
  196. car.setBrand(input.next());
  197. System.out.print("Day price: ");
  198. car.setDayPrice(input.nextInt());
  199. System.out.print("Engine Capcity: ");
  200. car.setEngineCapacity(input.next());
  201. System.out.print("Production year: ");
  202. car.setProductionYear(input.next());
  203. System.out.print("available: ");
  204. car.setAvailable(input.next());
  205.  
  206. return car;
  207. }
  208.  
  209. public Car makeCarUnavailable() {
  210. Car car = new Car();
  211.  
  212. System.out.print("Brand: ");
  213. car.setBrand(input.next());
  214. System.out.print("production year: ");
  215. car.setProductionYear(input.next());
  216.  
  217. return car;
  218. }
  219.  
  220. public Car makeCarAavailable() {
  221. Car car = new Car();
  222.  
  223. System.out.print("Brand: ");
  224. car.setBrand(input.next());
  225. System.out.print("Production year : ");
  226. car.setProductionYear(input.next());
  227.  
  228. return car;
  229. }
  230. }
  231.  
  232. public class ClientDataGetter {
  233. private Scanner input = new Scanner(System.in);
  234. private Random rand = new Random();
  235.  
  236. public Client createClient() {
  237. Client client = new Client();
  238. client.setClientNumber(rand.nextInt(999));
  239.  
  240. System.out.print("name: ");
  241. client.setName(input.next());
  242. System.out.print("surname: ");
  243. client.setSurname(input.next());
  244. System.out.print("city: ");
  245. client.setCity(input.next());
  246. System.out.print("house number: ");
  247. client.setHouseNumber(input.nextInt());
  248. System.out.print("street: ");
  249. client.setStreet(input.next());
  250. System.out.print("pesel number: ");
  251. client.setPeselNumber(input.nextLong());
  252. System.out.print("rent date: ");
  253. client.setRentDate(input.next());
  254. System.out.println("Your client number is: " + client.getClientNumber());
  255.  
  256. return client;
  257. }
  258.  
  259. public RentingACar rentACar() {
  260. RentingACar rentingACar = new RentingACar();
  261.  
  262. System.out.print("Brand: ");
  263. rentingACar.setBrand(input.next());
  264. System.out.print("Name: ");
  265. rentingACar.setName(input.next());
  266. System.out.print("Surname: ");
  267. rentingACar.setSurname(input.next());
  268. System.out.print("Rent Date: ");
  269. rentingACar.setRentDate(input.next());
  270. System.out.print("Client number: ");
  271. rentingACar.setClientNumber(input.nextInt());
  272.  
  273. return rentingACar;
  274. }
  275.  
  276. public Car populateTableViewCars() {
  277. Car car = new Car();
  278.  
  279. System.out.println("Input minimum price per day. If you want to display all cars - input 0.nMinimum price: ");
  280. car.setDayPrice(input.nextInt());
  281.  
  282. return car;
  283. }
  284.  
  285. public Client populateTableRent() {
  286. Client client = new Client();
  287.  
  288. System.out.println("Input your client number: ");
  289. client.setClientNumber(input.nextInt());
  290.  
  291. return client;
  292. }
  293.  
  294. public Car returnACar() {
  295. Car car = new Car();
  296.  
  297. System.out.println("Input brand of car that you want to return: ");
  298. car.setBrand(input.next());
  299. System.out.println("Input your client number, otherwise car won't be removed from our DataBase!");
  300. car.setClientNumber(input.nextInt());
  301.  
  302. return car;
  303. }
  304. }
  305.  
  306. public class CarRentalOptions {
  307. private DataBase dataBase = new DataBase();
  308.  
  309. CarRentalOptions() throws SQLException {
  310. }
  311.  
  312. void createNewCustomer(Client client) throws SQLException {
  313. dataBase.insertNewCustomer(client);
  314.  
  315. System.out.println("Client added successfully!");
  316. }
  317.  
  318. void createNewCar(Car car) throws SQLException {
  319. dataBase.insertNewCar(car);
  320.  
  321. System.out.println("Car added successfully!");
  322. }
  323.  
  324. void makeCarUnavailable(Car car) throws SQLException {
  325. dataBase.makeCarUnavailable(car);
  326. }
  327.  
  328. void makeCarAvailable(Car car) throws SQLException {
  329. dataBase.makeCarAvailable(car);
  330. }
  331.  
  332. void rentACar(RentingACar rentingACar) throws SQLException {
  333. dataBase.rentACar(rentingACar);
  334. }
  335.  
  336. void populateTableViewCars(Car car) throws SQLException {
  337. dataBase.populateTableViewCars(car);
  338. }
  339.  
  340. void populateTableRent(Client client) throws SQLException {
  341. dataBase.populateTableRent(client);
  342. }
  343.  
  344. void populateTableViewClients() throws SQLException {
  345. dataBase.populateTableViewClients();
  346. }
  347.  
  348. void returnACar(Car car) throws SQLException {
  349. dataBase.returnACar(car);
  350. }
  351. }
  352.  
  353. public class CarRentalEngine {
  354. private int option;
  355. private Scanner input = new Scanner(System.in);
  356. private CarRentalOptions carRentalOptions = new CarRentalOptions();
  357. private ClientDataGetter clientDataGetter = new ClientDataGetter();
  358. private WorkerDataGetter workerDataGetter = new WorkerDataGetter();
  359.  
  360. CarRentalEngine() throws SQLException {
  361. }
  362.  
  363. void startCarRental() throws SQLException {
  364. System.out.println("Who are you?n1. Customern2. Worker");
  365. try {
  366. switch (input.nextInt()) {
  367. case 1:
  368. executeClientCase();
  369. break;
  370. case 2:
  371. executeWorkerCase();
  372. break;
  373. }
  374. } catch (InputMismatchException e) {
  375. System.err.println("Your input is wrong!");
  376. }
  377. }
  378.  
  379.  
  380. private void executeOptionsForClient(int option) throws SQLException {
  381. switch (option) {
  382. case 1:
  383. carRentalOptions.rentACar(clientDataGetter.rentACar());
  384. break;
  385. case 2:
  386. carRentalOptions.returnACar(clientDataGetter.returnACar());
  387. break;
  388. case 3:
  389. carRentalOptions.populateTableRent(clientDataGetter.populateTableRent());
  390. break;
  391. case 4:
  392. carRentalOptions.populateTableViewCars(clientDataGetter.populateTableViewCars());
  393. break;
  394. case 5:
  395. break;
  396. }
  397. }
  398.  
  399.  
  400. private void executeOptionsForWorker(int option) throws SQLException {
  401. switch (option) {
  402. case 1:
  403. carRentalOptions.populateTableViewClients();
  404. break;
  405. case 2:
  406. carRentalOptions.populateTableViewCars(clientDataGetter.populateTableViewCars());
  407. break;
  408. case 3:
  409. carRentalOptions.makeCarAvailable(workerDataGetter.makeCarAavailable());
  410. break;
  411. case 4:
  412. carRentalOptions.makeCarUnavailable(workerDataGetter.makeCarUnavailable());
  413. break;
  414. case 5:
  415. carRentalOptions.createNewCar(workerDataGetter.createCar());
  416. case 6:
  417. break;
  418. }
  419. }
  420.  
  421.  
  422. private void executeClientCase() throws SQLException {
  423. System.out.println("1. Have you inputted your data before?nN/Y: ");
  424. if (input.next().toUpperCase().equals("N")) {
  425. carRentalOptions.createNewCustomer(clientDataGetter.createClient());
  426. System.out.println("Now you have your unique number clinet, use it where it is required!");
  427. } else {
  428. do {
  429. System.out.println("What do you want to do?");
  430. System.out.println("1. Rent a carn2. Return a carn3. Populate rented carsn4. Populate carsn5. Quit");
  431. option = input.nextInt();
  432. executeOptionsForClient(option);
  433. }
  434. while (option != 5);
  435. }
  436. }
  437.  
  438. private void executeWorkerCase() throws SQLException {
  439. do {
  440. System.out.println("What do you want to do?");
  441. System.out.println("1. Populate clientsn2. Populate carsn3. Make car availablen4. Make car unavailablen5. Insert new carn6. Quit");
  442. option = input.nextInt();
  443. executeOptionsForWorker(option);
  444. }
  445. while (option != 6);
  446. }
  447. }
Add Comment
Please, Sign In to add comment