java项目实战-超市管理系统(六)实体源码与分析

package duan.entity;
/**
 * goods 商品实体类
 * @author duan
 */
public final class Goods
{
 	//数据库Goods表主鍵
	private static int count=0;
	private int gid;
	private String gname;
	private double gprice;
	private int gnum;

	/**
	 * 添加商品信息
	 * @param gname,gprice,gum
	 */
	public Goods(String gname,double gprice,int gum)
	{
		this.gname  = gname;
		this.gprice = gprice;
		this.gnum 	= gum;
		count++;
		gid = count;
	}
	/**
	 * 展示所有商品
	 * @param gid,gname,gprice,gum
	 */
	public Goods(int gid,String gname,double gprice,int gum)
	{
		this.gid	= gid;
		this.gname  = gname;
		this.gprice = gprice;
		this.gnum 	= gum;
	}
	
	/**
	 * 根据编号更改商品信息
	 * @param gid,gum
	 */
	public Goods(int gid,int gnum)
	{
		this.gid	= gid;
		this.gnum 	= gnum;
	}
	/**
	 * 根据编号更改商品信息
	 * @param gid,gprice
	 */
	public  Goods(int gid,double gprice)
	{
		this.gid	= gid;
		this.gprice = gprice;
	}
	
	/**
	 * 根据编号更改商品信息
	 * @param gid,gname
	 */
	public  Goods(int gid,String gname)
	{
		this.gid	= gid;
		this.gname  = gname;
	}

	//共有-get、set-方法。
		public int getGid()
		{
			return gid;
		}
		public void setGid(int gid)
		{
			this.gid = gid;
		}
		public String getGname()
		{
			return gname;
		}
		public void setGname(String gname)
		{
			this.gname = gname;
		}
		public double getGprice()
		{
			return gprice;
		}
		public void setGprice(double gprice)
		{
			this.gprice = gprice;
		}
		public int getGnum()
		{
			return gnum;
		}
		public void setGnum(int gnum)
		{
			this.gnum = gnum;
		}						
}
package duan.entity;

/**
 * Gsales g购物结算实体类
 * @author duan
 */

public final class Gsales
{	private static int count;
	private int gsid;
	private int gId;
	private int sId;
	private int sNum;
	
	private String gName; 
	private double gPrice;
	private int gNum;
	
	/**
	 * 购物结算
	 * @param gId,sId,sNum
	 */
	public Gsales(int gId,int sId, int sNum)
	{
		this.gId = gId;
		this.sId = sId;
		this.sNum = sNum;
		count++;
		setGsid(count);
	}
	
	/**
	 * 展现商品列表
	 * @param gName,gPrice,gNum,allSnum
	 */
	public Gsales(String gName,double gPrice,int gNum)
	{
		this.gName = gName;
		this.gPrice = gPrice;
		this.gNum = gNum;
	}

	//共有set、get
		public int getGId()
		{
			return gId;
		}
		public void setGId(int id)
		{
			gId = id;
		}
		public int getSId()
		{
			return sId;
		}
		public void setSId(int id)
		{
			sId = id;
		}
		public int getSNum()
		{
			return sNum;
		}
		public void setSNum(int num)
		{
			sNum = num;
		}
		public String getGName()
		{
			return gName;
		}
		public void setGName(String name)
		{
			gName = name;
		}
		public double getGPrice()
		{
			return gPrice;
		}
		public void setGPrice(double price)
		{
			gPrice = price;
		}
		public int getGNum()
		{
			return gNum;
		}
		public void setGNum(int num)
		{
			gNum = num;
		}

		public int getGsid() {
			return gsid;
		}

		public void setGsid(int gsid) {
			this.gsid = gsid;
		}

}

package duan.entity;

/**
 * SalesMan 售货员实体类
 * @author duan
 */

public final class SalesMan
{	private static int count;
	private int sId;
	private String sName;
	private String sPassWord;
	
	/**
	 * 验证用户登陆
	 * @param sId,spassWord
	 */
	public SalesMan(int sId,String spassWord)
	{
		this.sId = sId;
		this.sPassWord = spassWord;
	}
	
	/**
	 * 查询用户、更改用户密码
	 * @param sId,sName,sPassWord
	 */
	public SalesMan(int sId,String sName,String sPassWord)
	{
		this.sId = sId;
		this.sName = sName;
		this.sPassWord = sPassWord;
	}
	
	/**
	 * 添加用户
	 * @param NsNameame,sPassWord
	 */
	public SalesMan(String NsNameame, String sPassWord)
	{
		this.sName = NsNameame;
		this.sPassWord = sPassWord;
		count++;
		sId = count;
	}

		
	//共有get.set方法
		public int getSId()
		{
			return sId;
		}
		public void setSId(int id)
		{
			sId = id;
		}
		public String getSName()
		{
			return sName;
		}
		public void setSName(String name)
		{
			sName = name;
		}
		public String getSPassWord()
		{
			return sPassWord;
		}
		public void setSPassWord(String passWord)
		{
			sPassWord = passWord;
		}

}

1.所有实体中,域中的字段都是private的,然后又get和set方法获取和设置字段,体现了封装的思想。

2.每个实体都有一个static的count字段。构造方法里面都有count++,用来自动生成ID号,然后再将这个值赋予各自的ID,这样就能每新建一个实体都自动生成一个ID,缺点就是,如果程序不是第一次启动,数据库里面可能已经有值了,而这个count还默认从0开始,这时候加入数据库就会出错,因为所有ID值在数据库里面都是对应的主键。完善的方法是count的初始值从数据库里面读取。

3.每个实体的构造方法都有不同的重载方法,这样调用时就会更加灵活和方便。

4.为了源码完整性,补充tool包,后面就不分析啦。

package duan.tools;

import java.util.Scanner;

import duan.page.GoodsPage;
import duan.page.MainPage;
import duan.page.SalesManPage;

/**
 * 1.各種完成操作后的 选择下一步
 * 2.界面选择操作
 * @author duan
 */

public class ScannerChoice
{
	/**
	 * @return double 键盘获取商品价格,小数点后两位
	 */
	public static double ScannerInfo()
	{
		double num = 0.00;
		do
		{
			Scanner sc = new Scanner(System.in);
			System.out.print("保留小数点后两位,请输入:");
			String info = sc.next();
			
			String regex = "(([1-9][0-9]*)\\.([0-9]{2}))|[0]\\.([0-9]{2})";//保留小数点后2位小数
			if (info.matches(regex))
			{
				num = Double.parseDouble(info);
			}else 
				{
					System.err.println("!输入有误!");
					continue;
				}
			break;
		} while (true);
			
		return num;
	}
	
	/**
	 * @return int 键盘获取商品数量
	 */
	public static int ScannerNum()
	{
		int num = 0;
		String regex = "([1-9])|([1-9][0-9]+)";//商品数量
		do
		{
			Scanner sc = new Scanner(System.in);
			System.out.print("请输入:");
			String nums = sc.next();
			
			if (nums.matches(regex))
			{
				num = Integer.parseInt(nums);
			}else 
				{
					System.err.println("!输入有误!");
					continue;
				}
		 break;
		} while (true);
	 return num;
	}
		
	/**
	 * @return String 获取的键盘输入
	 */
	public static String ScannerInfoString()
	{
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入:");
		return scanner.next();
	}
		
	/**
	 * 获取用户-更改完商品-下一步
	 * 获取用户-删除完商品-下一步
	 * 获取用户-添加完商品-下一步
	 * @param 调用者
	 */
		public static void changedInfoNext(String oper)
		{		
			do
			{
				System.out.println("是否继续进行-当前操作:(Y/N)");
				String choice = ScannerChoice.ScannerInfoString();
				
				//在JAVA中: Equals比较的是值,==比较的是地址
				 if ("y".equals(choice) || "Y".equals(choice)) 
					{
						//下面的嵌套if-else 是让用户选择继续操作当前步骤所跳转到指定页面。(因为不同函数调用,跳转的指定函数不同)
						if ("upateGoodsPage".equals(oper))
							{
								 GoodsPage.updateGoodsPage();
							}else if ("deleteGoodsPage".equals(oper)) 
									{
										GoodsPage.deleteGoodsPage();
									}else if ("addGoodsPage".equals(oper))
											 {
		 										GoodsPage.addGoodsPage();
	 					 					 }
						//上面的嵌套结束
					}else if ("N".equals(choice) || "n".equals(choice)) 
							{
			 					MainPage.MaintenancePage();
							}
			 	System.out.println("\n输入有误!请重新输入.");
			} while (true);
		}
	
		/**
		 * 获取用户-更改-完售货员-下一步
		 * 获取用户-添加-完售货员-下一步
		 * 获取用户-查询-完售货员-下一步
		 * 获取用户-删除-完售货员-下一步
		 * @param 调用者
		 */
			public static void choiceSalesManNext(String oper)
			{	
				 do
				{		
					System.out.println("是否继续进行-当前操作:(Y/N)");
					String choice = ScannerChoice.ScannerInfoString();
				
					 if ( "y".equals(choice) || "Y".equals(choice) ) 
						{
							//下面的嵌套if-else 是让用户选择继续操作当前步骤所跳转到指定页面。(因为不同函数调用,跳转的指定函数不同)
							if ("updateSalesMan".equals(oper))
								{
									 SalesManPage.updateSalesManPage();
								}else if ("deleteSalesMan".equals(oper)) 
										{
											SalesManPage.deleteSalesManPage();
										}else if ("addSalesMan".equals(oper))
												 {
													SalesManPage.addSalesManPage();
		 					 					 }else if ("querySalesMan".equals(oper)) 
		 					 					 		{
		 					 						 		SalesManPage.querySalesManPage();	
		 					 					 		} 
							//上面的嵌套结束
						}else if ("N".equals(choice) || "n".equals(choice)) 
								{
									MainPage.salesManManagementPage();
								}
				 	System.err.println("\t输入有误!");
				} while (true);
			}	
			
}

package duan.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import duan.dao.GoodsDao;
import duan.db.DbClose;
import duan.db.DbConn;
import duan.entity.Goods;
import duan.entity.SalesMan;

/**
 * 查询&&打印 函数工具(后期优化可能会删)
 *@author duan
 */

public final class QueryPrint
{
		Connection        conn  = null;
		PreparedStatement pstmt = null;
		ResultSet 		rs 	 	 = null;
	
	/**
	 * 模糊查询并陈列查询信息函数小工具
	 * @param oper 调用者
	 * @return 查询到的信息的gid,如果返回值等于-1,则代表查询异常。	  
	 */
		public static int query(String oper)
		{
			int gid = -1;
			String shopping = ScannerChoice.ScannerInfoString(); //键盘获取商品名字
			ArrayList goodsList = new QueryPrint().queryGoodsKey(-1,shopping);  //根据键盘获取的商品名字調用 精确查询函数,確定用戶所要操作的数据
			    if (goodsList == null || goodsList.size() <= 0)
				{
						System.err.println("\t!!查无此商品 !!");
						
						//調用选择下一步函数
						ScannerChoice.changedInfoNext(oper);
						
				}else	//查到有此商品,实现进行 更改商品 信息操作!
					{		
						Goods goods = goodsList.get(0);
						
						System.out.println("\t\t\t\t\t商品列表\n\n");
						System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n");
						System.out.print("\t"+goods.getGid()+"\t\t"+goods.getGname()+"\t\t"+goods.getGprice()+"\t\t"+goods.getGnum());
						if (goods.getGnum()==0)
						{
							System.out.println("\t\t该商品已售空");
						}else if (goods.getGnum()<10)
								{
									System.out.println("\t\t该商品已不足10件");
								}else 
									{
										System.out.println("\t\t-");
									}
						gid = goods.getGid(); //将商品编号返回给调用者
					}
		 return gid;
		} 
	
	/**
	 * 模糊查询函数小工具
	 * @return int 当商品件数有且只有一件时返回商品gid号,商品已售空时返回 -1. >1件时返回-2 . 查无此商品时返回-3
	 * 					  
	 */
		public static int querySettlement() 
		{
			int gid = -1;
			ArrayList goodsSettlement = new GoodsDao().queryGoods(3);//調用 关键字查询函数
		    if (goodsSettlement == null || goodsSettlement.size() <= 0)
			{
					System.err.println("\t!!查无此商品 !!\n");
					gid = -3;	
			}else	//查到有此商品,实现进行 更改商品 信息操作!
				{	
					System.out.println("\t\t\t\t\t商品列表\n\n");
					System.out.println("\t商品编号\t\t商品名称\t\t商品价格\t\t商品数量\t\t备注\n");
					for (int i = 0; i < goodsSettlement.size(); i++)
					{
						Goods goods = goodsSettlement.get(i);
						if (goods.getGnum() > 0)
						{
							System.out.print("\t"+goods.getGid()+"\t\t"+goods.getGname()+"\t\t"+goods.getGprice()+"\t\t"+goods.getGnum());
								
							if (goods.getGnum()==0)
							{
								System.out.println("\t\t该商品已售空");
							}else if (goods.getGnum()<10)
									{
										System.out.println("\t\t该商品已不足10件");
									}else 
										{
											System.out.println("\t\t-");
										}
								if (goodsSettlement.size()==1)
								{
									gid = goods.getGid(); //将商品编号返回给调用者
								}else 
									{
										gid = -2;
									}
						}
					}
				}
		 return gid; 
		} 
	

	/**
	 * 根据商品 gid or gName查询商品
	 * @param 商品id,商品名称
	 * @return 商品信息
	 */
		public ArrayList queryGoodsKey(int gId,String gName) 
		{										
			ArrayList goodsList = new ArrayList();
			conn = DbConn.getconn();	
			
			String sql = "SELECT * FROM GOODS WHERE GID=? OR GNAME=?"; 
			   try
			   {
					pstmt = conn.prepareStatement(sql);
					pstmt.setInt(1, gId);
					pstmt.setString(2, gName);
					rs = pstmt.executeQuery();
					while (rs.next())
					{
						int gid = rs.getInt("gid");
						String gname = rs.getString(2);
						double gprice = rs.getDouble(3);
						int gnum = rs.getInt(4);
						
						Goods goods = new Goods(gid,gname,gprice,gnum);
						goodsList.add(goods);
					}
				} catch (SQLException e)
				{
					e.printStackTrace();
				}finally
						{
							DbClose.queryClose(pstmt, rs, conn);
						}
		  return goodsList;
		}	
		
	/**
	 * 精确查询售货员信息
	 * @param 售货员名字
	 * @return 
	 */
	public ArrayList querySalesMan(String sName)
	{
		ArrayList SalesManList = new ArrayList();
		conn = DbConn.getconn();	
		String sql = "SELECT * FROM SALESMAN WHERE SNAME=?";
		   try
		   {
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, sName);
				rs = pstmt.executeQuery();
				while (rs.next())
				{
					int sid = rs.getInt("sid");
					String sname = rs.getString(2);
					String sPassWord = rs.getString(3);
					
					SalesMan salesMan = new SalesMan(sid,sname,sPassWord);
					SalesManList.add(salesMan);
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}finally
					{
						DbClose.queryClose(pstmt, rs, conn);
					}
		return SalesManList;
	}
}

package duan.tools;

import java.math.BigDecimal;

/**  
 * 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精  
 * 确的浮点数运算,包括加减乘除和四舍五入.
 * 
 * 注意:此代码将加减乘除四舍五入方法已实现,但本项目中并未全部使用.
 * @author lyons(zhanglei)
 */  

public class Arith
{
	private static final int DEF_DIV_SCALE = 2; //默认除法运算精度
	private Arith(){} 
	
	/**  
	* 提供精确的加法运算。  
	* @param v1 被加数  
	* @param v2 加数  
	* @return 两个参数的和  
	*/  
	public static double add(double v1,double v2)
	{   
		BigDecimal b1 = new BigDecimal(Double.toString(v1));   
		BigDecimal b2 = new BigDecimal(Double.toString(v2));   
		return b1.add(b2).doubleValue();   
	} 
	
	/**  
	* 提供精确的减法运算。  
	* @param v1 被减数  
	* @param v2 减数  
	* @return 两个参数的差  
	*/  
	public static double sub(double v1,double v2)
	{   
		BigDecimal b1 = new BigDecimal(Double.toString(v1));   
		BigDecimal b2 = new BigDecimal(Double.toString(v2));   
		return b1.subtract(b2).doubleValue();   
	}   
	
	/**  
	* 提供精确的乘法运算。  
	* @param v1 被乘数  
	* @param v2 乘数  
	* @return 两个参数的积  
	*/  
	public static double mul(double v1,double v2)
	{   
		BigDecimal b1 = new BigDecimal(Double.toString(v1));   
		BigDecimal b2 = new BigDecimal(Double.toString(v2));   
		return b1.multiply(b2).doubleValue();   
	}   
	
	/**  
	* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到  
	* 小数点以后2位,以后的数字四舍五入。  
	* @param v1 被除数  
	* @param v2 除数  
	* @return 两个参数的商  
	*/  
	public static double div(double v1,double v2)
	{   
		return div(v1,v2,DEF_DIV_SCALE);   
	}   
	
	/**  
	* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指  
	* 定精度,以后的数字四舍五入。  
	* @param v1 被除数  
	* @param v2 除数  
	* @param scale 表示表示需要精确到小数点以后几位。  
	* @return 两个参数的商  
	*/  
	public static double div(double v1,double v2,int scale)
	{   
		if(scale<0)
		{   
			throw new IllegalArgumentException(   
			"The scale must be a positive integer or zero");   
		}   
		BigDecimal b1 = new BigDecimal(Double.toString(v1));   
		BigDecimal b2 = new BigDecimal(Double.toString(v2));   
		return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue(); //BigDecimal.ROUND_HALF_UP 向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。 
	}   
	
	/**  
	* 提供精确的小数位四舍五入处理。  
	* @param v 需要四舍五入的数字  
	* @param scale 小数点后保留几位  
	* @return 四舍五入后的结果  
	*/  
	public static double round(double v,int scale)
	{   
		if(scale<0)
		{   
			throw new IllegalArgumentException("The scale must be a positive integer or zero");   
		}   
		BigDecimal b = new BigDecimal(Double.toString(v));   
		BigDecimal one = new BigDecimal("1");   
		return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();   
	}   
}


你可能感兴趣的:(Java,项目实战)