Java開發自動售貨機

來源:互聯網
上載者:User

標籤:database   pre   lis   drive   roo   import   void   ber   sql   

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();            }    }}

 

Java開發自動售貨機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.