完成任务后返回主菜单方法
我的所有代码都可以在我的控制台上正常运行。我能够完美地选择、更新、删除和查看所有内容。但是,在用户完成任何任务后,我希望从 mainMenuOptions() 获得原始主菜单,以便从头开始。目前它正在将我循环回到accountsTable() 方法。有什么想法吗?
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));
}
}
}
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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果代码将您循环到accountsTable 方法,则您似乎输入了“1”。
现在假设您执行了一次,浏览 accountTable 然后返回,您会期望显示菜单。我怀疑这确实正在发生。
但随后您的代码立即认为再次输入了“1”。我相信无论出于何种原因,它仍然在输入缓冲区中。
为了摆脱这个问题,请在显示菜单之后和读取输入之前清空缓冲区。查看如何清除 Java 中的扫描仪缓冲区?
编辑:在你的accountsTable方法中我可以看到你已经尝试过类似的事情了。在主菜单级别执行相同的操作。
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.