Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. //CONTROLLER
  2. public int modifyCustomer(Customer customer) {
  3. int result = -1;
  4. try {
  5. PreparedStatement stmt;
  6. if (customer.hasFirstName() && !customer.hasLastName()) { // Has firstName and does NOT have lastName
  7. stmt = con.prepareStatement("UPDATE Customer SET first_name = ? where customer_ID = ?");
  8. stmt.setString(1, customer.getFirstName());
  9. stmt.setString(2, customer.getLastName());
  10. result = stmt.executeUpdate();
  11. }
  12. } catch (SQLException e) {
  13. e.printStackTrace();
  14. }
  15. return result;
  16. }
  17.  
  18. //USER INTERFACE
  19. public void displayModifyCustomerRecordsMenu() {
  20. Customer cusFilters = new Customer();
  21. boolean validInput = false;
  22. boolean sameMenu = true;
  23. String first_name = "";
  24. String last_name = "";
  25. String address1 = "";
  26. String address2 = "";
  27. int zone_ID = 0;
  28. while (sameMenu) {
  29. System.out.println("Modify Customer Records");
  30. printCurrentCustomerFilters(cusFilters);
  31. System.out.println("4. Reset Filters");
  32. System.out.println("5. Modify using current filters");
  33. System.out.println("6. Back\n");
  34. System.out.print("Enter your choice: ");
  35. if (input.hasNext()) {
  36. int choice = input.nextInt();
  37.  
  38. switch (choice) {
  39. case 1:
  40. while (!validInput) {
  41. System.out.println("First Name or press i to not modify: ");
  42. if(input.hasNext()){
  43. first_name = input.nextLine();
  44. if(first_name != null && first_name != "") {
  45. validInput = true;
  46. cusFilters.setFirstName(first_name);
  47. }
  48. }
  49. }
  50. validInput = false;
  51. break;
  52.  
  53. case 2:
  54. while (!validInput) {
  55. System.out.println("Last Name: ");
  56. if(input.hasNext()){
  57. last_name = input.nextLine();
  58. if(last_name != null && last_name != "") {
  59. validInput = true;
  60. cusFilters.setLastName(last_name);
  61. }
  62. }
  63. }
  64. validInput = false;
  65. break;
  66.  
  67. case 3:
  68. while (!validInput) {
  69. System.out.println("Address 1: ");
  70. if(input.hasNext()){
  71. address1 = input.nextLine();
  72. if(address1 != null && address1 != "") {
  73. validInput = true;
  74. cusFilters.setAddress1(address1);
  75. }
  76. }
  77. }
  78. validInput = false;
  79. break;
  80.  
  81. case 4:
  82. while (!validInput) {
  83. System.out.println("Address 2: ");
  84. if(input.hasNext()){
  85. address2 = input.nextLine();
  86. if(address2 != null && address2 != "") {
  87. validInput = true;
  88. cusFilters.setAddress2(address2);
  89. }
  90. }
  91. }
  92. validInput = false;
  93. break;
  94.  
  95. case 5:
  96. while (!validInput) {
  97. System.out.println("Zone ID: ");
  98. if(input.hasNext()){
  99. zone_ID = input.nextInt();
  100. if(zone_ID >= 0 && zone_ID <= 65535) {
  101. validInput = true;
  102. cusFilters.setZone_ID(zone_ID);
  103. }
  104. }
  105. }
  106. break;
  107. case 6: // Reset Filters
  108. sameMenu = true;
  109. cusFilters = new Customer(); // Re-initialises filters object
  110. System.out.println("Filters Reset!");
  111. break;
  112. case 7: // Modify
  113. int result = controller.modifyCustomer(cusFilters);
  114. if (result != -1) {
  115. System.out.println("Successfully modified " + result + " rows");
  116. } else {
  117. System.out.println("Failed to modify rows.");
  118. }
  119. sameMenu = false;
  120. displayDeliveryMenu(); // Go back to deliveryMenu()
  121. break;
  122. case 8: // Back
  123. sameMenu = false;
  124. displayDeliveryMenu(); // Go back to deliveryMenu()
  125. break;
  126. }
  127. if (choice < 1 || choice > 8) { // TODO: Null & empty checks
  128. System.out.println("Please enter a number between 1 and 8.");
  129. }
  130. } else {
  131. // Clear the input buffer and start again
  132. input.nextLine();
  133. System.out.println("You entered an invalid choice, please try again...");
  134.  
  135. }
  136. // Assuming all fields are valid
  137. controller.addCustomer(cusFilters);
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement