简单的javaweb版kfc点餐收银系统

简单的javaweb版kfc点餐收银系统,包含单例模式与工厂模式详解

说明:本次作业实现为简单的网页版,由 两人组队 共同完成。

基本需求:

1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动
5.模拟打印小票的功能
6.html.jsp实现简单的可视化界面。
7.实现会员储值卡功能,完成储值卡消费。
8.实现当天营业额和餐品销量计算和统计,用数据库记录。
利用JS的图库表Echars,以表格形式实现数据可视化。

源代码说明:

1:bean文件夹:实体类源码
2:controller文件夹:控制层源码
3:dao文件夹:数据库操作源码
4:service文件夹:业务类源码
5:utils文件夹:工具类源码

用到的设计模式:

单点食物:
采用简单工厂:使用反射实现食品类的实例化
工厂类 :(SimFoodFactory)

套餐食物:
采用抽象工厂:生成套餐系列产品(三个套餐)
工厂类:(ComboOneFactory ComboTwoFactory ComboThreeFactory )

数据库连接池
采用单例模式:创建c3p0数据库连接池对象。

单例模式与工厂模式的简单学习与说明:

单例模式:
(1)饿汉式: 线程安全,调用效率高。 但是,不能延时加载
(2)懒汉式:线程安全,调用效率不高。但是可以延时加载。
(3)双重检测锁:线程安全,调用效率高。可以延时加载
(4)静态内部类:
(5)枚举单例:线程安全,调用效率高,不能延时加载
五种单例模式简单代码声明:
(1) 饿汉式:

public class SingletonDemo01{
private static final SingletonDemo01 s = new SingletonDemo02();
//不管有无调用,在内部直接实例化对象造成资源浪费,不可延时加载
private SingletonDemo01(){} //私有化构造器
public static /*synchronized*/ SingletonDemo02 getInstance(){
return s;
}
}

如果只是加载本类,而不是要调用getInstance(),甚至永远没有调用,则会造成资源浪费!
(2) 懒汉式:

public class SingletonDemo02 {
private static SingletonDemo02 s;
private SingletonDemo02(){} //私有化构造器
public static synchronized SingletonDemo02 getInstance(){
if(s==null){
s = new SingletonDemo01();
}
return s;
}
}

**特点:延迟加载, 懒加载! 真正用的时候才加载!资源利用率高了。但是,每次调用getInstance()方法都要同步,并发
效率较低。
**
(3) 双重检测锁:

public class SingletonDemo03 {
private static SingletonDemo03 instance = null;
public static SingletonDemo03 getInstance() {
if (instance == null) {
SingletonDemo03 sc;
synchronized (SingletonDemo03.class) {
sc = instance;
if (sc == null) {
synchronized (SingletonDemo03.class) {
if(sc == null) {
sc = new SingletonDemo03();
}
}
instance = sc;
}
}
}
return instance;
}
private SingletonDemo03() {
}
}

特点:式将同步内容下方到if内部,提高了执行的效率不必每次获取对象时都进行同步,只有第一次才同步
创建了以后就没必要了。

(4) 静态内部类:

public class SingletonDemo04 {
private static class SingletonClassInstance {
private static final SingletonDemo04 instance = new SingletonDemo04();
}
public static SingletonDemo04 getInstance() {
return SingletonClassInstance.instance;
}
private SingletonDemo04() {
}
}

特点:外部类没有static属性,则不会像饿汉式那样立即加载对象。只有真正调用getInstance(),才会加载静态内部类。加载类时是线程安全的。 instance是static final
类型,保证了内存中只有这样一个实例存在,而且只能被赋值一次,从而保证了线程安全性. –兼备了并发高效调用和延迟加载的优势!

(5) 枚举单例:

public enum SingletonDemo05 {
/**
* 定义一个枚举的元素,它就代表了Singleton的一个实例。
*/
INSTANCE;
/**
* 单例可以有自己的操作
*/
public void singletonOperation(){
//功能处理
}
}

特点:实现简单枚举本身就是单例模式。由JVM从根本上提供保障!避免通过反射和反序列化的漏洞! 无延迟加载
单例模式总结:除了枚举式其他几种都存在反射与反序列化的漏洞,饿汉式速度最快,但不可以延时加载,懒汉式因为线程同步的原因速度最慢,其他几种也较快,仅低于单例模式

应用场景:
-windows的Recycle Bin(回收站)也是典型的单例应用。在整个系统运行过程中,回收站一直维护着仅有的一个实例。
– 项目中,读取配置文件的类,一般也只有一个对象。没有必要每次使用配置文件数据,每次new一个对象去读取。
– 网站的计数器,一般也是采用单例模式实现,否则难以同步。
– 应用程序的日志应用,一般都何用单例模式实现,这一般是由于共享的日志文件一直处于打开状态,因为只能有一个实例去操作
,否则内容不好追加。
– 数据库连接池的设计一般也是采用单例模式,因为数据库连接是一种数据库资源。
– 操作系统的文件系统,也是大的单例模式实现的具体例子,一个操作系统只能有一个文件系统。
– Application 也是单例的典型应用(Servlet编程中会涉及到)
– 在Spring中,每个Bean默认就是单例的,这样做的优点是Spring容器可以管理
– 在servlet编程中,每个Servlet也是单例
– 在spring MVC框架/struts1框架中,控制器对象也是单例

工厂模式:

本质:实例化对象,用工厂方法代替new操作。将选择实现类、创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。
**
(1) 简单工厂模式:简单工厂模式也叫静态工厂模式,就是工厂类一般是使用静态方法,通过接收的参数的不同来返回不同的对象实例。用来生产同一等级结构中的任意产品。
**

public class CarFactory {
public static Car createCar(String type){
Car c = null;
if("奥迪".equals(type)){
c = new Audi();
}else if("奔驰".equals(type)){
c = new Benz();
}
return c;
}
}
public class CarFactory {
public static Car createAudi(){
return new Audi();
}
public static Car createBenz(){
return new Benz();
}
}

(2)工厂方法模式:
为了避免简单工厂模式的缺点,不完全满足OCP。 工厂方法模式和简单工厂模式最大的不同在于,简单工厂模式只有一个(对于一个项目或者一个独立模块而言)工厂类,而工厂方法模式有一组实现了相同接口的工厂类。用来生产同一等级结构中的固定产品。(支持增加任意产品)
(3)抽象工厂模式
用来生产不同产品族的全部产品。主要针对系列产品(对于增加新的产品,无能为力;支持增加产品族)

应用场景:
– JDK中Calendar的getInstance方法
– JDBC中Connection对象的获取
– Hibernate中SessionFactory创建Session
– spring中IOC容器创建管理bean对象
– XML解析时的DocumentBuilderFactory创建解析器对象
– 反射中Class对象的newInstance()

简单的学习完设计模式后,让我们来了解下kfc点餐收银系统吧

系统源代码结构:

1:bean文件夹:实体类源码
2:controller文件夹:控制层源码
3:dao文件夹:数据库操作源码
4:service文件夹:业务类源码
5:utils文件夹:工具类源码

简单的javaweb版kfc点餐收银系统

简单的javaweb版kfc点餐收银系统简单的javaweb版kfc点餐收银系统简单的javaweb版kfc点餐收银系统

系统uml类图

简单的javaweb版kfc点餐收银系统简单的javaweb版kfc点餐收银系统

系统源代码:

bean文件夹:实体类源码

package edu.xust.bean;

public class BusinessBean {
    private String name;
    private Integer count ;

    @Override
    public String toString() {
        return "BusinessBean{" +
                "name='" + name + '\'' +
                ", count=" + count +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }
}

package edu.xust.bean;

public class CafeBean extends Drink{
    public CafeBean() {
        this.id = 2;
        this.name = "咖啡";
        this.price = 12.0;
        this.capacity = 100f;
    }
}

package edu.xust.bean;

/**
 * 炸鸡
 */
public class ChickenBean extends Eat {
    public ChickenBean() {
        this.id = 2;
        this.name = "炸鸡";
        this.price = 25.0;
        this.quailty = 100f;
    }
}

package edu.xust.bean;

/**
 * 薯条
 */
public class ChipsBean extends Eat {
    public ChipsBean() {
        this.id = 3;
        this.name = "薯条";
        this.price = 8.0;
        this.quailty = 50f;
    }
}

package edu.xust.bean;

/**
 * 可乐
 */
public class ColaBean extends Drink {
    public ColaBean() {
        this.id = 1;
        this.name = "可乐";
        this.price = 8.0;
        this.capacity = 100f;
    }
}

package edu.xust.bean;

public class Drink extends Food{
public Float capacity;

public Float getCapacity() {
    return capacity;
}

@Override
public String toString() {
    return "Drink{" +
            "capacity=" + capacity +
            ", id=" + id +
            ", name='" + name + '\'' +
            ", price=" + price +
            '}';
}

}

package edu.xust.bean;

public  class Eat extends Food {
    public Float quailty;

    public Float getQuailty() {
        return quailty;
    }

    @Override
    public String toString() {
        return "Eat{" +
                "quailty=" + quailty +
                ", id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

package edu.xust.bean;

public class Food {
    public Integer id;
    public String name;
    public Double price;

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Double getPrice() {
        return price;
    }
}

package edu.xust.bean;

public class HamBean extends Eat {
    public HamBean() {
        this.id = 1;
        this.name = "汉堡";
        this.price = 12.0;
        this.quailty = 10f;
    }
}

package edu.xust.bean;

public class JuiceBean extends Drink {
    public JuiceBean() {
        this.id = 2;
        this.name = "咖啡";
        this.price = 12.0;
        this.capacity = 100f;
    }
}

package edu.xust.bean;

public class MemberBean {
    private String id;
    private String name;
    private Double money;

    @Override
    public String toString() {
        return "MemberBean{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

controller文件夹:控制层源码

package edu.xust.controller;

import com.alibaba.fastjson.JSON;
import edu.xust.bean.BusinessBean;
import edu.xust.service.BusinessService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/info.action")
public class EcharController extends HttpServlet{
    BusinessService bs = new BusinessService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        try {
            List<BusinessBean> bus = bs.findBus();
            String s = JSON.toJSONString(bus);
            resp.getWriter().write(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

package edu.xust.controller;


import com.alibaba.fastjson.JSON;
import edu.xust.bean.*;
import edu.xust.service.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/food.action")
public class FoodController extends HttpServlet{
    SimFoodFactory simFoodFactory = new SimFoodFactory();
    MoneyService moneyService = new MoneyService();
    BusinessService bussinessService = new BusinessService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        List<Food> foodList;
        String colaBean = req.getParameter("ColaBean");
        String cafeBean = req.getParameter("CafeBean");
        String juiceBean = req.getParameter("JuiceBean");
        String chickenBean = req.getParameter("ChickenBean");
        String chipsBean = req.getParameter("ChipsBean");
        String hamBean = req.getParameter("HamBean");
        String disCount = req.getParameter("discount");//折扣
        String tickle = req.getParameter("tickle");//优惠券

        String com = req.getParameter("com");
        switch (com){
            case "1":
                foodList = new ComboOneFactory().getCombo();
                break;
            case "2":
                foodList = new ComboTwoFactory().getCombo();
                break;
            case "3":
                foodList = new ComboThreeFactory().getCombo();
                break;

            default: foodList = new ArrayList();
        }
        if("on".equals(colaBean)) {//可乐
            Food food = simFoodFactory.getFood(ColaBean.class);
            foodList.add(food);
        }
        if("on".equals(cafeBean)) {//咖啡
            Food food = simFoodFactory.getFood(CafeBean.class);
            foodList.add(food);
        }
        if("on".equals(juiceBean)) {//果汁
            Food food = simFoodFactory.getFood(JuiceBean.class);
            foodList.add(food);
        }
        if("on".equals(chickenBean)) {//炸鸡
            Food food = simFoodFactory.getFood(ChickenBean.class);
            foodList.add(food);
        }
        if("on".equals(chipsBean)) {//薯条
            Food food = simFoodFactory.getFood(ChipsBean.class);
            foodList.add(food);
        }
        if("on".equals(hamBean)) {//汉堡
            Food food = simFoodFactory.getFood(HamBean.class);
            foodList.add(food);
        }
        try {
            //更新卖货信息
            bussinessService.updateBusiness(foodList);
            //获取总价钱
            Double moneyCount = moneyService.getMoneyCount(foodList, Double.valueOf(disCount), Double.valueOf(tickle));
            //更新总价钱
            bussinessService.updateMoney(moneyCount);
            //打印票
            moneyService.receipt(foodList,Double.valueOf(disCount),Double.valueOf(tickle));
            req.setAttribute("money",moneyCount);//总价
            req.setAttribute("disCount",Double.valueOf(disCount)*10);//折扣
            req.setAttribute("tickle",tickle);//优惠券
            req.setAttribute("food",foodList);//食物
            req.getRequestDispatcher("show.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

package edu.xust.controller;

import edu.xust.service.MoneyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/member.action")
public class MemberController extends HttpServlet{
    MoneyService moneyService = new MoneyService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String memberId = req.getParameter("memberId");
        String count = req.getParameter("count");
        String message;
        try {
            //会员的钱
            Double memberMoney = moneyService.getMemberMoneyById(memberId);
            //会员还剩的钱
            Double memberCoin = moneyService.getCoin(memberMoney, Double.valueOf(count));
            if(memberMoney==-1.0){
                message = "会员不存在";
                resp.getWriter().write(message);
                return;
            }
            if(memberCoin<0){
                message = "会员账户余额不足";
                resp.getWriter().write(message);
                return;
            }
            else {
                //更新账户余额
                moneyService.resetMoneyById(memberId,Double.valueOf(memberCoin));
                message = "扣费成功,还剩:"+moneyService.getMemberMoneyById(memberId);
                resp.getWriter().write(message);
                return;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

package edu.xust.controller;

import edu.xust.service.MoneyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/money.action")
public class MoneyConlroller extends HttpServlet {
    MoneyService moneyService = new MoneyService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String inMoney = req.getParameter("inMoney");
        String count = req.getParameter("count");
        String message;
        //计算找零
        Double coin = moneyService.getCoin(Double.valueOf(inMoney), Double.valueOf(count));
            if(coin >=0 ) {//现金支付
                message = "找零" + coin;
                resp.getWriter().write(message);
                return;
            }else{//收钱小鱼需要的钱
                message = "资金不足";
                resp.getWriter().write(message);
            }
        }
    }


dao文件夹:数据库操作源码

package edu.xust.dao;

import edu.xust.bean.BusinessBean;
import edu.xust.utils.DaoUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class BussDao {
    /**
     *
     * @param name 商品名字
     * @param count 商品数量
     * @throws SQLException
     */
    public void upDateBuss(String name,int count ) throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "update  bussiness set count="+count+" where name=?";
        queryRunner.update(sql,name);
    }

    /**
     *
     * @param name 商品名字
     * @return 返回该商品的已出售的数量
     * @throws SQLException
     */
    public int selectCountByName(String name) throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select count from bussiness where name=?";
        List<BusinessBean> bus = (List<BusinessBean>)queryRunner.query(sql, name, new BeanListHandler(BusinessBean.class));
        return bus.get(0).getCount();
    }

    /**
     * 查找卖货信息
     * @return
     * @throws SQLException
     */
    public List<BusinessBean> selectBus() throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select * from bussiness ";
        List<BusinessBean> bus = (List<BusinessBean>)queryRunner.query(sql, new BeanListHandler(BusinessBean.class));
        return bus;
    }
    /*
    获取现在的输入
     */
    public Integer selectMony() throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select count from bussiness where name='收入'";
        List<BusinessBean> money = (List<BusinessBean>)queryRunner.query(sql,  new BeanListHandler(BusinessBean.class));
        return money.get(0).getCount();
    }
    /*
        更新收入金额
     */
    public void updateMoney(Double count) throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "update  bussiness set count=? where name='收入'";
        queryRunner.update(sql,count);
    }
}

package edu.xust.dao;

import edu.xust.bean.MemberBean;
import edu.xust.utils.DaoUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

public class MemberDao {
    /**
     * 查询会余额
     * @param id
     * @return 返回会员余额
     * @throws SQLException
     */
    public  Double SelectMemberMoneyById(String id) throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select money from member where id=?";
        MemberBean memberBean = (MemberBean) queryRunner.query(sql,id, new BeanHandler(MemberBean.class));
        if(memberBean==null)
            return -1.0;
        return memberBean.getMoney();
    }

    /**
     * 更新会员余额
     * @param id
     * @param money
     * @throws SQLException
     */
    public  void UpdateMemberMoneyById(String id,Double money) throws SQLException {
        DataSource dataSource = DaoUtils.getDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "update  member set money="+money+" where id=?";
        queryRunner.update(sql,id);
    }
}

4:service文件夹:业务类源码

package edu.xust.service;

import edu.xust.bean.BusinessBean;
import edu.xust.bean.Food;
import edu.xust.dao.BussDao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BusinessService {
     BussDao  bussDao = new BussDao();
    /**
     * 更新卖货信息
     * @param foodList
     * @throws SQLException
     */
    public  void updateBusiness(List<Food> foodList) throws SQLException {
        Map <String ,Integer>map = new HashMap();
        //统计每个商品出现的次数
        for(Food f:foodList){
                if(!map.containsKey(f.name))
                    map.put(f.name,1);
                else
                    map.put(f.name,map.get(f.name)+1);
        }
        for(String key:map.keySet()) {
            //查询该商品已出售的数量
            int i = bussDao.selectCountByName(key);
            //更新数量
            bussDao.upDateBuss(key,i+map.get(key));
        }
    }
    //查找商品的卖出信息
    public List<BusinessBean> findBus() throws SQLException {
        return bussDao.selectBus();
    }
    //获取现收入
    public Integer getMoney() throws SQLException {
        return bussDao.selectMony();
    }
    //更新收入
    public void updateMoney(Double count) throws SQLException {
        Integer money = getMoney();
        bussDao.updateMoney(money+count);
    }
}


package edu.xust.service;

import edu.xust.bean.Food;

import java.util.List;

/**
 * 抽象工厂,加载套餐
 */
public interface ComboFactory {
    List<Food> getCombo();
}

package edu.xust.service;

import edu.xust.bean.ChickenBean;
import edu.xust.bean.ColaBean;
import edu.xust.bean.Food;

import java.util.ArrayList;
import java.util.List;

/**
 * 1号组合套餐
 * 炸鸡和可乐
 */
public class ComboOneFactory implements ComboFactory {
    @Override
    public List<Food> getCombo() {
        List<Food> foodOneList= new ArrayList<Food>();
        Food chick = new ChickenBean();
        Food cola = new ColaBean();
        chick.price = chick.price-2;//套餐的优惠
        cola.price = cola.price-1;
        foodOneList.add(chick);
        foodOneList.add(cola);
        return foodOneList;
    }
}

package edu.xust.service;

import edu.xust.bean.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 3号组合套餐
 * 汉堡和咖啡
 */
public class ComboThreeFactory implements ComboFactory {
    @Override
    public List<Food> getCombo() {
        List<Food> foodThreeList= new ArrayList<Food>();
        Food ham = new HamBean();
        Food  cafe= new CafeBean();
        ham.price = ham.price-1;//套餐组合的优惠
        cafe.price = cafe.price-0.5;
        foodThreeList.add(cafe);
        foodThreeList.add(ham);
        return foodThreeList;
    }
}

package edu.xust.service;

import edu.xust.bean.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 2号组合套餐
 * 薯条和果汁
 */
public class ComboTwoFactory implements ComboFactory {
    @Override
    public List<Food> getCombo() {
        List<Food> foodTwoList= new ArrayList<Food>();
        Food chips = new ChipsBean();
        Food  juice= new JuiceBean();
        chips.price = chips.price-1;//套餐组合的优惠
        juice.price = juice.price-0.5;
        foodTwoList.add(chips);
        foodTwoList.add(juice);
        return foodTwoList;
    }
}

package edu.xust.service;

import edu.xust.bean.Food;
import edu.xust.bean.MemberBean;
import edu.xust.dao.MemberDao;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class MoneyService {
    MemberDao memberDao = new MemberDao();
    /**
     * 根据商品获得总价钱
     * disCount折扣
     * tickte优惠券金额
     */
    public  Double getMoneyCount(List<Food> list,Double disCount,Double ticket){
        Double moneyCount=0.0;
        for(Food food:list)
            moneyCount +=food.price;
        return moneyCount*disCount-ticket;
    }

    /**
     *找零
     * @param inMoney 收入的钱
     * @param count 食物总价钱
     * @return 找零的钱
     */
    public Double getCoin(Double inMoney,Double count){
        return inMoney-count;
    }
    /**
     *打印小票
     * @param list 食物集合
     * @param disCount 折扣
     * @param ticket 优惠券
     */
    public void receipt(List<Food> list, Double disCount, Double ticket) throws IOException {
        File file = new File("e:/ticket.txt");
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write("消费清单如下!!"+"\r\n");
        for(Food food:list){
            bw.write("食物:"+food.name+"\r\n");
            bw.write("价格:"+food.price+"元"+"\r\n");
        }
        bw.write("折扣:"+disCount*10+"折"+"\r\n");
        bw.write("优惠:"+ticket+"元"+"\r\n");
        bw.write("总金额"+ getMoneyCount(list,disCount,ticket)+"元");
        bw.close();
    }

    /**
     * 通过id获取会员
     * @param id
     * @return
     * @throws SQLException
     */
    public Double getMemberMoneyById(String id) throws SQLException {
        return memberDao.SelectMemberMoneyById(id);
    }
    public void resetMoneyById(String id,Double money) throws SQLException {
        memberDao.UpdateMemberMoneyById(id,money);
    }
}

package edu.xust.service;

import edu.xust.bean.Food;

/**
 * 简单工厂,通过反射加载食物实例(单点)
 */
public class SimFoodFactory {
    public Food getFood(Class c) {
        try {
            return (Food) Class.forName(c.getName()).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

5:utils文件夹:工具类源码

package edu.xust.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;

public class DaoUtils {
    private static DataSource dataSource;
    public static DataSource getDataSource() {
        if(dataSource==null)
            dataSource = new ComboPooledDataSource();
        return dataSource;
    }
}

运行截图

简单的javaweb版kfc点餐收银系统简单的javaweb版kfc点餐收银系统简单的javaweb版kfc点餐收银系统简单的javaweb版kfc点餐收银系统
简单的javaweb版kfc点餐收银系统
简单的javaweb版kfc点餐收银系统
简单的javaweb版kfc点餐收银系统
简单的javaweb版kfc点餐收银系统
简单的javaweb版kfc点餐收银系统