Java Development Vending Machines

Source: Internet
Author: User

1: Write a class first, including the basic properties of the commodity

 PackageCom.xt.java.base25; Public classGoods {Private intID; PrivateString name; Private intNumber ; Private DoublePrice ; /**     * @returnThe ID*/     Public intGetID () {returnID; }    /**     * @paramID The ID to set*/     Public voidSetID (intID) {ID=ID; }    /**     * @returnThe name*/     PublicString GetName () {returnname; }    /**     * @paramname the name to set*/     Public voidsetName (String name) { This. Name =name; }    /**     * @return the number*/     Public intGetNumber () {returnNumber ; }    /**     * @paramNumber the number to set*/     Public voidSetnumber (intNumber ) {         This. Number =Number ; }    /**     * @return The price*/     Public DoubleGetPrice () {returnPrice ; }    /**     * @paramPrice the price to set*/     Public voidSetprice (DoublePrice ) {         This. Price =Price ; }            }

2: Connect the database to Java and write it as a separate class, which is more convenient to use .

 PackageCom.xt.java.base25;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classGooddao {PrivateConnection Conn; PrivateStatement Stat; PrivateString url= "Jdbc:mysql://localhost:3306/lyxdatabases"; PrivateString dbdriver= "Com.mysql.jdbc.Driver"; PrivateString username= "Root"; PrivateString password= "1234"; //private construction method, only in this class can be instantiated once, in order to protect the privacy of users use this method, remember!!!     PrivateGooddao () {}Private StaticGooddao gda=NULL; /*** Write a method that obtains an instance from a method and can only be instantiated once. */         Public StaticGooddao Getgooddao () {if(gda==NULL) {Gda=NewGooddao (); }        returnGda; }         PublicConnection getconnection ()throwsexception{Try{class.forname (dbdriver); returndrivermanager.getconnection (Url,username,password); } Catch(ClassNotFoundException e) {Throw NewClassNotFoundException ("The database cannot find the driver!! "); } Catch(SQLException e) {Throw NewSQLException ("Database connection Exception!! "); }    }        //Close Connection     Public voidCloseConnection (Connection conn) {Try{            if(conn!=NULL) {conn.close (); }                    }Catch(Exception e) {System.out.println (e); }    }        //Close Statement     Public voidclosestatement (Statement stat) {Try{            if(stat!=NULL) {stat.close (); }                    }Catch(Exception e) {System.out.println (e); }    }    //Close ResultSet     Public voidCloseresultset (ResultSet rs) {Try{            if(rs!=NULL) {rs.close (); }                    }Catch(Exception e) {System.out.println (e); }    }        }

3. Main method, direct realization function

 PackageCom.xt.java.base25;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.Statement;ImportJava.util.Scanner; Public classMain {Gooddao Gdao=Gooddao.getgooddao ();    Connection Conn;    Statement Stat;    ResultSet rs; Scanner Scanner=NewScanner (system.in); Static DoubleSummonnum; //Displays the list of goods, and the inventory requirements contain the remaining quantity for each item.      Public voidshowlist () {String SQL= "Select *from autogoods"; Try{conn=gdao.getconnection (); Stat=conn.createstatement (); RS=stat.executequery (SQL);  while(Rs.next ()) {System.out.println ("Item Number:" +rs.getstring ("id"))); System.out.print ("Product Name:" +rs.getstring ("name"))); System.out.print ("Quantity Remaining:" +rs.getint ("number"))); System.out.print ("Unit Price:" +rs.getdouble ("prices") + "\ n"); }        } Catch(Exception e) {e.printstacktrace (); }finally{Gdao.closeresultset (RS);            Gdao.closestatement (STAT);        Gdao.closeconnection (conn); }            }        //Select a product number to buy something     Public voidBuygoods () {System.out.println ("Please enter the product number you want to purchase:"); intBuyid=Scanner.nextint (); System.out.println ("Please coin:"); Summonnum=scanner.nextdouble (); System.out.println ("Your product will be shipped!!!" "); 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 ("Your change is" +summonnum); }            if(Stat.executeupdate (SQL) >0) {System.out.println ("Purchase success!! "); }        } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{Gdao.closeresultset (RS);            Gdao.closestatement (STAT);        Gdao.closeconnection (conn); }    }        //ask for further action         Public voidaskopration () { while(true) {System.out.println ("--------Please select what you need to do next-------"); System.out.println ("Continue to purchase-------1"); System.out.println ("End of purchase, change-------2"); intoperationnum=Scanner.nextint (); Switch(operationnum) { Case1: {System.out.println ("Please enter the product number you want to purchase:"); intBuyid=Scanner.nextint (); System.out.println ("Your product will be shipped!!!" "); 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 ("Insufficient balance, please re-select!! ");  Break ; }Else{System.out.println ("Your change is" +summonnum); if(Stat.executeupdate (SQL) >0) {System.out.println ("Purchase success!! "); }                            }                     }                }Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{gdao.closestatement (stat);                Gdao.closeconnection (conn); }                 Break ; }                         Case2: {System.out.println ("will give you change, thank you for your use, look forward to your next visit ~"); System.exit (0); }            }        }    }         Public Static voidMain (string[] args) {main main=NewMain ();  while(true) {main.showlist ();        Main.buygoods ();            Main.askopration (); }    }}

Java Development Vending Machine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.