Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CONTROLLER
- public int modifyCustomer(Customer customer) {
- int result = -1;
- try {
- PreparedStatement stmt;
- if (customer.hasFirstName() && !customer.hasLastName()) { // Has firstName and does NOT have lastName
- stmt = con.prepareStatement("UPDATE Customer SET first_name = ? where customer_ID = ?");
- stmt.setString(1, customer.getFirstName());
- stmt.setString(2, customer.getLastName());
- result = stmt.executeUpdate();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- //USER INTERFACE
- public void displayModifyCustomerRecordsMenu() {
- Customer cusFilters = new Customer();
- boolean validInput = false;
- boolean sameMenu = true;
- String first_name = "";
- String last_name = "";
- String address1 = "";
- String address2 = "";
- int zone_ID = 0;
- while (sameMenu) {
- System.out.println("Modify Customer Records");
- printCurrentCustomerFilters(cusFilters);
- System.out.println("4. Reset Filters");
- System.out.println("5. Modify using current filters");
- System.out.println("6. Back\n");
- System.out.print("Enter your choice: ");
- if (input.hasNext()) {
- int choice = input.nextInt();
- switch (choice) {
- case 1:
- while (!validInput) {
- System.out.println("First Name or press i to not modify: ");
- if(input.hasNext()){
- first_name = input.nextLine();
- if(first_name != null && first_name != "") {
- validInput = true;
- cusFilters.setFirstName(first_name);
- }
- }
- }
- validInput = false;
- break;
- case 2:
- while (!validInput) {
- System.out.println("Last Name: ");
- if(input.hasNext()){
- last_name = input.nextLine();
- if(last_name != null && last_name != "") {
- validInput = true;
- cusFilters.setLastName(last_name);
- }
- }
- }
- validInput = false;
- break;
- case 3:
- while (!validInput) {
- System.out.println("Address 1: ");
- if(input.hasNext()){
- address1 = input.nextLine();
- if(address1 != null && address1 != "") {
- validInput = true;
- cusFilters.setAddress1(address1);
- }
- }
- }
- validInput = false;
- break;
- case 4:
- while (!validInput) {
- System.out.println("Address 2: ");
- if(input.hasNext()){
- address2 = input.nextLine();
- if(address2 != null && address2 != "") {
- validInput = true;
- cusFilters.setAddress2(address2);
- }
- }
- }
- validInput = false;
- break;
- case 5:
- while (!validInput) {
- System.out.println("Zone ID: ");
- if(input.hasNext()){
- zone_ID = input.nextInt();
- if(zone_ID >= 0 && zone_ID <= 65535) {
- validInput = true;
- cusFilters.setZone_ID(zone_ID);
- }
- }
- }
- break;
- case 6: // Reset Filters
- sameMenu = true;
- cusFilters = new Customer(); // Re-initialises filters object
- System.out.println("Filters Reset!");
- break;
- case 7: // Modify
- int result = controller.modifyCustomer(cusFilters);
- if (result != -1) {
- System.out.println("Successfully modified " + result + " rows");
- } else {
- System.out.println("Failed to modify rows.");
- }
- sameMenu = false;
- displayDeliveryMenu(); // Go back to deliveryMenu()
- break;
- case 8: // Back
- sameMenu = false;
- displayDeliveryMenu(); // Go back to deliveryMenu()
- break;
- }
- if (choice < 1 || choice > 8) { // TODO: Null & empty checks
- System.out.println("Please enter a number between 1 and 8.");
- }
- } else {
- // Clear the input buffer and start again
- input.nextLine();
- System.out.println("You entered an invalid choice, please try again...");
- }
- // Assuming all fields are valid
- controller.addCustomer(cusFilters);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement