main方法类:
package javaone;
import java.io.File;
import java.nio.file.WatchEvent.Kind;
import java.security.interfaces.RSAKey;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.jar.Attributes.Name;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.PreparedStatement;
import java.sql.*;
import java.time.Period;
public class javazy {
public static void main(String[] args) {
Scanner zx = new Scanner(System.in);
int zongji = 0; //加菜点菜价格总计
int jiacaitwo = 0; //加菜价格
int pricezj = 0; //点菜价格
int zhuohao = 0; //客户桌号
List<Food> zhangdan = new ArrayList<Food>(); //点菜菜品集合
List<Food> jiacai = new ArrayList<Food>(); //加菜菜品集合
while (true){
System.out.println("*******************");第一界面
System.out.println("****在线点餐系统****");
System.out.println("****1. 菜品管理 ****");
System.out.println("****2. 点餐系统 ****");
System.out.println("****3. 加菜系统 ****");
System.out.println("****4. 结账系统 ****");
System.out.println("****0. 退出系统 ****");
System.out.println("*******************");
int choice = zx.nextInt();
if (choice == 1) {第一界面选择数choice
//菜品管理
System.out.println("请输入你的账号:");
int a = zx.nextInt();
System.out.println("请输入你的密码:");
int b = zx.nextInt();
if (a == 111 && b == 111) { //密码账号设置处
System.out.println("登录成功!");
}else {
System.out.println("登录失败!");
System.exit(0); 异常处理
}
//
while(true) {
System.out.println("****1`添加菜品*****");第一界面的第二界面 增删改查
System.out.println("****2`删除菜品*****");
System.out.println("****3`更改菜品*****");
System.out.println("****4`查询菜品*****");
System.out.println("****0`退出管理*****");
int choose = zx.nextInt();
if (choose == 1) { //增删改查选择数choose
System.out.println("运行添加程序!");
FoodDao dao = new FoodDao();
Food person = new Food();
System.out.println("请输入要添加菜品的编号:");
int tid = zx.nextInt();
System.out.println("请输入要添加菜品的名称:");
String tname = zx.next();
System.out.println("请输入要添加菜品的价格:");
int tprice = zx.nextInt();
System.out.println("请输入要添加菜品是否为特价类: (是为1 否为0)");
String tbargains = zx.next();
System.out.println("请输入要添加菜品是否为新品类: (是为1 否为0)");
String tproducts = zx.next();
System.out.println("请输入要添加菜品是否为套餐类: (是为1 否为0)");
String tmeal = zx.next();
person.setId(tid);
person.setName(tname);
person.setPrice(tprice);
person.setBargains(tbargains);
person.setProducts(tproducts);
person.setMeal(tmeal);
try {
dao.addPerson(person); ///将添加的person加入到addperson
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("菜品添加成功!");
}else if (choose == 2) { //增删改查选择数choose
删除
System.out.println("运行删除程序!");
System.out.println("请输入要删除菜品的编号:");
FoodDao dao = new FoodDao();
int sid = zx.nextInt();
try {
dao.deleteUser(sid);//要删除的id
} catch (Exception e) {
e.printStackTrace();
}
}else if (choose == 3) { //增删改查选择数choose
System.out.println("运行更改程序!");
FoodDao dao = new FoodDao();
Food person = new Food();
System.out.println("请输入要更改菜品的编号:");
int gid = zx.nextInt();
System.out.println("请输入更改后菜品的名称:");
String gname = zx.next();
System.out.println("请输入更改后菜品的价格:");
int gprice = zx.nextInt();
System.out.println("请输入更改后菜品是否为特价类:");
String gbargains = zx.next();
System.out.println("请输入更改后菜品是否为新品类:");
String gproducts = zx.next();
System.out.println("请输入更改后菜品是否为套餐类:");
String gmeal = zx.next();
person.setId(gid);//要更改的id
person.setName(gname);
person.setPrice (gprice);
person.setBargains(gbargains);
person.setProducts(gproducts);
person.setMeal(gmeal);
try {
dao.updatePerson(person);
} catch (Exception e) {
e.printStackTrace();
}
}else if (choose == 4) { //增删改查选择数choose
System.out.println("运行查询程序!");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.queryAll();
System.out.println("顺序 菜品编号 菜品名称 菜品价格 特价类\t新品类\t套餐类");
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if (choose ==0 ) { //增删改查选择数choose
System.out.println("退出管理");
break ;
}else {
System.out.println("输入有误!");
}
}
//
}else if (choice == 2) {第一界面选择数choice
System.out.println("运行在线点餐!");
while(true){
System.out.println();
System.out.println("*****1.查看特价类*****"); //第一界面的第二界面 点餐系统
System.out.println("*****2.查看新品类*****");
System.out.println("*****3.查看套餐类*****");
System.out.println("***4.不查看 立即点餐***");
System.out.println("***** 0.结束点餐 *****");
System.out.println("请输入操作数字:");
int sort = zx.nextInt();
if (sort == 1) {
System.out.println("特价商品为:");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.tiaojiante();
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if ( sort == 2) {
System.out.println("新品商品为:");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.tiaojianxin();
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if ( sort == 3) {
System.out.println("套餐商品为:");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.tiaojiantao();
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if ( sort == 4) {
System.out.println("开始点餐:");
System.out.println("请输入要点菜品的编号:");
int did = zx.nextInt();
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.queryAll();
int mo = 1;
for (int i = did; i <= did; i++) {
System.out.println(personList.get(did - 1));利用输入菜品编号提出id所在的所有信息
for (int j = 0; j < mo; j++) {
zhangdan.add(personList.get(did - 1));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}else if (sort == 0){
System.out.println("结束点菜");
System.out.println("账单:");
int a= 0;
for (int i = 0; i < zhangdan.size(); i++) { //集合循环查询出已点菜品
System.out.println( zhangdan.get(i).getName()+" " + zhangdan.get(i).getPrice());
//循环菜品菜名 价格
a = a +zhangdan.get(i).getPrice();
pricezj = a ; 赋值点菜价格
}
System.out.print("合计");
System.out.println(pricezj);
for (int i = 0; i < zhangdan.size(); i++) {
zhuohao = i +1 ; 通过循环输出桌号
}
System.out.println("您的桌号为:" + zhuohao );
System.out.println("祝您用餐愉快!");
break;
}
}
}else if (choice == 3) { /第一界面选择数choice
//加餐系统
System.out.println("请输入您的桌号:");
int zhuohaoj = zx.nextInt();
if (zhuohaoj == zhuohao) { 判断桌号是否正确
//开始加菜
while(true){
System.out.println("*****1.查看特价类*****");/第一界面的第二界面 加菜系统
System.out.println("*****2.查看新品类*****");
System.out.println("*****3.查看套餐类*****");
System.out.println("***4.不查看 开始加菜***");
System.out.println("***** 0.结束加菜 *****");
System.out.println("请输入操作序号:");
int jiajiacai = zx.nextInt();
if (jiajiacai == 1) {
System.out.println("特价商品为:");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.tiaojiante();
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if ( jiajiacai == 2) {
System.out.println("新品商品为:");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.tiaojianxin();
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if ( jiajiacai == 3) {
System.out.println("套餐商品为:");
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.tiaojiantao();
for (Food person : personList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}else if (jiajiacai == 4 ) {
System.out.println("请输入加菜菜品的编号:");
int jid = zx.nextInt(); //加菜id
FoodDao dao = new FoodDao();
try {
List<Food> personList = dao.queryAll();
int mo = 1;
for (int i = jid; i <= jid; i++) {
System.out.println(personList.get(jid - 1));
for (int j = 0; j < mo; j++) {
jiacai.add(personList.get(jid - 1));
}
} //将加菜菜品放入一个新集合中
} catch (Exception e) {
e.printStackTrace();
}
}else if (jiajiacai == 0) {
int a= 0; //打印加菜价格
for (int i = 0; i < jiacai.size(); i++) { //集合循环查询出加菜菜品
System.out.println( jiacai.get(i).getName()+" " + jiacai.get(i).getPrice());
//循环菜品菜名 价格
a = a +jiacai.get(i).getPrice();
jiacaitwo = a ; 赋值加菜价格
System.out.print("加菜合计");
System.out.println(jiacaitwo);
}
break;
}
}
}else {
System.out.println("桌号不存在!");
break;
}
}else if (choice == 4) { //第一界面选择数choice
zongji = jiacaitwo + pricezj ; ///计算总的钱数
System.out.println("您的本次消费共计:" + zongji + "元");
System.out.println("请选择您的支付方式:");
System.out.println("*****1.支付宝支付*****"); /第一界面的第二界面 结账界面
System.out.println("*****2.微信支付*****");
System.out.println("*****3.信用卡支付*****");
int zhifu = zx.nextInt();
if (zhifu == 1) {
System.out.println("进行支付宝支付......");
}else if (zhifu == 2) {
System.out.println("进行微信支付......");
}else if (zhifu == 3) {
System.out.println("进行信用卡支付......");
}
System.out.println("欢迎您的下次光临!!!");
System.exit(0);
}else if (choice == 0) { //第一界面选择数choice
System.out.println("退出系统!");
System.exit(0);
}else {
System.out.println("输入错误!");
}
}
}
}
链接数据库类
package javaone;
import java.sql.*;
//连接数据库
public class DBUtil {
private static String URL = “jdbc:mysql://127.0.0.1/zuoye?useSSL=false”; 输入中文
private static String Driver = “com.mysql.jdbc.Driver”;
private static String user = “root”; 账号
private static String password = “111111”; /密码
public static Connection connectDB() throws Exception {
Class.forName(Driver);
Connection conn = DriverManager.getConnection(URL, user, password);
return conn;
}
}
增删改查等 类
package javaone;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class FoodDao {
//查询
public List<Food> queryAll() throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "SELECT * FROM Food ";
List<Food> personList = new ArrayList<Food>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Food person = new Food();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
person.setPrice(rs.getInt("price"));
person.setBargains(rs.getString("bargains"));
person.setProducts(rs.getString("products"));
person.setMeal(rs.getString("meal"));
personList.add(person);
}
return personList;
}
//增加
public void addPerson(Food person) throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "INSERT INTO Food(id, name,price,bargains,products,meal)"
+ " VALUES(?, ?, ?, ?, ?, ?)";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, person.getId());
psmt.setString(2, person.getName());
psmt.setInt(3, person.getPrice());
psmt.setString(4, person.getBargains());
psmt.setString(5, person.getProducts());
psmt.setString(6, person.getMeal());
psmt.execute();
conn.close();
}
//删除
public void deleteUser(int id) throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "DELETE FROM Food WHERE id = ?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setInt(1, id);
psmt.execute();
}
//修改
public void updatePerson(Food person) throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "UPDATE Food SET name=?, price=?, bargains=?, products=?, meal=?"
+ " WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(6, person.getId());
pstmt.setString(1, person.getName());
pstmt.setInt(2, person.getPrice());
pstmt.setString(3, person.getBargains());
pstmt.setString(4, person.getProducts());
pstmt.setString(5, person.getMeal());
pstmt.executeUpdate();
conn.close();
}
//特价类条件查询
public List<Food> tiaojiante() throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "SELECT * FROM Food WHERE bargains = '1' ";
List<Food> personList = new ArrayList<Food>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Food person = new Food();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
person.setPrice(rs.getInt("price"));
person.setBargains(rs.getString("bargains"));
person.setProducts(rs.getString("products"));
person.setMeal(rs.getString("meal"));
personList.add(person);
}
return personList;
}
//新品类条件查询
public List<Food> tiaojianxin() throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "SELECT * FROM Food WHERE products = '1' ";
List<Food> personList = new ArrayList<Food>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Food person = new Food();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
person.setPrice(rs.getInt("price"));
person.setBargains(rs.getString("bargains"));
person.setProducts(rs.getString("products"));
person.setMeal(rs.getString("meal"));
personList.add(person);
}
return personList;
}
//套餐类条件查询
public List<Food> tiaojiantao() throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "SELECT * FROM Food WHERE meal = '1' ";
List<Food> personList = new ArrayList<Food>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Food person = new Food();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
person.setPrice(rs.getInt("price"));
person.setBargains(rs.getString("bargains"));
person.setProducts(rs.getString("products"));
person.setMeal(rs.getString("meal"));
personList.add(person);
}
return personList;
}
//点菜
public List<Food> diancan() throws Exception {
Connection conn = DBUtil.connectDB();
String sql = "SELECT * FROM ";
List<Food> personList = new ArrayList<Food>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.first()) {
Food person = new Food();
person.setId(rs.getInt("id"));
person.setName(rs.getString("name"));
person.setPrice(rs.getInt("price"));
person.setBargains(rs.getString("bargains"));
person.setProducts(rs.getString("products"));
person.setMeal(rs.getString("meal"));
personList.add(person);
}
return personList;
}
}
食物get set 等
package javaone;
public class Food {
private int id;
private String name ;
private int price ;
private String bargains ;
private String products ;
private String meal ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getBargains() {
return bargains;
}
public void setBargains(String bargains) {
this.bargains = bargains;
}
public String getProducts() {
return products;
}
public void setProducts(String products) {
this.products = products;
}
public String getMeal() {
return meal;
}
public void setMeal(String meal) {
this.meal = meal;
}
public Food(int id, String name, int price, String bargains,
String products, String meal) {
super();
this.id = id;
this.name = name;
this.price = price;
this.bargains = bargains;
this.products = products;
this.meal = meal;
}
public Food() {
super();
}
@Override
public String toString() {
return “id=” + id + “, name=” + name + “, price=” + price
+ “, bargains=” + bargains + “, products=” + products
+ “, meal=” + meal ;
}
}