dao
BasicDAOdomainBillDAO复制代码
- 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);
- }
- }
- }
DiningTableDAO复制代码
- package com.zwj.mhl.dao;
- import com.zwj.mhl.domain.Bill;
- public class BillDAO extends BasicDAO<Bill> {
- }
EmployeeDAO复制代码
- package com.zwj.mhl.dao;
- import com.zwj.mhl.domain.DiningTable;
- public class DiningTableDAO extends BasicDAO<DiningTable> {
- //如果有特别的操作,可以写在 DiningTableDAO
- }
MenuDAO复制代码
- package com.zwj.mhl.dao;
- import com.zwj.mhl.domain.Employee;
- public class EmployeeDAO extends BasicDAO<Employee> {
- //这里还可以写特有的操作.
- }
MultiTableDAO复制代码
- package com.zwj.mhl.dao;
- import com.zwj.mhl.domain.Menu;
- public class MenuDAO extends BasicDAO<Menu> {
- }
复制代码
- package com.zwj.mhl.dao;
- import com.zwj.mhl.domain.MultiTableBean;
- public class MultiTableDAO extends BasicDAO<MultiTableBean> {
- }
BillserviceDiningTable复制代码
- 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 ;
- }
- }
Employee复制代码
- 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;
- }
- }
Menu复制代码
- 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;
- }
- }
MultiTableBean复制代码
- 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;
- }
- }
复制代码
- 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;
- }
- }
BillServiceutilsEmployeeService复制代码
- 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;
- }
- }
MenuService复制代码
- 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;
- }
- }
复制代码
- 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);
- }
- }
JDBCUtilsByDruidviewUtility复制代码
- 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);
- }
- }
- }
复制代码
- 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);
- }
- }
- }
MHLView复制代码
- 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;
- }
- }
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |