1:先写一个类,包括商品的基本属性
package com.xt.java.base25; public class Goods { private int ID; private String name; private int number; private double price; /** * @return the iD */ public int getID() { return ID; } /** * @param iD the iD to set */ public void setID(int iD) { ID = iD; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the number */ public int getNumber() { return number; } /** * @param number the number to set */ public void setNumber(int number) { this.number = number; } /** * @return the price */ public double getPrice() { return price; } /** * @param price the price to set */ public void setPrice(double price) { this.price = price; } }
2:将数据库和Java连接起来,并写成一个单独的类,使用起来更加方便
package com.xt.java.base25; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class GoodDAO { private Connection conn; private Statement stat; private String url="jdbc:mysql://localhost:3306/lyxdatabases"; private String dbDriver="com.mysql.jdbc.Driver"; private String userName="root"; private String password="1234"; //私有的构造方法,只有在本类中可以进行实例化一次,为了保护用户的隐私使用这种方法,切记!!! private GoodDAO(){ } private static GoodDAO gda=null; /** * 写一个方法,从方法中获得实例,只能实例化一次。 */ public static GoodDAO getGoodDAO(){ if(gda==null){ gda=new GoodDAO(); } return gda; } public Connection getConnection() throws Exception{ try { Class.forName(dbDriver); return DriverManager.getConnection(url,userName,password); } catch (ClassNotFoundException e) { throw new ClassNotFoundException("数据库找不到驱动!!"); } catch (SQLException e) { throw new SQLException("数据库连接异常!!"); } } //关闭Connection public void closeConnection(Connection conn){ try{ if(conn!=null){ conn.close(); } }catch(Exception e){ System.out.println(e); } } //关闭Statement public void closeStatement(Statement stat){ try{ if(stat!=null){ stat.close(); } }catch(Exception e){ System.out.println(e); } } //关闭ResultSet public void closeResultSet(ResultSet rs){ try{ if(rs!=null){ rs.close(); } }catch(Exception e){ System.out.println(e); } } }
3;主方法,直接实现功能
package com.xt.java.base25; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class Main { GoodDAO gDao=GoodDAO.getGoodDAO(); Connection conn; Statement stat; ResultSet rs; Scanner scanner=new Scanner(System.in); static double sumMonNum; //显示货物清单, 清单要求包含每种商品的剩余数量。 public void showList(){ String sql="select *from autoGoods"; try { conn=gDao.getConnection(); stat=conn.createStatement(); rs=stat.executeQuery(sql); while(rs.next()){ System.out.println("商品编号:"+rs.getString("ID")); System.out.print("商品名称:"+rs.getString("name")); System.out.print(" 商品剩余数量:"+rs.getInt("number")); System.out.print("商品单价:"+rs.getDouble("price")+"\n\n"); } } catch (Exception e) { e.printStackTrace(); }finally{ gDao.closeResultSet(rs); gDao.closeStatement(stat); gDao.closeConnection(conn); } } //选择一个商品编号购买东西 public void buyGoods(){ System.out.println("请输入你要购买的商品编号:"); int buyID=scanner.nextInt(); System.out.println("请投币:"); sumMonNum=scanner.nextDouble(); System.out.println("您的商品将要出货!!!"); String sql="update autoGoods set number=(number-1) where ID='"+buyID+"'"; String sql1="select price from autoGoods where ID='"+buyID+"'"; try { conn=gDao.getConnection(); stat=conn.createStatement(); rs=stat.executeQuery(sql1); while(rs.next()){ sumMonNum=sumMonNum-rs.getDouble("price"); System.out.println("您的零钱为"+sumMonNum); } if(stat.executeUpdate(sql)>0){ System.out.println("购买成功!!"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ gDao.closeResultSet(rs); gDao.closeStatement(stat); gDao.closeConnection(conn); } } //询问继续的操作 public void askOpration(){ while(true){ System.out.println("--------请选择您接下来得操作-------"); System.out.println("继续购买-------1"); System.out.println("结束购买,找零-------2"); int operationNum=scanner.nextInt(); switch(operationNum){ case 1:{ System.out.println("请输入你要购买的商品编号:"); int buyID=scanner.nextInt(); System.out.println("您的商品将要出货!!!"); String sql="update autoGoods set number=(number-1) where ID='"+buyID+"'"; String sql1="select price from autoGoods where ID='"+buyID+"'"; try { conn=gDao.getConnection(); stat=conn.createStatement(); rs=stat.executeQuery(sql1); while(rs.next()){ sumMonNum=sumMonNum-rs.getDouble("price"); if(sumMonNum<0){ System.out.println("余额不足,请重新选择!!"); break ; }else{ System.out.println("您的零钱为"+sumMonNum); if(stat.executeUpdate(sql)>0){ System.out.println("购买成功!!"); } } } }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ gDao.closeStatement(stat); gDao.closeConnection(conn); } break ; } case 2:{ System.out.println("将要为您找零,谢谢您的使用,期待您的下次光临~"); System.exit(0); } } } } public static void main(String[] args) { Main main=new Main(); while(true){ main.showList(); main.buyGoods(); main.askOpration(); } } }