1 public static void checkstandLogPage() { 2 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 3 System.out.println("\t 1.登录系统\n"); 4 System.out.println("\t 2.退出\n"); 5 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 6 System.out.println("按0返回上一级"); 7 do { 8 String choice=ScannerInfoString();//记录输入的字符 9 String regex="[0-2]"; //正则设定输入的为0-2 10 if(choice.matches(regex)){ 11 int info=Integer.parseInt(choice); 12 switch (info) { 13 case 0: 14 mainpage(); 15 break; 16 case 1: 17 int loginTimes=3; 18 while (loginTimes!=0) { 19 loginTimes--;//获取 20 System.out.println("用户名"); 21 String sName=ScannerInfoString(); 22 System.out.println("密 码"); 23 String sPssWord=ScannerInfoString(); 24 ArrayList<SalesMan> salesManInfo=new SalesManDao().checkstandLog(sName); 25 if(salesManInfo == null || salesManInfo.size() == 0){ 26 System.err.println("\t!!用户名输入有误!!\n"); 27 System.out.println("\n剩余登陆次数:" + loginTimes); 28 }else { 29 SalesMan salesMan=salesManInfo.get(0);// 30 if (sPssWord.equals(salesMan.getsPassWord())) { 31 System.out.println("\t ■ ■ ■ ■ ■ ■ ■ ■ 账户成功登陆 ■ ■ ■ ■ ■ ■ ■ ■ "); 32 shoppingSettlementPage(salesMan.getsId()); //shoppingSettlementPage是结算商品的 33 } else { 34 System.err.println("\t!!密码错误!!\n"); 35 System.out.println("\n剩余登陆次数:" + loginTimes); 36 } 37 } 38 } 39 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 40 System.err.println("\t!!您已被开除!"); 41 System.exit(1); 42 break; 43 case 2: 44 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 45 System.out.println("您已经退出系统!"); 46 System.exit(-1); 47 break; 48 default: 49 break; 50 } 51 } 52 System.out.println("输入有误请重新输入!"); 53 } while (true); 54 }
1 public static void shoppingSettlementPage(int salesManSid) { 2 System.out.println("■■■■■■■■■■■■■■■■结算■■■■■■■■■■■■■■■■"); 3 do { 4 System.out.println("按 S 开始购物结算.按 0 返回账户登录界面"); 5 String choiNext=ScannerInfoString(); 6 if("0".equals(choiNext)){ 7 //上一页 8 checkstandLogPage(); 9 }else if ("s".equals(choiNext) || "S".equals(choiNext)) { 10 System.out.println("\n--请输入商品编号或商品名--"); 11 int gid=QueryPrint.querySettlement(); 12 switch (gid) { 13 case -3: 14 break; 15 case -1: 16 System.out.println("售罄!"); 17 break; 18 default: 19 System.out.println("商品编号:"); 20 int shoppingGid=ScannerNum(); 21 ArrayList<Goods> goodsList=new QueryPrint().queryGoodsKey(shoppingGid, null); 22 if(goodsList==null || goodsList.size()==0){ 23 System.out.println("未查询到该商品!"); 24 }else { 25 Goods goods=goodsList.get(0); 26 int gNum=goods.getGnum(); 27 double gPrice=goods.getGprice(); 28 System.out.println("请输入购买数量:"); 29 do { 30 int choicegoodsNum=ScannerNum(); 31 if(choicegoodsNum>gNum){ 32 System.err.println("\t!!仓库储备不足!!"); 33 System.out.println("--请重新输入购买数量--"); 34 }else { 35 double allPrice=Arith.mul(choicegoodsNum, gPrice); 36 System.out.println("\t\t\t 购物车结算\n"); 37 System.out.println("\t\t商品名称\t商品单价\t购买数量\t总价\n"); 38 System.out.println("\t\t" + goods.getGname()+ "\t" + gPrice + " $\t"+ choicegoodsNum + "\t" + allPrice 39 + " $\n"); 40 41 do { 42 System.out.println("确认购买:Y/N"); 43 String chshopping=ScannerInfoString(); 44 if ("Y".equals(chshopping) ||"y".equals(chshopping)) { 45 System.out.println("\n总价:" + allPrice 46 + " $"); 47 System.out.println("\n实际缴费金额"); 48 49 do { 50 double amount=ScannerInfo(); 51 double balance=Arith.sub(amount, allPrice); 52 if(balance<0){ 53 System.err.println("\t!!缴纳金额不足!!"); 54 System.out.println("\n请重新输入缴纳金额($)"); 55 }else { 56 Gsales gSales = new Gsales(goods.getGid(),salesManSid,choicegoodsNum); 57 boolean insert=new GsalesDao().shoppingSettlement(gSales); 58 int goodsNewNum=gNum-choicegoodsNum; 59 Goods newGoods=new Goods(goods.getGid(),goodsNewNum); 60 boolean update = new GoodsDao().updateGoods(3, newGoods); 61 if(update && insert){ 62 System.out.println("找零:"+ balance); 63 System.out.println("\n谢谢光临,欢迎下次惠顾"); 64 }else { 65 System.out.println("!支付失败!"); 66 } 67 shoppingSettlementPage(salesManSid); 68 } 69 } while (true); 70 }else if ("n".equals(chshopping) || "N".equals(chshopping)) { 71 shoppingSettlementPage(salesManSid); 72 } 73 System.out.println("确定购买?"); 74 } while (true); 75 } 76 } while (true); 77 } 78 break; 79 } 80 }else { 81 System.out.println("输入有误!"); 82 } 83 } while (true); 84 }
1 /** 2 * 前台收银-数据库 3 * @param sName 4 * @return 5 */ 6 public ArrayList<SalesMan> checkstandLog(String sName){ 7 ArrayList<SalesMan> salesMansof=new ArrayList<SalesMan>(); 8 conn=DbConn.getconn(); 9 String sql = "SELECT SID,SPASSWORD FROM SALESMAN WHERE SNAME=?"; 10 try { 11 ps=conn.prepareStatement(sql); 12 ps.setString(1, sName); 13 rs=ps.executeQuery(); 14 15 while (rs.next()) { 16 int sId=rs.getInt("sId"); 17 String sPassWord=rs.getString("spassword"); 18 SalesMan salesMan=new SalesMan(sId, sPassWord); 19 salesMansof.add(salesMan); 20 } 21 } catch (SQLException e) { 22 e.printStackTrace(); 23 }finally { 24 DbClose.query(ps, conn, rs); 25 } 26 return salesMansof; 27 }
1 package per.sww.tools; 2 3 import java.math.BigDecimal; 4 5 public class Arith { 6 private static final int DEE_DIV_SCALE=2; 7 8 public Arith() { 9 } 10 11 /** 12 * 加法 13 * @param v1 14 * @param v2 15 * @return 16 */ 17 public static double add(double v1,double v2){ 18 BigDecimal b1=new BigDecimal(Double.toString(v1)); 19 BigDecimal b2=new BigDecimal(Double.toString(v2)); 20 return b1.add(b2).doubleValue(); 21 } 22 23 /** 24 * 乘法 25 * @param v1 26 * @param v2 27 * @return 28 */ 29 public static double mul(double v1,double v2) { 30 BigDecimal b1=new BigDecimal(Double.toString(v1)); 31 BigDecimal b2=new BigDecimal(Double.toString(v2)); 32 return b1.add(b2).doubleValue(); 33 } 34 35 /** 36 * 减法 37 * @param v1 38 * @param v2 39 * @return 40 */ 41 public static double sub(double v1,double v2) { 42 BigDecimal b1=new BigDecimal(Double.toString(v1)); 43 BigDecimal b2=new BigDecimal(Double.toString(v2)); 44 return b1.subtract(b2).doubleValue(); 45 } 46 47 /** 48 * 除法 49 * @param v1 50 * @param v2 51 * @return 52 */ 53 public static double div(double v1,double v2){ 54 return div(v1, v2); 55 } 56 57 /** 58 * 除法 59 * @param v1 60 * @param v2 61 * @param scale 62 * @return 63 */ 64 public static double div(double v1,double v2,int scale){ 65 if(scale<0){ 66 throw new IllegalArgumentException("The scale must be a positive integer or zero"); 67 } 68 BigDecimal b1=new BigDecimal(Double.toString(v1)); 69 BigDecimal b2=new BigDecimal(Double.toString(v2)); 70 return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); 71 } 72 73 /** 74 * 四舍五入 75 * @param v 76 * @param scale 77 * @return 78 */ 79 public static double round(double v,int scale){ 80 if(scale<0){ 81 throw new IllegalArgumentException("The scale must be a positive integer or zero"); 82 } 83 84 BigDecimal bigDecimal=new BigDecimal(Double.toString(v)); 85 BigDecimal one=new BigDecimal("1"); 86 return bigDecimal.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); 87 } 88 }
1 /** 2 * 根据商品 gid or gName查询商品 3 * @param gId 4 * @param gName 5 * @return 6 */ 7 public ArrayList<Goods> queryGoodsKey(int gId, String gName) { 8 ArrayList<Goods> arrayList=new ArrayList<Goods>(); 9 conn=DbConn.getconn(); 10 String sql = "SELECT * FROM GOODS WHERE GID=? OR GNAME=?"; 11 try { 12 ps=conn.prepareStatement(sql); 13 ps.setInt(1, gId); 14 ps.setString(2, gName); 15 rs=ps.executeQuery(); 16 while (rs.next()) { 17 int gid=rs.getInt("gid"); 18 String gname=rs.getString(2); 19 double gprice=rs.getDouble(3); 20 int gnum=rs.getInt(4); 21 22 Goods goods=new Goods(gid, gname, gprice, gnum); 23 arrayList.add(goods); 24 } 25 } catch (SQLException e) { 26 e.printStackTrace(); 27 }finally { 28 DbClose.query(ps, conn, rs); 29 } 30 return arrayList; 31 32 }
乘法沒改啊!!
整了这么半天,豆竟然被别人顺走了
@Rich.T:
你該去看看她前一個問題,我也沒少費心..
@RosonJ: ~_~!!,开个玩笑,别当真
@Rich.T:
沒的事,我相信你也沒少費心
上来pia ji一堆代码,啥也不讲?
啊..那个算法那里算不对
@眉间剪水泛千愁: 那个是哪个?大姐,咱能直接一点详细一点不?
@Rich.T: 就是我贴的那个加减乘除那..算出来的总是加的
@眉间剪水泛千愁:
/** 24 * 乘法 25 * @param v1 26 * @param v2 27 * @return 28 */ 29 public static double mul(double v1,double v2) { 30 BigDecimal b1=new BigDecimal(Double.toString(v1)); 31 BigDecimal b2=new BigDecimal(Double.toString(v2)); 32 return b1.add(b2).doubleValue(); 33 }
@眉间剪水泛千愁: 姐啊,复制的时候走点心啊
@Rich.T: 哪有,我照着敲得...
又在制造bug啊,马虎虫,乘法的方法用的是加法。
围观 代码复制