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开发自动售货机的更多相关文章

  1. 开发实践丨用小熊派STM32开发板模拟自动售货机

    摘要:本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代码,通过云端开发和设备终端开发,实现终端数据在的华为云平台显示. 本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代 ...

  2. 玩转华为物联网IoTDA服务系列三-自动售货机销售分析场景示例

    场景简介 通过收集自动售货机系统的销售数据,EI数据分析售货销量状况. 该场景主要描述的是设备可以通过MQTT协议与物联网平台进行交互,应用侧可以到物联网平台订阅设备侧变化的通知,用户可以在控制台或通 ...

  3. 09自动售货机综设实验(含按键消抖,led和状态机)

    一设计功能 1.上次状态机的练习 2这次自动售货机综设 (一)对比两次的售货机 上次售货机的关键是画出状态转移图.明确输入分几种,输出是啥,有哪些状态.如下图所示 (二)系统或综合设计的经验: 既然这 ...

  4. 使用NewLife网络库构建可靠的自动售货机Socket服务端(一)

    最近有个基于tcp socket 协议和设备交互需求,想到了新生命团队的各种组件,所以决定用NewLife网络库作为服务端来完成一系列的信息交互. 第一,首先说一下我们需要实现的功能需求吧 1,首先客 ...

  5. YTU 2598: 编程题B-小平智斗自动售货机

    2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec  内存限制: 128 MB 提交: 268  解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...

  6. C#骏鹏自动售货机接口

    MachineJP类: 第1部分:串口初始化,串口数据读写 using System; using System.Collections.Generic; using System.IO.Ports; ...

  7. FSM自动售货机 verilog 实现及 code 细节讲解

    1.题目: 饮料1.5 元, 可投入硬币1 元 0.5 元,输出饮料 零钱 2. 画出状态机. 3.仿真结果:coin=1 --> 0.5 元 coin=2-->1元 4.关键代码分析: ...

  8. 使用Java注解开发自动生成SQL

    使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...

  9. 开发环境,eclipse编辑器java代码自动提示

    Eclipse+ADT+Android SDK 搭建安卓开发环境 eclipse编辑器java代码自动提示 window-->Preferences-->JAva-->Content ...

随机推荐

  1. rabbitmq权限细分二

    禁止用户远程登录 主要有以下几步 1.进入容器 docker exec -it ID /bin/bash 2.打开配置文件 vi /etc/rabbitmq/rabbitmq.conf 3.加入配置, ...

  2. css background之设置图片为背景技巧

    原文 Background是什么意思,翻译过来有背景意思.同样在css里面作为css属性一成员同样是有背景意思,并且是设置背景图片.背景颜色.背景图片截取等样式. 首先先来看看background有那 ...

  3. ES6中的模板字符串使用方法

    传统的 JavaScript 语言,输出模板通常是这样写的. $('#result').append( 'There are <b>' + basket.count + '</b&g ...

  4. 1753 -- Flip Game

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48663   Accepted: 20724 Descr ...

  5. Spring事务注解分析

    1.使用spring事务注解 2.手写事务注解 1).sql执行器 2).事务注解定义 3).AOP实现事务具体实现(同一个线程中使用同一个连接) 4).应用使用注解前 5).应用使用注解后

  6. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-1.数据信息安全--微信授权一键登录功能介绍

    笔记 1.数据信息安全--微信授权一键登录功能介绍 简介:讲解登录方式优缺点和微信授权一键登录功能介绍         1.手机号或者邮箱注册             优点:              ...

  8. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-1.整合Mybatis访问数据库和阿里巴巴数据源

    笔记 1.整合Mybatis访问数据库和阿里巴巴数据源     简介:整合mysql 加入mybatis依赖,和加入alibaba druid数据源 1.加入依赖(可以用 http://start.s ...

  9. Vim的强大配置文件

    我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2.自动插入文件头 ,新建C.C++源文件时自动插入表 ...

  10. 由DBCursor的“can't switch cursor access methods”异常引发的思考

    先谈谈我是怎么用的: DBCollection dbcollection = XXXXXXXXXX(); //连接mongo DBCursor dbCursor = mergeVideoDB.find ...