点餐系统JAVASE点餐系统,java基础项目练习-Java文档类资源-CSDN下载
一,知识点
集合框架,IO流,序列化,反序列化,面向对象,接口,实现类
二,功能
1.用户
1)登录注册
2)查看所有菜品
3)购买菜品
4)查看购买状态(正在派送,已接收)
5)根据条件查看订单
2.商家(兼职管理员)
1)登录
2)查看已上架的菜品
3)更新菜品
4)更新用户信息
5)更新订单状态
三,实体类设计
//省略get/set
public class User implements Serializable {
private String tel;
private String pwd;
private String name;
private String address;
}
public class Admin implements Serializable {
private String account;
private String pwd;
}
public class Cuisine implements Serializable {
private int cid;
private String cName;
private double price;
private String introduce;
private String stock;
}
public class Order implements Serializable{
private int oId;
private String tel;//用户的编号
private int cId;//菜品编号
private int num;
private Date date;//日期
private double sum;//总价
private int state;//派送状态 0已付款 1.派送中 2.已接收
private int abandon;//是否弃用 0正常 1弃用
}
四,创建工具类
package com.sk.util;
import com.sk.entity.Admin;
import com.sk.entity.User;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* ClassName: UserUtil <br/>
* Description: <br/>
* date: 2022/1/3 9:21<br/>
*
* @author 斑马<br />
*/
public class DataUtil {
public static final String USER="user.data";
public static final String ADMIN="admin.data";
public static final String CUISINE="cuisine.data";
public static final String ORDER="user.data";
public static <E> void write(List<E> list, String url){
try (OutputStream ob =new FileOutputStream(url);
ObjectOutputStream out=new ObjectOutputStream(ob);) {
out.writeObject(list);
}catch (Exception e){
e.printStackTrace();
}
}
public static <E> List<E> read(String url){
try (InputStream is = new FileInputStream(url);
ObjectInputStream in=new ObjectInputStream(is)) {
List<E> list= (List<E>) in.readObject();
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
五,实现管理员登录
1.实现dao
public interface AdminDao {
/**根据账户密码查找管理员*/
Admin findAdminByAccAndPwd(String account,String pwd);
}
public class AdminDaoImpl implements AdminDao {
@Override
public Admin findAdminByAccAndPwd(String account, String pwd) {
List<Admin> list= DataUtil.read(DataUtil.ADMIN);
for (int i=0;i<list.size();i++){
Admin ad=list.get(i);
if(ad.getAccount().equals(account)&&ad.getPwd().equals(pwd)){
return ad;
}
}
return null;
}
}
2.实现service
public interface AdminService {
/**根据账户密码查找管理员*/
Admin findAdminByAccAndPwd(String account, String pwd);
}
public class AdminServiceImpl implements AdminService {
@Override
public Admin findAdminByAccAndPwd(String account, String pwd) {
return new AdminDaoImpl().findAdminByAccAndPwd(account,pwd);
}
}
3.实现view
public class AdminView {
Scanner sc=new Scanner(System.in);
AdminService adminService=new AdminServiceImpl();
public void login(){
System.out.println("***********管理员登录***********");
System.out.println("请输入账户:");
String account=sc.next();
System.out.println("请输入密码:");
String pwd=sc.next();
Admin ad=adminService.findAdminByAccAndPwd(account,pwd);
if(ad==null){
System.out.println("登录失败!");
login();
}else{
System.out.println("欢迎管理员:\t"+ad.getAccount()+"\t使用本系统");
}
}
}
4.测试
public class Test {
/**初始化管理员信息*/
public static void inintAdmin(){
Admin ad1=new Admin("admin","admin");
Admin ad2=new Admin("zs","zs");
List<Admin> list=new ArrayList<>();
list.add(ad1);
list.add(ad2);
DataUtil.write(list,DataUtil.ADMIN);
}
/**开始程序*/
public static void main(String[] args) {
//inintAdmin();
AdminView adminView=new AdminView();
adminView.login();
}
}
六,实现菜品更新
1.实现dao
public interface CuisineDao {
/**添加*/
public boolean addCuisine(Cuisine cuisine);
/**自增的id*/
public int autoCId();
/**下架*/
public boolean updateStateById(int cId,int state);
/**修改*/
public boolean updateById(Cuisine cuisine);
/**根据状态查看菜品*/
public List<Cuisine> showCuisineByState(int state);
/**查看所有菜品*/
public List<Cuisine> showCuisines();
}
public class CuisineDaoImpl implements CuisineDao {
@Override
public boolean addCuisine(Cuisine cuisine) {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
list.add(cuisine);
DataUtil.write(list,DataUtil.CUISINE);
return true;
}
@Override
public int autoCId() {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
int id= new Random().nextInt();
for (Cuisine c:list){
if(c.getCid()==id){
autoCId();
}
}
return id;
}
@Override
public boolean updateStateById(int cId, int state) {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
for (Cuisine c:list){
if(c.getCid()==cId){//cid相同
c.setState(state);//修改状态
DataUtil.write(list,DataUtil.CUISINE);
return true;
}
}
return false;
}
@Override
public boolean updateById(Cuisine cuisine) {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
for (int i=0;i<list.size();i++){
Cuisine c=list.get(i);
if(c.getCid()==cuisine.getCid()){//cid相同
list.set(i,cuisine);
DataUtil.write(list,DataUtil.CUISINE);
return true;
}
}
return false;
}
@Override
public List<Cuisine> showCuisineByState(int state) {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
List<Cuisine> newList=new ArrayList<>();//用来存不同状态的菜品
for (Cuisine c:list){
if(c.getState()==state){//状态相同
newList.add(c);//添加到新的集合中
}
}
return newList;
}
@Override
public List<Cuisine> showCuisines() {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
return list;
}
}
2.实现service
public interface CuisineService {
/**添加*/
public void addCuisine(Cuisine cuisine);
/**下架*/
public boolean updateStateById(int cId,int state);
/**修改*/
public boolean updateById(Cuisine cuisine);
/**根据状态查看菜品*/
public List<Cuisine> showCuisineByState(int state);
/**查看所有菜品*/
public List<Cuisine> showCuisines();
}
public class CuisineServiceImpl implements CuisineService {
CuisineDao cuisineDao=new CuisineDaoImpl();
@Override
public void addCuisine(Cuisine cuisine) {
int cid=cuisineDao.autoCId();//获取id
cuisine.setCid(cid);//随机的id
cuisine.setState(0);//默认状态是 上架
cuisineDao.addCuisine(cuisine);
}
//可以把上架的下架,把下架的上架
@Override
public boolean updateStateById(int cId, int state) {
return cuisineDao.updateStateById(cId,state);
}
@Override
public boolean updateById(Cuisine cuisine) {
return cuisineDao.updateById(cuisine);
}
@Override
public List<Cuisine> showCuisineByState(int state) {
return cuisineDao.showCuisineByState(state);
}
@Override
public List<Cuisine> showCuisines() {
return cuisineDao.showCuisines();
}
}
3.实现view
package com.sk.view;
import com.sk.entity.Cuisine;
import com.sk.service.CuisineService;
import com.sk.service.impl.CuisineServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
* ClassName: CuisineView <br/>
* Description: <br/>
* date: 2022/1/3 15:04<br/>
*
* @author 斑马<br />
*/
public class CuisineView {
Scanner sc = new Scanner(System.in);
CuisineService cuisineService = new CuisineServiceImpl();
/**
* 上架新菜品
*/
public void addCuisine() {
System.out.println("**********菜品添加***********");
System.out.println("请输入菜品名称:");
String cName = sc.next();
System.out.println("请输入菜品价格:");
double price = sc.nextDouble();
System.out.println("请输入菜品原料:");
String stock = sc.next();
System.out.println("请输入菜品介绍:");
String introduce = sc.next();
Cuisine c = new Cuisine(0, cName, price, introduce, stock, 0);
cuisineService.addCuisine(c);
System.out.println("上架成功!");
}
/**
* 下架
*/
public void updateStateById() {
System.out.println("**************下架***************");
//显示已经上架的
showCuisine0();
System.out.println("请选中要下架的商品编号:");
int cId=sc.nextInt();
boolean flag=cuisineService.updateStateById(cId,1);
if(flag){
System.out.println("菜品已下架!");
}else {
System.out.println("下架失败!");
}
}
/**
* 重新上架
*/
public void updateStateById1() {
System.out.println("**************重新上架***************");
//显示已经下架的
showCuisine1();
System.out.println("请选中要重新上架的商品编号:");
int cId=sc.nextInt();
boolean flag=cuisineService.updateStateById(cId,0);
if(flag){
System.out.println("菜品已上架!");
}else {
System.out.println("上架失败!");
}
}
/**
* 修改
*/
public void updateById() {
System.out.println("**********修改菜品***********");
System.out.println("请输入要修改的菜品编号:");
int cid=sc.nextInt();
System.out.println("请输入修改后的菜品名称:");
String cName = sc.next();
System.out.println("请输入修改后的菜品价格:");
double price = sc.nextDouble();
System.out.println("请输入修改后的菜品原料:");
String stock = sc.next();
System.out.println("请输入修改后的菜品介绍:");
String introduce = sc.next();
System.out.println("请选择菜品状态0.已上架1.已下架");
int state=sc.nextInt();
Cuisine c = new Cuisine(cid, cName, price, introduce, stock, state);
boolean flag=cuisineService.updateById(c);
if(flag){
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
}
/**
* 查看已上架菜品
*/
public void showCuisine0() {
System.out.println("**************已上架菜品***************");
List<Cuisine> list = cuisineService.showCuisineByState(0);
for (Cuisine c : list) {
System.out.println("编号:" + c.getCid() + "\t名称:" + c.getcName() + "\t价格:" + c.getPrice());
System.out.println("原料:" + c.getStock() + "\t状态:已上架");
System.out.println("简介:" + c.getIntroduce());
System.out.println("---------------------------");
}
}
/**
* 查看已下架菜品
*/
public void showCuisine1() {
System.out.println("**************已下架菜品***************");
List<Cuisine> list = cuisineService.showCuisineByState(1);
for (Cuisine c : list) {
System.out.println("编号:" + c.getCid() + "\t名称:" + c.getcName() + "\t价格:" + c.getPrice());
System.out.println("原料:" + c.getStock() + "\t状态:已下架");
System.out.println("简介:" + c.getIntroduce());
System.out.println("---------------------------");
}
}
/**
* 查看所有菜品
*/
public void showCuisines() {
System.out.println("*********查看所有菜品*********");
List<Cuisine> list = cuisineService.showCuisines();
for (Cuisine c : list) {
if (c.getState() == 0) {//上架
System.out.println("编号:" + c.getCid() + "\t名称:" + c.getcName() + "\t价格:" + c.getPrice());
System.out.println("原料:" + c.getStock() + "\t状态:已上架");
System.out.println("简介:" + c.getIntroduce());
} else {//已下架
System.out.println("编号:" + c.getCid() + "\t名称:" + c.getcName() + "\t价格:" + c.getPrice());
System.out.println("原料:" + c.getStock() + "\t状态:已下架");
System.out.println("简介:" + c.getIntroduce());
}
System.out.println("---------------------------");
}
}
}
4.初始化数据
public static void inintCuisine(){
Cuisine c1=new Cuisine(1,"红烧肉",30,"很好吃,吃不死人","豆腐",0);
Cuisine c2=new Cuisine(2,"红烧人肉",300000,"吃人犯法","豆腐",1);
List<Cuisine>list=new ArrayList<>();
list.add(c1);
list.add(c2);
DataUtil.write(list,DataUtil.CUISINE);
}
七,实现用户的登录注册
1.实现dao
public interface UserDao {
/**查找用户*/
public User findUserByTelAndPwd(String tel,String pwd);
/**添加用户*/
public void addUser(User user);
/**根据手机号查找用户*/
public User findUserByTel(String tel);
/**根据手机号修改用户信息*/
public void updateUserByTel(User user);
}
package com.sk.dao.impl;
import com.sk.dao.UserDao;
import com.sk.entity.User;
import com.sk.util.DataUtil;
import java.util.List;
/**
* ClassName: UserDaoImpl <br/>
* Description: <br/>
* date: 2022/1/4 8:19<br/>
*
* @author 斑马<br />
*/
public class UserDaoImpl implements UserDao {
@Override
public User findUserByTelAndPwd(String tel, String pwd) {
List<User> list= DataUtil.read(DataUtil.USER);
for (User u:list){
if(u.getTel().equals(tel)&&u.getPwd().equals(pwd)){
return u;
}
}
return null;
}
@Override
public void addUser(User user) {
List<User> list= DataUtil.read(DataUtil.USER);
list.add(user);
DataUtil.write(list,DataUtil.USER);
}
@Override
public User findUserByTel(String tel) {
List<User> list= DataUtil.read(DataUtil.USER);
for (User u:list){
if(u.getTel().equals(tel)){
return u;
}
}
return null;
}
@Override
public void updateUserByTel(User user) {
List<User> list= DataUtil.read(DataUtil.USER);
for (int i=0;i<list.size();i++){
User u=list.get(i);
if(u.getTel().equals(user.getTel())){
list.set(i,user);
DataUtil.write(list,DataUtil.USER);
}
}
}
}
2.实现service
package com.sk.service;
import com.sk.entity.User;
/**
* ClassName: UserService <br/>
* Description: <br/>
* date: 2022/1/4 8:33<br/>
*
* @author 斑马<br />
*/
public interface UserService {
/**查找用户*/
public User findUserByTelAndPwd(String tel, String pwd);
/**添加用户*/
public void addUser(User user);
/**根据手机号查找用户*/
public User findUserByTel(String tel);
/**根据手机号修改用户信息*/
public void updateUserByTel(User user);
}
package com.sk.service.impl;
import com.sk.dao.UserDao;
import com.sk.dao.impl.UserDaoImpl;
import com.sk.entity.User;
import com.sk.service.UserService;
/**
* ClassName: UserServiceImpl <br/>
* Description: <br/>
* date: 2022/1/4 8:35<br/>
*
* @author 斑马<br />
*/
public class UserServiceImpl implements UserService {
UserDao userDao=new UserDaoImpl();
@Override
public User findUserByTelAndPwd(String tel, String pwd) {
return userDao.findUserByTelAndPwd(tel,pwd);
}
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public User findUserByTel(String tel) {
return userDao.findUserByTel(tel);
}
@Override
public void updateUserByTel(User user) {
userDao.updateUserByTel(user);
}
}
3.实现view
public class UserView {
Scanner sc=new Scanner(System.in);
UserService userService=new UserServiceImpl();
CuisineView cuisineView=new CuisineView();
OrderView orderView=new OrderView();
/**登录菜单*/
public void star(){
System.out.println("************用户界面***************");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("请选择(其他选项退出):");
int num=sc.nextInt();
switch (num){
case 1:
login();
break;
case 2:
addUser();
break;
default:
System.out.println("欢迎下次使用!");
System.exit(0);
}
System.out.println("是否返回用户界面?y/n");
String str=sc.next();
if(str.equals("y")){
star();
}
}
/**登录*/
public void login(){
System.out.println("******************用户登录*****************");
System.out.println("请输入手机号:");
String tel=sc.next();
System.out.println("请输入密码:");
String pwd=sc.next();
User user=userService.findUserByTelAndPwd(tel,pwd);
if(user==null){
System.out.println("登录失败,手机号或密码错误!");
}else{
System.out.println("***********欢迎用户:\t"+user.getName()+"\t使用本系统*****************");
menu(user);
}
}
/**注册*/
public void addUser(){
System.out.println("******************用户注册*****************");
System.out.println("请输入手机号:");
String tel=sc.next();
//验证手机号格式
if(tel.matches("^1\\w{10}$")){
User u=userService.findUserByTel(tel);
if(u!=null){
System.out.println("手机号已存在!");
addUser();
}else {
System.out.println("请输入密码:");
String pwd=sc.next();
System.out.println("请再次输入密码:");
String pwd1=sc.next();
if(pwd1.equals(pwd)){
System.out.println("请输入昵称:");
String name=sc.next();
System.out.println("请输入收货地址:");
String address=sc.next();
User user=new User(tel,pwd,name,address);
userService.addUser(user);
System.out.println("注册成功!");
}else{
System.out.println("密码不一致!");
addUser();
}
}
}else{
System.out.println("手机号格式错误!");
addUser();
}
}
}
4.初始化数据并测试
/**初始化数据*/
public static void inintUser(){
User u1=new User("1","1","张三","天津");
List<User> list=new ArrayList<>();
list.add(u1);
DataUtil.write(list,DataUtil.USER);
}
八,实现用户的其他功能
后续会省略已经存在的代码
1.查看所有菜品
1)CuisineDao
public interface CuisineDao {
/**根据状态查看菜品*/
public List<Cuisine> showCuisineByState(int state);
}
public class CuisineDaoImpl implements CuisineDao {
@Override
public List<Cuisine> showCuisineByState(int state) {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
List<Cuisine> newList=new ArrayList<>();//用来存不同状态的菜品
for (Cuisine c:list){
if(c.getState()==state){//状态相同
newList.add(c);//添加到新的集合中
}
}
return newList;
}
}
2)CuisineService
public interface CuisineService {
/**根据状态查看菜品*/
public List<Cuisine> showCuisineByState(int state);
}
}
public class CuisineServiceImpl implements CuisineService{
@Override
public List<Cuisine> showCuisineByState(int state) {
return cuisineDao.showCuisineByState(state);
}
}
3)CuisineView
public class CuisineView {
public void showCuisine0() {
System.out.println("**************已上架菜品***************");
List<Cuisine> list = cuisineService.showCuisineByState(0);
for (Cuisine c : list) {
System.out.println("编号:" + c.getCid() + "\t名称:" + c.getcName() + "\t价格:" + c.getPrice());
System.out.println("原料:" + c.getStock() + "\t状态:已上架");
System.out.println("简介:" + c.getIntroduce());
System.out.println("---------------------------");
}
}
}
4)用户菜单
public class UserView {
/**用户菜单*/
public void menu(User user){
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("请选择:");
int num=sc.nextInt();
switch (num){
case 1:
cuisineView.showCuisine0();
break;
case 2:
orderView.addOrder(user);
break;
case 3:
orderView.receiving(user);
break;
case 4:
orderView.showOrder(user);
break;
case 5:
orderView.delete(user);
break;
default:
System.out.println("输入错误!请重新输入!");
menu(user);
}
System.out.println("是否返回用户菜单?y/n");
String str=sc.next();
if(str.equals("y")){
menu(user);
}
}
}
2.实现点餐
OrderDao
public interface OrderDao {
/**添加订单*/
public void addOrder(Order order);
/**自增主键*/
public int autoId();
/**根据订单编号修改订单状态*/
public boolean updateStateById(int id,int state);
}
public class OrderDaoImpl implements OrderDao {
@Override
public void addOrder(Order order) {
List<Order> list= DataUtil.read(DataUtil.ORDER);
list.add(order);
DataUtil.write(list,DataUtil.ORDER);
}
@Override
public int autoId() {
List<Order> list= DataUtil.read(DataUtil.ORDER);
//获取最后一个订单
Order o= list.get(list.size()-1);
//获取id
int id=o.getoId();
return id+1;
}
@Override
public boolean updateStateById(int id, int state) {
List<Order> list= DataUtil.read(DataUtil.ORDER);
for (int i=0;i<list.size();i++){
Order o=list.get(i);
if(o.getoId()==id){
o.setState(state);
DataUtil.write(list,DataUtil.ORDER);
return true;
}
}
return false;
}
}
CuisineDao
public interface CuisineDao {
/**根据菜品编号查看菜品*/
public Cuisine showCuisineByCid(int cid);
}
public class CuisineDaoImpl implements CuisineDao {
@Override
public Cuisine showCuisineByCid(int cid) {
List<Cuisine> list= DataUtil.read(DataUtil.CUISINE);
for (Cuisine c:list){
if(c.getCid()==cid){//id相同
return c;
}
}
return null;
}
}
OrderService
public interface OrderService {
/**点餐
*/
public Order addOrder(String tel, int cid, int num);
/**根据订单编号修改订单状态*/
public boolean updateStateById(int id,int state);
}
public class OrderServiceImpl implements OrderService {
OrderDao orderDao=new OrderDaoImpl();
CuisineDao cuisineDao=new CuisineDaoImpl();
@Override
public Order addOrder(String tel, int cid, int num) {
//判断菜品是否存在
Cuisine cuisine=cuisineDao.showCuisineByCid(cid);
if(cuisine!=null){
if(num>0){//数量大于0
double sum=cuisine.getPrice()*num;
Order o=new Order(orderDao.autoId(),tel,cid,num,new Date(),sum,0,0);
orderDao.addOrder(o);
return o;
}
}
return null;
}
@Override
public boolean updateStateById(int id, int state) {
return orderDao.updateStateById(id,state);
}
}
OrderView
public class OrderView {
Scanner sc=new Scanner(System.in);
OrderService orderService=new OrderServiceImpl();
/**点餐*/
public void addOrder(User user){
//显示商品
new CuisineView().showCuisine0();
System.out.println("请输入要购买的菜品编号:");
int cid=sc.nextInt();
System.out.println("请输入购买数量:");
int num=sc.nextInt();
Order o=orderService.addOrder(user.getTel(),cid,num);
if(o!=null){
System.out.println("点餐成功!总金额:"+o.getSum());
System.out.println("是否付款?y/n");
String str=sc.next();
if(str.equals("y")){
orderService.updateStateById(o.getoId(),1);
System.out.println("付款成功!");
}else{
System.out.println("付款失败!");
}
}else{
System.out.println("点餐失败!菜品不存在!");
}
}
}
3.实现确认收货
OrderView
public class OrderView {
public void receiving(User user){
//显示未收货的列表
List<Order> list=orderService.showOrderByTelAndState(user.getTel(),2);
CuisineService service=new CuisineServiceImpl();
System.out.println("订单编号:\t菜品名称\t菜品单价\t购买数量\t总价\t配送状态\t购买日期");
for (Order o:list){
//根据菜品编号找到对应的菜品
Cuisine c=service.showCuisineByCid(o.getcId());
Date date=o.getDate();
SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d=sm.format(date);//格式化日期类型
System.out.println(o.getoId()+"\t"+c.getcName()+"\t"+c.getPrice()+"\t"+o.getNum()
+"\t"+o.getSum()+"\t"+"配送中\t"+d
);
}
System.out.println("请输入要确认收货的订单编号:");
int oid=sc.nextInt();
boolean flag=orderService.updateStateById(oid,3);
if(flag){
System.out.println("签收成功!");
}else {
System.out.println("签收失败!");
}
}
}
4.实现查看订单
OrderDao
public interface OrderDao {
/**根据用户id 查看订单*/
public List<Order> showOrderByTel(String tel);
}
public class OrderDaoImpl implements OrderDao {
@Override
public List<Order> showOrderByTel(String tel) {
List<Order> list= DataUtil.read(DataUtil.ORDER);
List<Order> newList=new ArrayList<>();
for (Order o:list){
if(o.getTel().equals(tel)){
newList.add(o);
}
}
return newList;
}
}
OrderService
public interface OrderService {
/**根据用户id 查看订单*/
public List<Order> showOrderByTel(String tel);
}
public class OrderServiceImpl implements OrderService {
OrderDao orderDao=new OrderDaoImpl();
@Override
public List<Order> showOrderByTel(String tel) {
return orderDao.showOrderByTel(tel);
}
OrderView
public class OrderView {
/**查看订单*/
public void showOrder(User user){
List<Order> list=orderService.showOrderByTel(user.getTel());
CuisineService service=new CuisineServiceImpl();
System.out.println("订单编号:\t菜品名称\t菜品单价\t购买数量\t总价\t配送状态\t购买日期");
for (Order o:list){
if(o.getAbandon()==1){//弃用
continue;
}
//根据菜品编号找到对应的菜品
Cuisine c=service.showCuisineByCid(o.getcId());
Date date=o.getDate();
SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d=sm.format(date);//格式化日期类型
String state=null;
switch (o.getState()){
case 0:
state="未付款";
break;
case 1:
state="已付款";
break;
case 2:
state="配送中";
break;
case 3:
state="已签收";
break;
}
System.out.println(o.getoId()+"\t"+c.getcName()+"\t"+c.getPrice()+"\t"+o.getNum()
+"\t"+o.getSum()+"\t"+state+"\t"+d
);
}
}
}
5.删除订单
OrderDao
public interface OrderDao {
/**根据订单编号修改弃用状态*/
public boolean updateAbandonById(int id,int abandon);
}
public class OrderDaoImpl implements OrderDao {
@Override
public boolean updateAbandonById(int id, int abandon) {
List<Order> list= DataUtil.read(DataUtil.ORDER);
for (int i=0;i<list.size();i++){
Order o=list.get(i);
if(o.getoId()==id){
o.setAbandon(abandon);
DataUtil.write(list,DataUtil.ORDER);
return true;
}
}
return false;
}
}
OrderService
public interface OrderService {
/**删除订单*/
public boolean updateAbandonById(int id,int abandon);
}
public class OrderServiceImpl implements OrderService {
@Override
public boolean updateAbandonById(int id, int abandon) {
return orderDao.updateAbandonById(id,abandon);
}
}
OrderView
public class OrderView {
/**删除订单*/
public void delete(User user){
showOrder(user);//显示所有订单
System.out.println("请输入要删除的订单");
int oid=sc.nextInt();
boolean flag=orderService.updateAbandonById(oid,1);
if(flag){
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
}
九,实现管理员订单配送
public interface OrderService {
/**查看所有订单*/
public List<Order> showOrders();
}
public class OrderServiceImpl implements OrderService {
OrderDao orderDao=new OrderDaoImpl();
@Override
public List<Order> showOrders() {
return orderDao.showOrders();
}
}
1.显示所有订单
OrderDao
public interface OrderDao {
/**查看所有订单*/
public List<Order> showOrders();
}
public class OrderDaoImpl implements OrderDao {
@Override
public List<Order> showOrders() {
return DataUtil.read(DataUtil.ORDER);
}
}
OrderService
public interface OrderService {
/**查看所有订单*/
public List<Order> showOrders();
}
public class OrderServiceImpl implements OrderService {
OrderDao orderDao=new OrderDaoImpl();
@Override
public List<Order> showOrders() {
return orderDao.showOrders();
}
}
OrderView
public class OrderView {
Scanner sc=new Scanner(System.in);
OrderService orderService=new OrderServiceImpl();
/**显示所有订单*/
public void showAllOrder(){
CuisineService service=new CuisineServiceImpl();
UserService userService=new UserServiceImpl();
//所有的订单
List<Order> list=orderService.showOrders();
System.out.println("订单编号:\t菜品名称\t菜品单价\t购买数量\t总价\t配送状态\t购买日期\t用户\t收货地址");
for (Order o:list){
//根据菜品编号找到对应的菜品
Cuisine c=service.showCuisineByCid(o.getcId());
Date date=o.getDate();
SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d=sm.format(date);//格式化日期类型
User user=userService.findUserByTel(o.getTel());
String state=null;
switch (o.getState()){
case 0:
state="未付款";
break;
case 1:
state="已付款";
break;
case 2:
state="配送中";
break;
case 3:
state="已签收";
break;
}
System.out.println(o.getoId()+"\t"+c.getcName()+"\t"+c.getPrice()+"\t"+o.getNum()
+"\t"+o.getSum()+"\t"+state+"\t"+d+"\t"+user.getTel()+"\t"+user.getAddress()
);
}
}
}
2.配送订单
OrderDao
public interface OrderDao {
/**根据状态查看订单*/
public List<Order> showOrdersByState(int state);
}
public class OrderDaoImpl implements OrderDao {
@Override
public List<Order> showOrdersByState(int state) {
List<Order> list= DataUtil.read(DataUtil.ORDER);
List<Order> newList=new ArrayList<>();
for (Order o:list){
if(o.getState()==state){
newList.add(o);
}
}
return newList;
}
}
OrderService
public interface OrderService {
/**根据状态查看订单*/
public List<Order> showOrdersByState(int state);
}
public class OrderServiceImpl implements OrderService {
@Override
public List<Order> showOrdersByState(int state) {
return orderDao.showOrdersByState(state);
}
}
OrderView
package com.sk.view;
import com.sk.dao.CuisineDao;
import com.sk.dao.impl.CuisineDaoImpl;
import com.sk.entity.Cuisine;
import com.sk.entity.Order;
import com.sk.entity.User;
import com.sk.service.CuisineService;
import com.sk.service.OrderService;
import com.sk.service.UserService;
import com.sk.service.impl.CuisineServiceImpl;
import com.sk.service.impl.OrderServiceImpl;
import com.sk.service.impl.UserServiceImpl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* ClassName: OrderView <br/>
* Description: <br/>
* date: 2022/1/4 10:57<br/>
*
* @author 斑马<br />
*/
public class OrderView {
Scanner sc=new Scanner(System.in);
OrderService orderService=new OrderServiceImpl();
/**配送订单*/
public void distribution(){
//显示所有需要配送的订单
List<Order> list=orderService.showOrdersByState(1);
CuisineService service=new CuisineServiceImpl();
UserService userService=new UserServiceImpl();
System.out.println("订单编号:\t菜品名称\t菜品单价\t购买数量\t总价\t配送状态\t购买日期\t手机号\t收货地址");
for (Order o:list){
//根据菜品编号找到对应的菜品
Cuisine c=service.showCuisineByCid(o.getcId());
Date date=o.getDate();
SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d=sm.format(date);//格式化日期类型
User user=userService.findUserByTel(o.getTel());
System.out.println(o.getoId()+"\t"+c.getcName()+"\t"+c.getPrice()+"\t"+o.getNum()
+"\t"+o.getSum()+"\t"+"已付款\t"+d+"\t"+user.getTel()+"\t"+user.getAddress()
);
}
//配送单个订单
System.out.println("请输入配送的订单编号:");
int oid=sc.nextInt();
boolean flag= orderService.updateStateById(oid,2);
if(flag){
System.out.println("配送成功");
}else {
System.out.println("配送失败");
}
}
}
十,整合项目
package com.sk.view;
import java.util.Scanner;
/**
* ClassName: View <br/>
* Description: <br/>
* date: 2022/1/5 9:00<br/>
*
* @author 斑马<br />
*/
public class View {
Scanner sc=new Scanner(System.in);
AdminView adminView=new AdminView();
UserView userView=new UserView();
public void menu(){
System.out.println("*************点餐系统**************");
System.out.println("1.用户界面");
System.out.println("2.管理员界面");
int num=sc.nextInt();
switch (num){
case 1:
userView.star();
break;
case 2:
adminView.login();
break;
default:
System.out.println("输入错误!");
menu();
}
System.out.println("是否返回点餐系统界面?y/n");
String str=sc.next();
if("y".equals(str)){
menu();
}
}
}
十一,效果示意图