【Java】贴一个自动贩卖机系统
1 package com.xt.vending; 2 3 import java.util.Scanner; 4 /** 5 * 6 * 7 * 自动贩卖机 8 * 实现功能: 9 *1.充值 10 *2.查看所有商品 11 *3.购买 12 *4.找零 13 *5.输出购物清单 14 *6.后台(物品售卖时间) 15 * 16 * @author 菠萝 17 * 18 * @date 2017年5月13日 下午4:41:24 19 * 20 * 21 */ 22 public class Main 23 { 24 public static void main(String[] args) 25 { 26 27 Menu me = new Menu(); 28 double inputmoney = 0; 29 int id2 = me.SearchLast(); //获取sell+info 表中总共有多少条数据 30 int id = me.SearchLast(),num1=0; //num1 表示客户购买的第几件商品 31 double temp=0; 32 while(true){ 33 System.out.println("╔════════════════════════════╗"); 34 System.out.println(" 本机器支持微信 支付宝付款 "); 35 System.out.println(" 机器出现故障 请拨打妖妖灵 "); 36 System.out.println("╠════════════════════════════╣"); 37 System.out.println(" 请充值: "); 38 System.out.println(" 输入 0 查看商品列表 "); 39 System.out.println("╚════════════════════════════╝"); 40 Scanner scan = new Scanner(System.in); 41 double money = scan.nextDouble(); 42 inputmoney = inputmoney+money; 43 money+=temp; 44 me.showmenu(); 45 System.out.println("╔════════════════════════════╗"); 46 System.out.println(" 当前余额"+money+"元 "); 47 System.out.println("╠════════════════════════════╣"); 48 System.out.println(" 请选择您要购买的商品 "); 49 System.out.println("╚════════════════════════════╝"); 50 Scanner scan1 = new Scanner(System.in); 51 int ID = scan1.nextInt(); 52 if(me.Number(ID)>0){ 53 if(money>=me.Price(ID)){ 54 System.out.println("╔════════════════════════════════════════════════╗"); 55 System.out.println(" "+me.Name(ID)+"已出货 ,请查收 谢谢惠顾 ! "); 56 System.out.println("╚════════════════════════════════════════════════╝"); 57 id++;num1++; 58 me.SellInfo(id, ID, num1); 59 money=money-me.Price(ID); 60 temp = money; 61 me.minNumber(ID); 62 System.out.println("╔════════════════════════════╗"); 63 System.out.println(" 1.继续购买 "); 64 System.out.println(" 2.退款 "); 65 System.out.println("╚════════════════════════════╝"); 66 Scanner san = new Scanner(System.in); 67 int a = san.nextInt(); 68 if(a==2) 69 { 70 System.out.println("╔════════════════════════════╗"); 71 System.out.println(" 退款"+money+"元 "); 72 System.out.println("╚════════════════════════════╝"); 73 me.ShowSellmenu(id2, num1, money, inputmoney); 74 money=0;num1 = 0; 75 break;} 76 if(a!=1&&a!=2){ 77 System.out.println("错误指令");} 78 }else{ 79 System.out.println("╔════════════════════════════╗"); 80 System.out.println(" 金额不足,购买失败 "); 81 System.out.println("╠════════════════════════════╣"); 82 System.out.println(" 1.继续充值 "); 83 System.out.println(" 2.退款 "); 84 System.out.println("╚════════════════════════════╝"); 85 Scanner san = new Scanner(System.in); 86 int a = san.nextInt(); 87 if(a==2) 88 { 89 System.out.println("╔════════════════════════════╗"); 90 System.out.println(" 退款"+money+"元 "); 91 System.out.println("╚════════════════════════════╝"); 92 me.ShowSellmenu(id2, num1, money, inputmoney); 93 money=0;num1 = 0; 94 break;} 95 if(a!=1&&a!=2){ 96 System.out.println("错误指令");} 97 temp = money;} 98 } 99 if(me.Number(ID)==0){ 100 System.out.println("╔════════════════════════════╗"); 101 System.out.println(" 商品已售完 ! "); 102 System.out.println("╠════════════════════════════╣"); 103 System.out.println(" 1.继续购买 "); 104 System.out.println(" 2.退款 "); 105 System.out.println("╚════════════════════════════╝"); 106 Scanner san = new Scanner(System.in); 107 int a = san.nextInt(); 108 if(a==2) 109 { 110 System.out.println("╔════════════════════════════╗"); 111 System.out.println(" 退款"+money+"元 "); 112 System.out.println("╚════════════════════════════╝"); 113 me.ShowSellmenu(id2, num1, money, inputmoney); 114 money=0;num1= 0; 115 break;} 116 if(a!=1&&a!=2){ 117 System.out.println("错误指令");} 118 temp = money;} 119 120 } 121 } 122 }
1 package com.xt.vending; 2 3 import java.sql.Connection; 4 import java.sql.Date; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 import java.text.DateFormat; 11 import java.text.SimpleDateFormat; 12 13 public class Menu 14 { 15 Connection conn = null; 16 Statement state = null; 17 ResultSet rs = null; 18 //商品清单 19 public void showmenu(){ 20 String sql = "select * from vending_info"; 21 try { 22 conn = DBUtil.getDBConn(); 23 state = conn.createStatement(); 24 rs = state.executeQuery(sql); 25 if(state.execute(sql)){ 26 rs = state.getResultSet(); 27 System.out.println("ID\t品名\t\t库存\t价格\t单位\t"); 28 while(rs.next()){ 29 System.out.print(rs.getInt(1) + "\t"); 30 System.out.print(rs.getString(2) + "\t\t"); 31 System.out.print(rs.getInt(3) + "\t"); 32 System.out.print(rs.getDouble(4) + "\t"); 33 System.out.print(rs.getString(5) + "\t"); 34 System.out.println(""); 35 } 36 37 } 38 } catch (Exception e) { 39 System.out.println("错误报告:" + e); 40 }finally{ 41 DBUtil.closeConn(conn); 42 DBUtil.closeStatement(state); 43 DBUtil.closeResultSet(rs);} 44 } 45 // 商品数量减一 46 public void minNumber(int ID){ 47 String sql = "update vending_info set number = number-1 where ID='"+ ID +"'"; 48 try { 49 Class.forName("com.mysql.jdbc.Driver"); 50 conn = DBUtil.getDBConn(); 51 state = conn.createStatement(); 52 state.executeUpdate(sql); 53 System.out.println(""); 54 } catch (Exception e) { 55 System.out.println("错误报告:" + e); 56 }finally{ 57 DBUtil.closeConn(conn); 58 DBUtil.closeStatement(state); 59 DBUtil.closeResultSet(rs);} 60 } 61 // 获取当前商品价格 62 public double Price(int ID){ 63 double price = 0 ; 64 String sql = "select price from vending_info where ID ='"+ ID +"'"; 65 java.sql.PreparedStatement ps = null; 66 ResultSet rs = null; 67 try{ 68 Class.forName("com.mysql.jdbc.Driver"); 69 conn = DBUtil.getDBConn(); 70 ps = conn.prepareStatement(sql); 71 rs = ps.executeQuery(); 72 while(rs.next()){ 73 price= rs.getDouble(1);} 74 } catch (Exception e) { 75 System.out.println("错误报告:" + e); 76 }finally{ 77 DBUtil.closeConn(conn); 78 DBUtil.closeStatement(state); 79 DBUtil.closeResultSet(rs);} 80 return price; 81 } 82 // 获取当前商品数量 83 public int Number(int ID){ 84 int number = 0; 85 String sql = "select number from vending_info where ID ='"+ ID +"'"; 86 java.sql.PreparedStatement ps = null; 87 ResultSet rs = null; 88 try{ 89 Class.forName("com.mysql.jdbc.Driver"); 90 conn = DBUtil.getDBConn(); 91 ps = conn.prepareStatement(sql); 92 rs = ps.executeQuery(); 93 while(rs.next()){ 94 number = rs.getInt(1);} 95 } catch (Exception e) { 96 System.out.println("错误报告:" + e); 97 }finally{ 98 DBUtil.closeConn(conn); 99 DBUtil.closeStatement(state); 100 DBUtil.closeResultSet(rs);} 101 return number; 102 } 103 // 当前商品名称 104 public String Name(int ID){ 105 String sql = "select name from vending_info where ID ='"+ ID +"'"; 106 String name = null; 107 try { 108 Class.forName("com.mysql.jdbc.Driver"); 109 conn = DBUtil.getDBConn(); 110 state = conn.createStatement(); 111 if(state.execute(sql)){ 112 rs = state.getResultSet(); 113 while(rs.next()){ 114 name = rs.getString(1); 115 } 116 117 } 118 } catch (Exception e) { 119 System.out.println("错误报告:" + e); 120 }finally{ 121 DBUtil.closeConn(conn); 122 DBUtil.closeStatement(state); 123 DBUtil.closeResultSet(rs);} 124 return name; 125 } 126 //售出清单 127 //序号 商品ID 商品名称 商品价格 售出时间 128 public void SellInfo(int id,int ID,int num1) { 129 java.util.Date date = new java.util.Date(); 130 DateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); 131 String time = format.format(date); 132 String sql = "insert into sell_info (numb,number,ID,name,price,date) values ("+ "'"+id +"','"+num1+"','"+ ID +"','"+Name(ID)+"','"+Price(ID)+"','"+time+"')"; 133 Connection conn = null; 134 Statement state = null; 135 try{ 136 Class.forName("com.mysql.jdbc.Driver"); 137 conn = DBUtil.getDBConn(); 138 state = conn.createStatement(); 139 state.executeUpdate(sql); 140 } catch (ClassNotFoundException e) { 141 e.printStackTrace(); 142 } catch (SQLException e) { 143 e.printStackTrace(); 144 }finally{ 145 try { 146 state.close(); 147 conn.close(); 148 } catch (SQLException e) { 149 e.printStackTrace();} 150 } 151 } 152 // 展示给客户自己买的东西 153 public void ShowSellmenu(int id2,int num1,double money,double inputmoney){ 154 int x = id2; 155 double y = inputmoney-money; 156 String sql ="select * from sell_info limit " + x + ","+ num1; 157 try { 158 Class.forName("com.mysql.jdbc.Driver"); 159 conn = DBUtil.getDBConn(); 160 state = conn.createStatement(); 161 rs = state.executeQuery(sql); 162 System.out.println("---------------------菠萝自动贩卖机--------------------"); 163 System.out.println(""); 164 System.out.println("序号\t商品ID\t品名\t\t价格\t订单时间\t"); 165 while(rs.next()){ 166 System.out.print(rs.getInt(2) + "\t"); 167 System.out.print(rs.getInt(3) + "\t"); 168 System.out.print(rs.getString(4) + "\t\t"); 169 System.out.print(rs.getInt(5) + "\t"); 170 System.out.print(rs.getString(6) + "\t"); 171 System.out.println("");} 172 System.out.println(""); 173 System.out.println("\t\t\t合计:"+y); 174 System.out.println("\t\t\t实收:"+inputmoney); 175 System.out.println("\t\t\t找零:"+money); 176 System.out.println("\t谢谢惠顾!"); 177 System.out.println("\t\t欢迎下次光临"); 178 System.out.println("\t\tTel:妖妖零"); 179 } catch (Exception e) { 180 System.out.println("错误报告:" + e); 181 }finally{ 182 DBUtil.closeConn(conn); 183 DBUtil.closeStatement(state); 184 DBUtil.closeResultSet(rs);} 185 } 186 // 查询本次购买前总共有多少条数据 187 public int SearchLast(){ 188 int lastnumber = 0; 189 String sql = "select count(*) from sell_info"; 190 java.sql.PreparedStatement ps = null; 191 ResultSet rs = null; 192 try{ 193 Class.forName("com.mysql.jdbc.Driver"); 194 conn = DBUtil.getDBConn(); 195 ps = conn.prepareStatement(sql); 196 rs = ps.executeQuery(); 197 while(rs.next()){ 198 lastnumber = rs.getInt(1);} 199 } catch (Exception e) { 200 System.out.println("错误报告:" + e); 201 }finally{ 202 DBUtil.closeConn(conn); 203 DBUtil.closeStatement(state); 204 DBUtil.closeResultSet(rs);} 205 return lastnumber; 206 } 207 }
1 package com.xt.vending; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 10 public class DBUtil { 11 12 private static final String DB_DRIVER = "com.mysql.jdbc.Driver"; 13 14 private static final String URL = "jdbc:mysql://localhost:3306/vending"; 15 16 private static final String USER_NAME = "root"; 17 18 private static final String PASSWORD = "19981112.bl"; 19 20 21 /** 22 * 23 * @return 24 */ 25 public static Connection getDBConn(){ 26 Connection conn = null; 27 try { 28 Class.forName(DB_DRIVER); 29 30 return DriverManager.getConnection(URL, USER_NAME, PASSWORD); 31 } catch (ClassNotFoundException e) { 32 System.out.println("驱动加载出现异常:" + e); 33 } catch (SQLException e) { 34 System.out.println("数据库链接出现异常 :" + e); 35 } 36 return conn; 37 } 38 39 /** 40 * 41 * @param conn 42 */ 43 public static void closeConn(Connection conn){ 44 45 try { 46 if(conn != null){ 47 conn.close(); 48 } 49 } catch (SQLException e) { 50 System.out.println("数据库链接关闭出现异常:" + e); 51 } 52 53 } 54 55 /** 56 * 57 * @param conn 58 */ 59 public static void closeStatement(Statement stat){ 60 61 try { 62 if(stat != null){ 63 stat.close(); 64 } 65 } catch (SQLException e) { 66 System.out.println("Statement关闭出现异常:" + e); 67 } 68 69 } 70 71 72 73 /** 74 * 75 * @param conn 76 */ 77 public static void closeResultSet(ResultSet rs){ 78 79 try { 80 if(rs != null){ 81 rs.close(); 82 } 83 } catch (SQLException e) { 84 System.out.println("ResultSet关闭出现异常:" + e); 85 } 86 87 } 88 }
CREATE TABLE `sell_info` ( `numb` int(20) NOT NULL, `number` int(20) NOT NULL, `ID` int(20) NOT NULL, `name` varchar(20) NOT NULL, `price` double(20,2) NOT NULL, `date` date NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `vending_info` ( `ID` int(20) NOT NULL, `name` enum('水晶葡萄','芬达','农夫山泉','冰红茶','可乐','脉动','红牛','酒鬼花生','雪碧') NOT NULL, `number` int(20) DEFAULT NULL, `price` double(20,1) NOT NULL, `type` enum('瓶','袋','听') NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
利用MYSQL数据库,制作一个自动贩卖机系统。初学阶段,代码显得有点凌乱,但最基本的功能实现了。