基于mysql的“吃货联盟系统”可以更好地对数据进行存储,因为引入了mysql的概念,所以在进行Java的代码编写之前我们要先在数据库中设计好我们将要使用的一系列表。这里我设计了三个表:
##数据库food表设计
##创建food表
create table eat.food(
`id` int auto_increment primary key ,
`name` varchar(20) not null ,
`price` decimal(10,2) not null,
`praise` int not null
);
##创建order表
create table if not exists eat.order (
`id` int auto_increment primary key ,
`userName` varchar(20) not null ,
`address` varchar(20) not null,
`phone` varchar(100) not null
);
##创建order_detial表
create table eat.order_detial(
`id` int auto_increment primary key ,
`userName` varchar(20) not null ,
`foodName` varchar(100) not null,
`price` decimal(10,2) not null ,
`count` int not null ,
`sum_price` decimal(10,2) not null,
`time` int not null,
`state` varchar(20) not null
);
效果图:
表设计好后我们就可以进行代码的编写,在这里我将功能都封装在Cash类中,然后在测试类中进行调用。
public class Cash {
private String jdbcUri = "jdbc:mysql://数据库连接地址:3306/数据库名称";
private String uName = "用户名"; //数据库的用户名
private String pass = "密码"; //数据库的密码
private Connection conn;
private Statement statement ;
private String name;
Scanner input = new Scanner(System.in);
public Cash(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(jdbcUri,uName,pass); //连接数据库
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void queryFood(){ //打印菜单
try {
statement = conn.createStatement(); //实例化statement对象
ResultSet resultSet = statement.executeQuery("select * from food");
while(resultSet.next()){ //打印输出菜单
StringBuffer s = new StringBuffer();
s.append("序号:").append(resultSet.getInt(1))
.append("\t菜名:").append(resultSet.getString(2))
.append("\t价格:").append(resultSet.getBigDecimal(3))
.append("\t点赞数:").append(resultSet.getInt(4));
System.out.println(s);
}
} catch (SQLException e) { //捕获异常
e.printStackTrace();
}finally {
try {
statement.close(); //关闭statement
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void myInfo(){ //个人信息
try {
statement = conn.createStatement();
System.out.print("请输入你的姓名:");
this.name = input.next();
System.out.print("请输入你的地址:");
String address = input.next();
System.out.print("请输入你的电话:");
String phone = input.next();
statement.executeUpdate("insert into `order`(userName,address,phone) values ('"+this.name+"','"+address+"','"+phone+"')");
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void orderFood(){ //我要点餐
try {
statement = conn.createStatement();
System.out.print("请输入菜品的序号:");
int no = input.nextInt();
System.out.print("请输入份数:");
int num = input.nextInt();
BigDecimal count = BigDecimal.valueOf(num);
System.out.print("请输入送餐时间:");
int time = input.nextInt();
if(time<8||time>22){
System.out.println("不在送餐时间范围内,请重新输入!");
System.out.print("请输入送餐时间:");
time = input.nextInt();
}
String state = "未签收";
ResultSet resultSet = statement.executeQuery("select * from `order` where userName ='"+this.name+"'");
resultSet.next();
String userName = resultSet.getString(2);
resultSet.close();
resultSet = statement.executeQuery("select * from food where id="+no+"");
resultSet.next();
String foodName = resultSet.getString(2);
BigDecimal price = resultSet.getBigDecimal(3);
BigDecimal sumPrice = price.multiply(count);
resultSet.close();
statement.executeUpdate("insert into order_detial(userName,foodName,price,count,sum_price,time,state)" +
" values ('"+userName+"','"+foodName+"',"+price+","+num+","+sumPrice+","+time+",'"+state+"')");
System.out.println("订餐成功!");
System.out.println("您订的是:"+foodName+num+"份"+"\t总共:"+sumPrice+"元"+"\t送餐时间:"+time+"点");
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void show(){ //查看餐袋
try{
statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from order_detial where userName='"+this.name+"'");
while(resultSet.next()){
StringBuffer s = new StringBuffer();
s.append("序号:").append(resultSet.getInt(1))
.append("\t姓名:").append(resultSet.getString(2))
.append("\t菜名:").append(resultSet.getString(3))
.append("\t价格:").append(resultSet.getBigDecimal(4))
.append("\t份数:").append(resultSet.getInt(5))
.append("\t总价:").append(resultSet.getBigDecimal(6))
.append("\t送餐时间:").append(resultSet.getInt(7))
.append("\t订单状态:").append(resultSet.getString(8));
System.out.println(s);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void sign(){ //签收订单
try {
statement = conn.createStatement();
System.out.print("请输入你要签收的订单序号");
int no = input.nextInt();
ResultSet resultSet = statement.executeQuery("select * from order_detial where userName ='"+this.name+"'");
List list = new ArrayList();
while(resultSet.next()){
list.add(resultSet.getInt(1));
}
resultSet.close();
if(list.contains(no)) {
resultSet = statement.executeQuery("select * from order_detial where id = "+no);
resultSet.next();
if(resultSet.getString(8).contains("已签收")){
System.out.println("订单已签收,不能再次签收!");
resultSet.close();
}else{
resultSet.close();
statement.executeUpdate("update order_detial set state='已签收' where id = " + no + "");
System.out.println("签收成功!");
}
}else{
System.out.println("没有该订单!");
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void delete(){ //删除订单
try {
statement = conn.createStatement();
System.out.print("请输入你要删除的订单序号");
int no = input.nextInt();
ResultSet resultSet = statement.executeQuery("select * from order_detial where userName ='"+this.name+"'");
List list = new ArrayList();
while(resultSet.next()){
list.add(resultSet.getInt(1));
}
resultSet.close();
if(list.contains(no)) {
resultSet = statement.executeQuery("select * from order_detial where id = "+no);
resultSet.next();
if(resultSet.getString(8).contains("未签收")){
System.out.println("订单未签收,不能删除!");
resultSet.close();
}else{
resultSet.close();
statement.executeUpdate("delete from order_detial where id = "+no+"");
System.out.println("删除成功!");
}
}else{
System.out.println("没有该订单!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void praise(){ //点赞
try {
statement = conn.createStatement();
System.out.print("请输入你要点赞的菜品序号");
int no = input.nextInt();
ResultSet resultSet = statement.executeQuery("select * from food");
List list = new ArrayList();
while(resultSet.next()){
list.add(resultSet.getInt(1));
}
resultSet.close();
if(list.contains(no)) {
resultSet = statement.executeQuery("select * from food where id = "+no);
resultSet.next();
int praiseNum = resultSet.getInt(4)+1;
resultSet.close();
statement.executeUpdate("update food set praise="+praiseNum+" where id = "+no+"");
System.out.println("点赞成功!");
}else{
System.out.println("不存在该菜品,无法点赞!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class Eat {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Cash cash = new Cash();
System.out.println("*****欢迎使用吃货联盟系统******");
cash.myInfo();
int num =0;
while(num==0) {
printMenu();
System.out.print("请输入你要进行的操作序号:");
int caozuo=input.nextInt();
switch(caozuo) {
case 1:
System.out.println("****我要点餐****");
cash.queryFood();
cash.orderFood();
break;
case 2:
System.out.println("****查看餐袋****");
cash.show();
break;
case 3:
System.out.println("****签收订单****");
cash.sign();
break;
case 4:
System.out.println("****删除订单****");
cash.delete();
break;
case 5:
System.out.println("****我要点赞****");
cash.praise();
break;
case 6:
System.out.println("****退出系统****");
num=1;
cash.close();
System.out.println("谢谢你的使用,欢迎下次光临!");
break;
default:
break;
}
}
}
public static void printMenu() {
System.out.println("********************");
System.out.println("1、我要点餐");
System.out.println("2、查看餐袋");
System.out.println("3、签收订单");
System.out.println("4、删除订单");
System.out.println("5、我要点赞");
System.out.println("6、退出系统");
System.out.println("********************");
}
}