主頁 > 後端開發 > Java學習-第一部分-第三階段-專案實戰:滿漢樓專案

Java學習-第一部分-第三階段-專案實戰:滿漢樓專案

2022-09-25 06:18:30 後端開發

滿漢樓專案

筆記目錄:(https://www.cnblogs.com/wenjie2000/p/16378441.html)

注意:筆記內容僅為實作該專案的基本后端功能,并不會實作可視化界面,效果都在控制臺展示,

要完成的滿漢樓專案說明

滿漢樓專案功能多,界面復雜,涉及到復雜的awt和swing技術和事件編程,做如下調整:

  1. 去掉界面和事件處理(作業中使用很少),使用控制臺界面
  2. 完成滿漢樓專案的登錄、訂座、點餐和結賬、查看賬單等功能
  3. 提示:在實際作業中,獨立完成專案新功能非常重要,這是鍛煉編程能力和思想的重要途徑

界面設計

用戶登錄

image

顯示餐桌狀態

image

預定

image

顯示菜品

image

點餐

image

查看賬單

image

結賬

image

專案設計

image

準備作業

準備工具類Utility,提高開發效率,并搭建專案的整體結構

在實際開發中,公司都會提供相應的工具類和開發庫,可以提高開發效率

  1. 了解Utility類的使用

  2. 測驗Utility類

基本結構

image

在MySQL中創建表

#用戶表
create table employee (
	id int primary key auto_increment, #自增
	empId varchar(50) not null default '',#員工號
	pwd char(32) not null default '',#密碼md5
	name varchar(50) not null default '',#姓名
	job varchar(50) not null default '' #崗位
)charset=utf8; 

insert into employee values(null, '6668612', md5('123456'), '張三豐', '經理');
insert into employee values(null, '6668622', md5('123456'),'小龍女', '服務員');
insert into employee values(null, '6668633', md5('123456'), '張無忌', '收銀員');
insert into employee values(null, '666', md5('123456'), '老韓', '經理');

#餐桌表
create table diningTable (
	id int primary key auto_increment, #自增, 表示餐桌編號
	state varchar(20) not null default '',#餐桌的狀態
	orderName varchar(50) not null default '',#預訂人的名字
	orderTel varchar(20) not null default ''
)charset=utf8; 

insert into diningTable values(null, '空','','');

#菜譜
create table menu (
	id int primary key auto_increment, #自增主鍵,作為菜譜編號(唯一)
	name varchar(50) not null default '',#菜品名稱
	type varchar(50) not null default '', #菜品種類
	price double not null default 0#價格
)charset=utf8; 

insert into menu values(null, '八寶飯', '主食類', 10);
insert into menu values(null, '叉燒包', '主食類', 20);
insert into menu values(null, '宮保雞丁', '熱菜類', 30);
insert into menu values(null, '山藥撥魚', '涼菜類', 14);
insert into menu values(null, '銀絲卷', '甜食類', 9);
insert into menu values(null, '水煮魚', '熱菜類', 26);
insert into menu values(null, '甲魚湯', '湯類', 100);
insert into menu values(null, '雞蛋湯', '湯類', 16);

#賬單流水, 考慮可以分開結賬, 并考慮將來分別統計各個不同菜品的銷售情況
create table bill (
	id int primary key auto_increment, #自增主鍵
	billId varchar(50) not null default '',#賬單號可以按照自己規則生成 UUID
	menuId int not null default 0,#菜品的編號, 也可以使用外鍵
	nums SMALLINT not null default 0,#份數
	money double not null default 0, #金額
	diningTableId int not null default 0, #餐桌
	billDate datetime not null ,#訂單日期
	state varchar(50) not null default '' # 狀態 '未結賬' , '已經結賬', '掛單'
)charset=utf8;

druid組態檔(需要根據自己情況進行配置)

#key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db02?rewriteBatchedStatements=true
username=root
password=123456
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=50
#max wait time (5000 mil seconds)
maxWait=5000

BasicDAO(如果看過我上一節的筆記,就可以不需要再寫一遍,直接拿來用)

package com.zwj.mhl.dao;

import com.zwj.mhl.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


/**
 * 開發BasicDAO , 是其他DAO的父類
 */
public class BasicDAO<T> { //泛型指定具體型別

    private QueryRunner qr =  new QueryRunner();

    //開發通用的dml方法, 針對任意的表
    public int update(String sql, Object... parameters) {

        Connection connection = null;

        try {
            connection = JDBCUtilsByDruid.getConnection();
            int update = qr.update(connection, sql, parameters);
            return  update;
        } catch (SQLException e) {
           throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }
    }

    //回傳多個物件(即查詢的結果是多行), 針對任意表

    /**
     *
     * @param sql sql 陳述句,可以有 ?
     * @param clazz 傳入一個類的Class物件 比如 Actor.class
     * @param parameters 傳入 ? 的具體的值,可以是多個
     * @return 根據Actor.class 回傳對應的 ArrayList 集合
     */
    public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);

        } catch (SQLException e) {
            throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }
    }

    //查詢單行結果 的通用方法
    public T querySingle(String sql, Class<T> clazz, Object... parameters) {

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return  qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);

        } catch (SQLException e) {
            throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }
    }

    //查詢單行單列的方法,即回傳單值的方法

    public Object queryScalar(String sql, Object... parameters) {

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return  qr.query(connection, sql, new ScalarHandler(), parameters);

        } catch (SQLException e) {
            throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }
    }
}

JDBCUtilsByDruid(基于druid資料庫連接池的工具類)

package com.zwj.mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 基于druid資料庫連接池的工具類
 */
public class JDBCUtilsByDruid {

    private static DataSource ds;

    //在靜態代碼塊完成 ds初始化
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //撰寫getConnection方法
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    //關閉連接, 老師再次強調: 在資料庫連接池技術中,close 不是真的斷掉連接
    //而是把使用的Connection物件放回連接池
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {

        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

Utility(輔助輸入資料的工具類)

package com.zwj.mhl.utils;


/**
   工具類的作用:
   處理各種情況的用戶輸入,并且能夠按照程式員的需求,得到用戶的控制臺輸入,
*/

import java.util.*;

public class Utility {
   //靜態屬性,,,
    private static Scanner scanner = new Scanner(System.in);

    
    /**
     * 功能:讀取鍵盤輸入的一個選單選項,值:1——5的范圍
     * @return 1——5
     */
   public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一個字符的字串
            c = str.charAt(0);//將字串轉換成字符char型別
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("選擇錯誤,請重新輸入:");
            } else break;
        }
        return c;
    }

   /**
    * 功能:讀取鍵盤輸入的一個字符
    * @return 一個字符
    */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一個字符
        return str.charAt(0);
    }
    /**
     * 功能:讀取鍵盤輸入的一個字符,如果直接按回車,則回傳指定的默認值;否則回傳輸入的那個字符
     * @param defaultValue 指定的默認值
     * @return 默認值或輸入的字符
     */
    
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字串,要么是一個字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
   
    /**
     * 功能:讀取鍵盤輸入的整型,長度小于2位
     * @return 整數
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);//一個整數,長度<=2位
            try {
                n = Integer.parseInt(str);//將字串轉換成整數
                break;
            } catch (NumberFormatException e) {
                System.out.print("數字輸入錯誤,請重新輸入:");
            }
        }
        return n;
    }
    /**
     * 功能:讀取鍵盤輸入的 整數或默認值,如果直接回車,則回傳默認值,否則回傳輸入的整數
     * @param defaultValue 指定的默認值
     * @return 整數或默認值
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }
         
         //例外處理...
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("數字輸入錯誤,請重新輸入:");
            }
        }
        return n;
    }

    /**
     * 功能:讀取鍵盤輸入的指定長度的字串
     * @param limit 限制的長度
     * @return 指定長度的字串
     */

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    /**
     * 功能:讀取鍵盤輸入的指定長度的字串或默認值,如果直接回車,回傳默認值,否則回傳字串
     * @param limit 限制的長度
     * @param defaultValue 指定的默認值
     * @return 指定長度的字串
     */
   
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }


   /**
    * 功能:讀取鍵盤輸入的確認選項,Y或N
    * 將小的功能,封裝到一個方法中.
    * @return Y或N
    */
    public static char readConfirmSelection() {
        System.out.println("請輸入你的選擇(Y/N)");
        char c;
        for (; ; ) {//無限回圈
           //在這里,將接受到字符,轉成了大寫字母
           //y => Y n=>N
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("選擇錯誤,請重新輸入:");
            }
        }
        return c;
    }

    /**
     * 功能: 讀取一個字串
     * @param limit 讀取的長度
     * @param blankReturn 如果為true ,表示 可以讀空字串, 
     *                   如果為false表示 不能讀空字串,
     *           
    * 如果輸入為空,或者輸入大于limit的長度,就會提示重新輸入,
     * @return
     */
    private static String readKeyBoard(int limit, boolean blankReturn) {
        
      //定義了字串
      String line = "";

      //scanner.hasNextLine() 判斷有沒有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//讀取這一行
           
         //如果line.length=0, 即用戶沒有輸入任何內容,直接回車
         if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以回傳空串
                else continue; //如果blankReturn=false,不接受空串,必須輸入內容
            }

         //如果用戶輸入的內容大于了 limit,就提示重寫輸入  
         //如果用戶如的內容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("輸入長度(不能大于" + limit + ")錯誤,請重新輸入:");
                continue;
            }
            break;
        }
        return line;
    }
}

代碼實作

注意:建議看代碼之前先根據上面的資訊自己寫

image

com.zwj.mhl

dao

BasicDAO

package com.zwj.mhl.dao;

import com.zwj.mhl.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


/**
 * 開發BasicDAO , 是其他DAO的父類
 */
public class BasicDAO<T> { //泛型指定具體型別

    private QueryRunner qr =  new QueryRunner();

    //開發通用的dml方法, 針對任意的表
    public int update(String sql, Object... parameters) {

        Connection connection = null;

        try {
            connection = JDBCUtilsByDruid.getConnection();
            int update = qr.update(connection, sql, parameters);
            return  update;
        } catch (SQLException e) {
           throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }

    }

    //回傳多個物件(即查詢的結果是多行), 針對任意表

    /**
     *
     * @param sql sql 陳述句,可以有 ?
     * @param clazz 傳入一個類的Class物件 比如 Actor.class
     * @param parameters 傳入 ? 的具體的值,可以是多個
     * @return 根據Actor.class 回傳對應的 ArrayList 集合
     */
    public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);

        } catch (SQLException e) {
            throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }

    }

    //查詢單行結果 的通用方法
    public T querySingle(String sql, Class<T> clazz, Object... parameters) {

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return  qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);

        } catch (SQLException e) {
            throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }
    }

    //查詢單行單列的方法,即回傳單值的方法

    public Object queryScalar(String sql, Object... parameters) {

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return  qr.query(connection, sql, new ScalarHandler(), parameters);

        } catch (SQLException e) {
            throw  new RuntimeException(e); //將編譯例外->運行例外 ,拋出
        } finally {
            JDBCUtilsByDruid.close(null, null, connection);
        }
    }
}

BillDAO

package com.zwj.mhl.dao;

import com.zwj.mhl.domain.Bill;

public class BillDAO extends BasicDAO<Bill> {
}

DiningTableDAO

package com.zwj.mhl.dao;

import com.zwj.mhl.domain.DiningTable;

public class DiningTableDAO extends BasicDAO<DiningTable> {
    //如果有特別的操作,可以寫在 DiningTableDAO
}

EmployeeDAO

package com.zwj.mhl.dao;

import com.zwj.mhl.domain.Employee;

public class EmployeeDAO extends BasicDAO<Employee> {
    //這里還可以寫特有的操作.
}

MenuDAO

package com.zwj.mhl.dao;

import com.zwj.mhl.domain.Menu;

public class MenuDAO extends BasicDAO<Menu> {
}

MultiTableDAO

package com.zwj.mhl.dao;


import com.zwj.mhl.domain.MultiTableBean;

public class MultiTableDAO extends BasicDAO<MultiTableBean> {
}

domain

Bill

package com.zwj.mhl.domain;

import java.util.Date;

/**
 * Bill 是javabean 和 bill對應
 * id int primary key auto_increment, #自增主鍵
 *     billId varchar(50) not null default '',#賬單號可以按照自己規則生成 UUID
 *     menuId int not null default 0,#菜品的編號, 也可以使用外鍵
 *     nums int not null default 0,#份數
 *     money double not null default 0, #金額
 *     diningTableId int not null default 0, #餐桌
 *     billDate datetime not null ,#訂單日期
 *     state varchar(50) not null default '' # 狀態 '未結賬' , '已經結賬', '掛單'
 */
public class Bill {
    private Integer id;
    private String billId;
    private Integer menuId;
    private Integer nums;
    private Double money;
    private Integer diningTableId;
    private Date billDate;
    private String state;

    public Bill() {
    }

    public Bill(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state) {
        this.id = id;
        this.billId = billId;
        this.menuId = menuId;
        this.nums = nums;
        this.money = money;
        this.diningTableId = diningTableId;
        this.billDate = billDate;
        this.state = state;
    }

    public Integer getId() {
        return id;
    }

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

    public String getBillId() {
        return billId;
    }

    public void setBillId(String billId) {
        this.billId = billId;
    }

    public Integer getMenuId() {
        return menuId;
    }

    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Double getMoney() {
        return money;
    }

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

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    public Date getBillDate() {
        return billDate;
    }

    public void setBillDate(Date billDate) {
        this.billDate = billDate;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return  id +
                "\t\t" + menuId +
                "\t\t\t" + nums +
                "\t\t\t" + money +
                "\t" + diningTableId +
                "\t\t" + billDate +
                "\t\t" + state ;
    }
}

DiningTable

package com.zwj.mhl.domain;

/**
 * 這是一個javabean 和 diningTable 表對應
 *     id int primary key auto_increment, #自增, 表示餐桌編號
 *     state varchar(20) not null default '',#餐桌的狀態
 *     orderName varchar(50) not null default '',#預訂人的名字
 *     orderTel varchar(20) not null default ''
 */
public class DiningTable {
    private Integer id;
    private String state;
    private String orderName;
    private String orderTel;

    public DiningTable() {
    }

    public DiningTable(Integer id, String state, String orderName, String orderTel) {
        this.id = id;
        this.state = state;
        this.orderName = orderName;
        this.orderTel = orderTel;
    }

    public Integer getId() {
        return id;
    }

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

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public String getOrderTel() {
        return orderTel;
    }

    public void setOrderTel(String orderTel) {
        this.orderTel = orderTel;
    }

    @Override
    public String toString() {
        return id + "\t\t\t" + state;
    }
}

Employee

package com.zwj.mhl.domain;

/**
 * 這是一個javabean 和 employee對應
 * id int primary key auto_increment, #自增
 *     empId varchar(50) not null default '',#員工號
 *     pwd char(32) not null default '',#密碼md5
 *     name varchar(50) not null default '',#姓名
 *     job varchar(50) not null default '' #崗位
 */
public class Employee {
    private Integer id;
    private String empId;
    private String pwd;
    private String name;
    private String job;

    public Employee() { //無參構造器,底層apache-dbutils反射需要
    }

    public Employee(Integer id, String empId, String pwd, String name, String job) {
        this.id = id;
        this.empId = empId;
        this.pwd = pwd;
        this.name = name;
        this.job = job;
    }

    public Integer getId() {
        return id;
    }

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

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

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

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

Menu

package com.zwj.mhl.domain;

/**
 * 該類(javabean)和 menu 表對應
 * id int primary key auto_increment, #自增主鍵,作為菜譜編號(唯一)
 * name varchar(50) not null default '',#菜品名稱
 * type varchar(50) not null default '', #菜品種類
 * price double not null default 0#價格
 */
public class Menu {
    private Integer id;
    private String name;
    private String type;
    private Double price;

    public Menu() {//無參構造器
    }

    public Menu(Integer id, String name, String type, Double price) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.price = price;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

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

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return id + "\t\t\t" + name + "\t\t" + type + "\t\t" + price;
    }
}

MultiTableBean

package com.zwj.mhl.domain;

import java.util.Date;

/**
 * 這是一個javabean 可以和多張表進行對應
 */
public class MultiTableBean {
    private Integer id;
    private String billId;
    private Integer menuId;
    private Integer nums;
    private Double money;
    private Integer diningTableId;
    private Date billDate;
    private String state;
    //增加一個來自menu表的列 name
    //思考 這里的屬性名是否一定要和表的列名保持一致.
    //答: 可以不一致,但是需要sql做相應的修改, 規范需要保持一致.
    private String name;
    //增加來自menu表的列 price
    private Double price;//默認值 null

    public MultiTableBean() {
        System.out.println("反射呼叫....");
    }

//    public MultiTableBean(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state, String name, Double price) {
//        this.id = id;
//        this.billId = billId;
//        this.menuId = menuId;
//        this.nums = nums;
//        this.money = money;
//        this.diningTableId = diningTableId;
//        this.billDate = billDate;
//        this.state = state;
//        this.name = name;
//        this.price = price;
//    }

    //給price生成setter 和 getter

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }


    //給name生成setter 和 getter


    public String getName() {
        return name;
    }

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

    public Integer getId() {
        return id;
    }

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

    public String getBillId() {
        return billId;
    }

    public void setBillId(String billId) {
        this.billId = billId;
    }

    public Integer getMenuId() {
        return menuId;
    }

    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Double getMoney() {
        return money;
    }

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

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    public Date getBillDate() {
        return billDate;
    }

    public void setBillDate(Date billDate) {
        this.billDate = billDate;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return  id +
                "\t\t" + menuId +
                "\t\t\t" + nums +
                "\t\t\t" + money +
                "\t" + diningTableId +
                "\t\t" + billDate +
                "\t\t" + state +
                "\t\t" + name +
                "\t\t" + price;
    }
}

service

BillService

package com.zwj.mhl.service;

import com.zwj.mhl.dao.BillDAO;
import com.zwj.mhl.dao.MultiTableDAO;
import com.zwj.mhl.domain.Bill;
import com.zwj.mhl.domain.MultiTableBean;

import java.util.List;
import java.util.UUID;

/**
 * 處理和賬單相關的業務邏輯
 */
public class BillService {
    //定義BillDAO屬性
    private BillDAO billDAO = new BillDAO();
    //定義MenuService 屬性
    private MenuService menuService = new MenuService();
    //定義DiningTableService屬性
    private DiningTableService diningTableService = new DiningTableService();

    private MultiTableDAO multiTableDAO = new MultiTableDAO();

    //思考
    //撰寫點餐的方法
    //1. 生成賬單
    //2. 需要更新對應餐桌的狀態
    //3. 如果成功回傳true, 否則回傳false
    public boolean orderMenu(int menuId, int nums, int diningTableId) {
        //生成一個賬單號,UUID
        String billID = UUID.randomUUID().toString();

        //將賬單生成到bill表, 要求直接計算賬單金額
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未結賬')",
                billID, menuId, nums, menuService.getMenuById(menuId).getPrice() * nums, diningTableId);

        if (update <= 0) {
            return false;
        }

        //需要更新對應餐桌的狀態
        return diningTableService.updateDiningTableState(diningTableId, "就餐中");

    }

    //回傳所有的賬單, 提供給View呼叫
    public List<Bill> list() {
        return billDAO.queryMulti("select * from bill", Bill.class);
    }

    //回傳所有的賬單并帶有菜品名,價格, 提供給View呼叫
    public List<MultiTableBean> list2() {
        return multiTableDAO.queryMulti("SELECT bill.*, name,price " +
                "FROM bill, menu " +
                "WHERE bill.menuId = menu.id", MultiTableBean.class);
    }


    //查看某個餐桌是否有未結賬的賬單
    public boolean hasPayBillByDiningTableId(int diningTableId) {

        Bill bill =
                billDAO.querySingle("SELECT * FROM bill WHERE diningTableId=? AND state = '未結賬' LIMIT 0, 1", Bill.class, diningTableId);
        return bill != null;
    }

    //完成結賬[如果餐桌存在,并且該餐桌有未結賬的賬單]
    //如果成功,回傳true, 失敗回傳 false
    public boolean payBill(int diningTableId, String payMode) {

        //如果這里使用事務的話,需要用ThreadLocal來解決 , 框架中比如mybatis 提供了事務支持
        //1. 修改bill表
        int update = billDAO.update("update bill set state=? where diningTableId=? and state='未結賬'", payMode, diningTableId);

        if(update <= 0) { //如果更新沒有成功,則表示失敗...
            return false;
        }
        //2. 修改diningTable表
        //注意:不要直接在這里操作,而應該呼叫DiningTableService 方法,完成更新,體現各司其職

        if(!diningTableService.updateDiningTableToFree(diningTableId, "空")) {
            return false;
        }
        return true;

    }

}

DiningTableService

package com.zwj.mhl.service;

import com.zwj.mhl.dao.DiningTableDAO;
import com.zwj.mhl.domain.DiningTable;

import java.util.List;

public class DiningTableService { //業務層
    //定義一個DiningTableDAO物件
    private DiningTableDAO diningTableDAO = new DiningTableDAO();

    //回傳所有餐桌的資訊
    public List<DiningTable> list() {

        return diningTableDAO.queryMulti("select id, state from diningTable", DiningTable.class);
    }

    //根據id , 查詢對應的餐桌DiningTable 物件
    //,如果回傳null , 表示id編號對應的餐桌不存在
    public DiningTable getDiningTableById(int id) {

        //老韓小技巧:把sql陳述句放在查詢分析器去測驗一下.
        return diningTableDAO.querySingle("select * from diningTable where id = ?", DiningTable.class, id);
    }

    //如果餐桌可以預定,呼叫方法,對其狀態進行更新(包括預定人的名字和電話)
    public boolean orderDiningTable(int id, String orderName, String orderTel) {

        int update =
                diningTableDAO.update("update diningTable set state='已經預定', orderName=?, orderTel=? where id=?", orderName, orderTel, id);

        return  update > 0;
    }

    //需要提供一個更新 餐桌狀態的方法
    public boolean updateDiningTableState(int id, String state) {

        int update = diningTableDAO.update("update diningTable set state=? where id=?", state, id);
        return update > 0;
    }

    //提供方法,將指定的餐桌設定為空閑狀態
    public boolean updateDiningTableToFree(int id, String state) {

        int update = diningTableDAO.update("update diningTable set state=?,orderName='',orderTel='' where id=?", state, id);
        return update > 0;
    }

}

EmployeeService

package com.zwj.mhl.service;

import com.zwj.mhl.dao.EmployeeDAO;
import com.zwj.mhl.domain.Employee;

/**
 * 該類完成對employee表的各種操作(通過呼叫EmployeeDAO物件完成)
 */
public class EmployeeService {

    //定義一個 EmployeeDAO 屬性
    private EmployeeDAO employeeDAO = new EmployeeDAO();

    //方法,根據empId 和 pwd 回傳一個Employee物件
    //如果查詢不到,就回傳null
    public Employee getEmployeeByIdAndPwd(String empId, String pwd) {

        return employeeDAO.querySingle("select * from employee where empId=? and pwd=md5(?)", Employee.class, empId, pwd);

    }
}

MenuService

package com.zwj.mhl.service;

import com.zwj.mhl.dao.MenuDAO;
import com.zwj.mhl.domain.Menu;

import java.util.List;

/**
 * 完成對menu表的各種操作(通過呼叫MenuDAO)
 */
public class MenuService {

    //定義MenuDAO 屬性
    private MenuDAO menuDAO = new MenuDAO();

    //回傳所有的菜品, 回傳給界面使用
    public List<Menu> list() {
        return menuDAO.queryMulti("select * from menu", Menu.class);
    }

    //需要方法,根據id, 回傳Menu物件
    public Menu getMenuById(int id) {
        return menuDAO.querySingle("select * from menu where id = ?", Menu.class, id);
    }
}

utils

JDBCUtilsByDruid

package com.zwj.mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 基于druid資料庫連接池的工具類
 */
public class JDBCUtilsByDruid {

    private static DataSource ds;

    //在靜態代碼塊完成 ds初始化
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //撰寫getConnection方法
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    //關閉連接, 老師再次強調: 在資料庫連接池技術中,close 不是真的斷掉連接
    //而是把使用的Connection物件放回連接池
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {

        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

Utility

package com.zwj.mhl.utils;


/**
   工具類的作用:
   處理各種情況的用戶輸入,并且能夠按照程式員的需求,得到用戶的控制臺輸入,
*/

import java.util.*;
/**

   
*/
public class Utility {
   //靜態屬性,,,
    private static Scanner scanner = new Scanner(System.in);

    
    /**
     * 功能:讀取鍵盤輸入的一個選單選項,值:1——5的范圍
     * @return 1——5
     */
   public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一個字符的字串
            c = str.charAt(0);//將字串轉換成字符char型別
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("選擇錯誤,請重新輸入:");
            } else break;
        }
        return c;
    }

   /**
    * 功能:讀取鍵盤輸入的一個字符
    * @return 一個字符
    */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一個字符
        return str.charAt(0);
    }
    /**
     * 功能:讀取鍵盤輸入的一個字符,如果直接按回車,則回傳指定的默認值;否則回傳輸入的那個字符
     * @param defaultValue 指定的默認值
     * @return 默認值或輸入的字符
     */
    
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字串,要么是一個字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
   
    /**
     * 功能:讀取鍵盤輸入的整型,長度小于2位
     * @return 整數
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);//一個整數,長度<=2位
            try {
                n = Integer.parseInt(str);//將字串轉換成整數
                break;
            } catch (NumberFormatException e) {
                System.out.print("數字輸入錯誤,請重新輸入:");
            }
        }
        return n;
    }
    /**
     * 功能:讀取鍵盤輸入的 整數或默認值,如果直接回車,則回傳默認值,否則回傳輸入的整數
     * @param defaultValue 指定的默認值
     * @return 整數或默認值
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }
         
         //例外處理...
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("數字輸入錯誤,請重新輸入:");
            }
        }
        return n;
    }

    /**
     * 功能:讀取鍵盤輸入的指定長度的字串
     * @param limit 限制的長度
     * @return 指定長度的字串
     */

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    /**
     * 功能:讀取鍵盤輸入的指定長度的字串或默認值,如果直接回車,回傳默認值,否則回傳字串
     * @param limit 限制的長度
     * @param defaultValue 指定的默認值
     * @return 指定長度的字串
     */
   
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }


   /**
    * 功能:讀取鍵盤輸入的確認選項,Y或N
    * 將小的功能,封裝到一個方法中.
    * @return Y或N
    */
    public static char readConfirmSelection() {
        System.out.println("請輸入你的選擇(Y/N)");
        char c;
        for (; ; ) {//無限回圈
           //在這里,將接受到字符,轉成了大寫字母
           //y => Y n=>N
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("選擇錯誤,請重新輸入:");
            }
        }
        return c;
    }

    /**
     * 功能: 讀取一個字串
     * @param limit 讀取的長度
     * @param blankReturn 如果為true ,表示 可以讀空字串, 
     *                   如果為false表示 不能讀空字串,
     *           
    * 如果輸入為空,或者輸入大于limit的長度,就會提示重新輸入,
     * @return
     */
    private static String readKeyBoard(int limit, boolean blankReturn) {
        
      //定義了字串
      String line = "";

      //scanner.hasNextLine() 判斷有沒有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//讀取這一行
           
         //如果line.length=0, 即用戶沒有輸入任何內容,直接回車
         if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以回傳空串
                else continue; //如果blankReturn=false,不接受空串,必須輸入內容
            }

         //如果用戶輸入的內容大于了 limit,就提示重寫輸入  
         //如果用戶如的內容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("輸入長度(不能大于" + limit + ")錯誤,請重新輸入:");
                continue;
            }
            break;
        }

        return line;
    }
}

view

MHLView

package com.zwj.mhl.view;

import com.zwj.mhl.domain.*;
import com.zwj.mhl.service.BillService;
import com.zwj.mhl.service.DiningTableService;
import com.zwj.mhl.service.EmployeeService;
import com.zwj.mhl.service.MenuService;
import com.zwj.mhl.utils.Utility;

import java.util.List;
import java.util.UUID;

/**
 * 這是主界面
 */
public class MHLView {

    //控制是否退出選單
    private boolean loop = true;
    private String key = ""; //接收用戶的選擇
    //定義EmployeeService 屬性
    private EmployeeService employeeService = new EmployeeService();
    //定義DiningTableService的屬性
    private DiningTableService diningTableService = new DiningTableService();
    //定義MenuService屬性
    private MenuService menuService = new MenuService();
    //定義BillService屬性
    private BillService billService = new BillService();

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //完成結賬
    public void payBill() {
        System.out.println("==============結賬服務============");
        System.out.print("請選擇要結賬的餐桌編號(-1退出): ");
        int diningTableId = Utility.readInt();
        if (diningTableId == -1) {
            System.out.println("=============取消結賬============");
            return;
        }
        //驗證餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
        if (diningTable == null) {
            System.out.println("=============結賬的餐桌不存在============");
            return;
        }
        //驗證餐桌是否有需要結賬的賬單
        if (!billService.hasPayBillByDiningTableId(diningTableId)) {
            System.out.println("=============該餐位沒有未結賬賬單============");
            return;
        }
        System.out.print("結賬方式(現金/支付寶/微信)回車表示退出: ");
        String payMode = Utility.readString(20, "");//說明如果回車,就是回傳 ""
        if ("".equals(payMode)) {
            System.out.println("=============取消結賬============");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y') { //結賬

            //呼叫我們寫的方法
            if (billService.payBill(diningTableId, payMode)) {
                System.out.println("=============完成結賬============");
            } else {
                System.out.println("=============結賬失敗============");
            }

        } else {
            System.out.println("=============取消結賬============");
        }
    }

    //顯示賬單資訊
    public void listBill() {
//        List<Bill> bills = billService.list();
//        System.out.println("\n編號\t\t菜品號\t\t菜品量\t\t金額\t\t桌號\t\t日期\t\t\t\t\t\t\t狀態");
//        for (Bill bill : bills) {
//            System.out.println(bill);
//        }
//        System.out.println("==============顯示完畢============");

        List<MultiTableBean> multiTableBeans = billService.list2();
        System.out.println("\n編號\t\t菜品號\t\t菜品量\t\t金額\t\t桌號\t\t日期\t\t\t\t\t\t\t狀態\t\t菜品名\t\t價格");
        for (MultiTableBean bill : multiTableBeans) {
            System.out.println(bill);
        }
        System.out.println("==============顯示完畢============");
    }

    //完成點餐
    public void orderMenu() {
        System.out.println("==============點餐服務============");
        System.out.print("請輸入點餐的桌號(-1退出): ");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1) {
            System.out.println("==============取消點餐============");
            return;
        }
        System.out.print("請輸入點餐的菜品號(-1退出): ");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1) {
            System.out.println("==============取消點餐============");
            return;
        }
        System.out.print("請輸入點餐的菜品量(-1退出): ");
        int orderNums = Utility.readInt();
        if (orderNums == -1) {
            System.out.println("==============取消點餐============");
            return;
        }

        //驗證餐桌號是否存在.
        DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
        if (diningTable == null) {
            System.out.println("==============餐桌號不存在============");
            return;
        }
        //驗證菜品編號
        Menu menu = menuService.getMenuById(orderMenuId);
        if (menu == null) {
            System.out.println("==============菜品號不存在============");
            return;
        }

        //點餐
        if (billService.orderMenu(orderMenuId, orderNums, orderDiningTableId)) {
            System.out.println("==============點餐成功============");
        } else {
            System.out.println("==============點餐失敗============");
        }
    }

    //顯示所有菜品
    public void listMenu() {
        List<Menu> list = menuService.list();
        System.out.println("\n菜品編號\t\t菜品名\t\t類別\t\t價格");
        for (Menu menu : list) {
            System.out.println(menu);
        }
        System.out.println("==============顯示完畢============");
    }

    //完成訂座
    public void orderDiningTable() {
        System.out.println("==============預定餐桌============");
        System.out.print("請選擇要預定的餐桌編號(-1退出): ");
        int orderId = Utility.readInt();
        if (orderId == -1) {
            System.out.println("==============取消預訂餐桌============");
            return;
        }
        //該方法得到的是 Y 或者 N
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {//要預定

            //根據orderId 回傳 對應DiningTable物件, 如果為null, 說明該物件不存在
            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable == null) {//
                System.out.println("==============預訂餐桌不存在============");
                return;
            }
            //判斷該餐桌的狀態是否 "空"
            if (!("空".equals(diningTable.getState()))) {//說明當前這個餐桌不是 "空" 狀態
                System.out.println("==============該餐桌已經預定或者就餐中============");
                return;
            }

            //接收預定資訊
            System.out.print("預定人的名字: ");
            String orderName = Utility.readString(50);
            System.out.print("預定人的電話: ");
            String orderTel = Utility.readString(50);

            //更新餐桌狀態
            if (diningTableService.orderDiningTable(orderId, orderName, orderTel)) {
                System.out.println("==============預訂餐桌成功============");
            } else {
                System.out.println("==============預訂餐桌失敗============");
            }

        } else {
            System.out.println("==============取消預訂餐桌============");
        }

    }

    //顯示所有餐桌狀態
    public void listDiningTable() {
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌編號\t\t餐桌狀態");
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("==============顯示完畢============");
    }

    //顯示主選單
    public void mainMenu() {

        while (loop) {

            System.out.println("\n===============滿漢樓================");
            System.out.println("\t\t 1 登錄滿漢樓");
            System.out.println("\t\t 2 退出滿漢樓");
            System.out.print("請輸入你的選擇: ");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("輸入員工號: ");
                    String empId = Utility.readString(50);
                    System.out.print("輸入密  碼: ");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
                    if (employee != null) { //說明存在該用戶
                        System.out.println("===============登錄成功[" + employee.getName() + "]================\n");
                        //顯示二級選單, 這里二級選單是回圈操作,所以做成while
                        while (loop) {
                            System.out.println("\n===============滿漢樓(二級選單)================");
                            System.out.println("\t\t 1 顯示餐桌狀態");
                            System.out.println("\t\t 2 預定餐桌");
                            System.out.println("\t\t 3 顯示所有菜品");
                            System.out.println("\t\t 4 點餐服務");
                            System.out.println("\t\t 5 查看賬單");
                            System.out.println("\t\t 6 結賬");
                            System.out.println("\t\t 9 退出滿漢樓");
                            System.out.print("請輸入你的選擇: ");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    listDiningTable();//顯示餐桌狀態
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();//顯示所有賬單
                                    break;
                                case "6":
                                    payBill();
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("你的輸入有誤,請重新輸入");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("===============登錄失敗================");
                    }
                    break;
                case "2":
                    loop = false;//
                    break;
                default:
                    System.out.println("你輸入有誤,請重新輸入.");
            }
        }
        System.out.println("你退出了滿漢樓系統~");
    }

}

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/509410.html

標籤:Java

上一篇:樹的結點查找和洗掉

下一篇:爬蟲基本原理

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more