如何在java

时间:2018-04-09 11:43:08

标签: java

我刚刚创建了一个循环的senario,我必须选择从数据库中选择的列,例如这个带有senario的javaMain

package myrestorderproject;

import enitities.Menu;
import enitities.Tables;
import java.awt.List;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class MyRestOrderProject {

    private static Scanner input;

    public static void main(String[] args) {
        // TODO code application logic here
        input = new Scanner(System.in);
        System.out.println("*-*-*-*-*-*Welcome to MyRestaurant*-*-*-*-*-*\n");
        System.out.println("Please Choose Table From Tables List");
        Tables t = new Tables();
        t.getAllRows();
        ArrayList<String> listItem = new ArrayList<String>();
        boolean orderNotFinished = true;

        while (orderNotFinished) {

            System.out.print("Enter Table Number: ");
            String tableNumber = input.nextLine();

            boolean insertedTableNumber = db.goTodataBase.checkTableNumber(tableNumber);
            if (insertedTableNumber) {
                System.out.println("You Choose Table Number: " + tableNumber);
                Menu m = new Menu();
                m.getAllRows();
                while (orderNotFinished) {

                    System.out.println("Please Choose Item From Menu List");

                    input = new Scanner(System.in);
                    String itemChosen = input.nextLine();
                    boolean insertedMenuItemId = db.goTodataBase.checkMenuItemInDB(itemChosen);
                    if (insertedMenuItemId) {
                        System.out.println("You Choose Item ID: " + itemChosen);
                        listItem.add(m.getAllRows(itemChosen));
                        System.out.print("Do you need to add more Items ? ");
                        String hasFinished = input.nextLine();
                        orderNotFinished = hasFinished.equals("yes");

                    } else {
                        System.out.println("Item Chosen doen't exist");
                    }

                }
            } else {
                System.out.println("Table number does not exist");
            }
        }

    }

}

我现在需要在选择合适的项目之后打印“请从菜单列表中选择项目”的部分关闭while循环,我还需要选择多个项目,如果我从菜单中选择项目给我从菜单表中选择了详细信息 喜欢如果我选择商品ID 1 +商品ID 2 +商品ID 3表示您已选择 项目1蔬菜Pakora 20.00 veg起动器 项目1蔬菜Pakora 20.00 veg起动器 项目1 Chicken Tikka 20.00非蔬菜初学者 之后退出while循环

因为每列都有ID,名称,价格,类型和类别 和以前在Senario中使用的方法

public static boolean checkMenuItemInDB(String menuId) {
    try {
        setConnection();
        Statement stmt = con.createStatement();
        String strCheck = "select * from menu where "
                + "Menu_Id=" + menuId;
        stmt.executeQuery(strCheck);
        while (stmt.getResultSet().next()) {                
            return true;
        }
    } catch (Exception e) {
    }
    return false;
}

这是菜单类

package enitities;

import javax.swing.JTable;

/**
 *
 * @author DELL
 */
public class Menu {
    private int Menu_Id;
    private String Name;
    private float Price;
    private String Type;
    private String Category;

    public int getMenu_Id() {
        return Menu_Id;
    }

    public void setMenu_Id(int Menu_Id) {
        this.Menu_Id = Menu_Id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public float getPrice() {
        return Price;
    }

    public void setPrice(float Price) {
        this.Price = Price;
    }

    public String getType() {
        return Type;
    }

    public void setType(String Type) {
        this.Type = Type;
    }

    public String getCategory() {
        return Category;
    }

    public void setCategory(String Category) {
        this.Category = Category;
    }
        public void getAllRows() {
        db.goTodataBase.printData("menu");
    }

        public String getAllRows(String itemChosen) {
        db.goTodataBase.printData("menu");
        return itemChosen;
    }
}

这是我在getAllRows

中调用的方法
    public static void printData(String tableNameOrSelectStatement) {
        try {
            setConnection();
            Statement stmt = con.createStatement();
            ResultSet rs;
            String strSelectPart = tableNameOrSelectStatement.substring(0, 4).toLowerCase();
            String strSelect;
            if ("select ".equals(strSelectPart)) {
                strSelect = tableNameOrSelectStatement;
            } else {
                strSelect = "select * from " + tableNameOrSelectStatement;
            }
            rs = stmt.executeQuery(strSelect);

            ResultSetMetaData rsmd = rs.getMetaData();
            int c = rsmd.getColumnCount();
            while (rs.next()) {
                for (int i = 1; i <= c; i++) {
                    if (i > 1) {
                        System.out.print(",  ");
                    }
                    String columnValue = rs.getString(i);
//                    System.out.print(columnValue + " " + rsmd.getColumnName(i));
                    System.out.print(columnValue + " ");

                }
                System.out.println("");
            }

        } catch (Exception e) {
            Tools.msgBox(e.getMessage());
        }
    }

1 个答案:

答案 0 :(得分:0)

关于循环:

程序员提示:您需要尽可能避免while(true) - 循环。

在您的情况下,您可以使用旗帜系统,它将如下所示:

boolean customerHasFinished = false;
while(!customerHasFinished){
    ...
    //Do your stuff
    ...

    System.out.print("Have you finished ? ");
    String hasFinished = input.nextLine();
    customerHasFinished = hasFinished.equals("yes");
}

关于多个项目:

存储多件商品的最佳方法是使用collection

在您的情况下,您可能需要创建一个表示Item的Java类。拥有名称,成本等字段然后创建Item的集合。

ArrayList:

的示例
List<Item> listItem = new ArrayList<Item>();
boolean orderNotFinished = true;
while (orderNotFinished) {
    System.out.println("Please Choose an Item From Menu List");
    input = new Scanner(System.in);
    String itemChosen = input.nextLine();
    boolean insertedMenuItemId = db.goTodataBase.checkMenuItemInDB(itemChosen);
    if (insertedMenuItemId) {
        System.out.println("You Choose Item ID: " + itemChosen);
        listItem.add(Item.getItemByName(itemChosen)); //Add the chosen item to the list
        System.out.print("You want something else ? ");
        String hasFinished = input.nextLine();
        orderNotFinished = hasFinished.equals("yes");
    }else {
        System.out.println("Item Chosen doen't exist");
    }
}