0

All my code works well with my console. I am able to select, update, delete, and view everything perfectly fine. However, after I have the user complete any task I would like to have the original main menu from the mainMenuOptions() in order to start from the beginning. At the moment it is looping me back to the accountsTable() method. Any thoughts?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
import java.sql.PreparedStatement;
import java.sql.*;
import static java.lang.System.out;

public class Menu {

    static boolean exit;
    static final String DB_URL = "jdbc:mysql://localhost:3306/chocolatemilk_review";
    static final String USER = "root";
    static final String PASS = "";

    private static Connection connection = null;
    static Scanner input = new Scanner(System.in);

    public static void main (String [] args) throws SQLException {

        mainMenuOptions();
        input.close();

    }

    public static Connection connect() throws SQLException {

        return DriverManager.getConnection(DB_URL, USER, PASS);

    }

    public static void mainMenuOptions() {

        int numEntered;

        try { 

            do {

                out.println("Choconnoisseur Menu");
                out.println();
                out.println("Enter 1 for Accounts ");
                out.println("Enter 2 for Merchandise ");
                out.println("Enter 3 for Merchsales ");
                out.println("Enter 4 for Product Reviews ");
                out.println("Enter 5 to Exit"); 
                out.println();
                out.println("Enter number here: ");
                numEntered = input.nextInt(); 
                input.nextLine();

                switch(numEntered) {
                case 1: 
                    accountsTable();
                    out.println("Account Options");
                    break;
                case 2: 
                    merchandiseTable();
                    out.println("Merchandise Options");
                    out.println();
                    break;
                case 3: 
                    merchsalesTable();
                    out.println("Merchsales Options");
                    out.println();
                    break;
                case 4: 
                    productReviewsTable();
                    out.println("Product Reviews Options");
                    out.println();
                    break;
                case 5: 
                    exit = true;
                    out.println("Have a nice day! "); 
                    out.println();
                    break;
                default:
                    out.println("Invalid entry, choose from one the options listed.");
                    out.println();
                    break;
                }

            }
            while (numEntered != 5);  
        } 
        catch (Exception e) { 
            out.println("Invalid entry");
            e.printStackTrace();
        }
    }

    public static void accountsTable() throws SQLException  {

        int accountOpts;

        try { 

            do {

                out.println("Accounts Table");
                out.println();
                out.println("Enter 1 for Add Account");
                out.println("Enter 2 for Edit Account");
                out.println("Enter 3 for  Remove Account");
                out.println("Enter 4 for View Account");
                out.println("Enter 5 back to Main Menu"); 
                out.println();
                out.println("Enter number here: ");
                accountOpts = input.nextInt();
                input.nextLine();
                out.println();

                switch (accountOpts) {
                case 1: 
                    out.println("Add Accounts");
                    insertInfo();

                    break;
                case 2: 
                    out.println("Edit Accounts");
                    updateAccounts();
                    out.println();
                    break;
                case 3: 
                    out.println("Remove Accounts");
                    deleteAccounts(); 
                    out.println();
                    break;
                case 4: 
                    out.println("View Accounts");
                    selectAccounts();    
                    out.println();
                    break;
                case 5: 
                    out.println("Back to Main Menu ");
                    out.println();
                    break; 
                default:
                    out.println("Invalid entry, choose from one the options listed.");
                    out.println();
                    break;
                }
            }
            while (accountOpts != 5);  
        } 
        catch (Exception e) { 
            out.println("Invalid entry");
            e.printStackTrace();
        }
    }

    private static void insertInfo() throws SQLException   {

        out.println();

        connection = connect();

        String SQL = "INSERT INTO Accounts(last, first, address, city, state, email) VALUES(?,?,?,?,?,?)";
        PreparedStatement prepInsertStatement = connection.prepareStatement(SQL);

        out.println("Enter last name: ");
        String lastName = input.nextLine();
        prepInsertStatement.setString(1, lastName);

        out.println("Enter first name: ");
        String firstName = input.nextLine();
        prepInsertStatement.setString(2, firstName);

        out.println("Enter address: ");
        String address = input.nextLine();
        prepInsertStatement.setString(3, address);

        out.println("Enter city: ");
        String city = input.nextLine();
        prepInsertStatement.setString(4,  city);

        out.println("Enter state: ");
        String state = input.nextLine();
        prepInsertStatement.setString(5, state);

        out.println("Enter email: ");
        String email = input.nextLine();
        prepInsertStatement.setString(6, email);

        int result = prepInsertStatement.executeUpdate();
        if(result == 1)
            out.println("Account added. ");
        else
            out.println("Account not added. ");
        out.println();

    }

    private static void updateAccounts() throws SQLException {

        out.println();

        connection = connect();

        String SQL = "UPDATE Accounts SET last = ?, first = ?, address = ?, city = ?, state = ?, "
                + "email = ? WHERE account_id = ?";
        PreparedStatement prepUpdateStatement = connection.prepareStatement(SQL);

        out.println("Enter Account to Update: ");
        int accountUpdate = input.nextInt();
        prepUpdateStatement.setInt(7, accountUpdate);
        //how come when int comes first both update last and first come at same time in console?

        out.println("Update last name: ");
        String lastName = input.nextLine();
        prepUpdateStatement.setString(1, lastName);

        out.println("Update first name: ");
        String firstName = input.nextLine();
        prepUpdateStatement.setString(2, firstName);

        out.println("Update address: ");
        String address = input.nextLine();
        prepUpdateStatement.setString(3, address);

        out.println("Update city: ");
        String city = input.nextLine();
        prepUpdateStatement.setString(4,  city);

        out.println("Update state: ");
        String state = input.nextLine();
        prepUpdateStatement.setString(5, state);

        out.println("Update email: ");
        String email = input.nextLine();
        prepUpdateStatement.setString(6, email);

        int updateRow = prepUpdateStatement.executeUpdate();
        if(updateRow == 1)
            out.println("Update Successful. ");
        else
            out.println("Update Not Successful. ");

        //why 1 for updateRow == 1?

    }

    private static void deleteAccounts() throws SQLException  {

        out.println();

        connection = connect();

        String SQL = "DELETE FROM Accounts WHERE account_id = ? ";
        PreparedStatement prepDeleteStatement = connection.prepareStatement(SQL);

        out.println("Enter an account_id to remove account: "); 
        int enterAccount = input.nextInt();
        prepDeleteStatement.setInt(1, enterAccount);

        int deleteAccount = prepDeleteStatement.executeUpdate();
        if(deleteAccount == 1)
            out.println("Account deleted. ");
        else
            out.println("Account doesn't exist or not deleted. ");


    }

    private static void selectAccounts() throws SQLException  {

        out.println();

        connection = connect();

        String SQL = "SELECT * FROM accounts WHERE account_id = ? ";

        PreparedStatement prepSelectStatement = connection.prepareStatement(SQL);    

        out.println("Enter account ID to review account: ");
        int accountId = input.nextInt();
        prepSelectStatement.setInt(1, accountId);
        out.println();

        ResultSet rsViewAccount = prepSelectStatement.executeQuery();
        if(!rsViewAccount.isBeforeFirst()) {

            out.println("No rows in Accounts");
        }

        else {

            while (rsViewAccount.next()) {

                out.println("Enter 1 for last name: " + rsViewAccount.getString(2));
                out.println("Enter 2 for first name: " + rsViewAccount.getString(3));
                out.println("Enter 3 for address: " + rsViewAccount.getString(4));
                out.println("Enter 4 for city: " + rsViewAccount.getString(5));
                out.println("Enter 5 for state: " + rsViewAccount.getString(6));
                out.println("Enter 6 email: " + rsViewAccount.getString(7));

            }
        }
    }

1 Answers1

0

If the code loops you to the accountsTable method it seems you entered '1'. Now assuming you do that once, go through accountTable and come back, you'd expect the menu to be shown. I suspect exactly that is happening.

But then your code immediately believes that again a '1' was entered. I believe that is still in the input buffer - for whatever reason.

To get rid of that, empty the buffer after you display the menu and before you read input. Check out How can I clear the Scanner buffer in Java?

Edit: inside your accountsTable method I can see you tried something like this already. Do the same on the main menu level.

Queeg
  • 7,748
  • 1
  • 16
  • 42