Java开发自动售货机
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开发自动售货机的更多相关文章
- 开发实践丨用小熊派STM32开发板模拟自动售货机
摘要:本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代码,通过云端开发和设备终端开发,实现终端数据在的华为云平台显示. 本文内容是讲述用小熊派开发板模拟自动售货机,基于论坛提供的工程代 ...
- 玩转华为物联网IoTDA服务系列三-自动售货机销售分析场景示例
场景简介 通过收集自动售货机系统的销售数据,EI数据分析售货销量状况. 该场景主要描述的是设备可以通过MQTT协议与物联网平台进行交互,应用侧可以到物联网平台订阅设备侧变化的通知,用户可以在控制台或通 ...
- 09自动售货机综设实验(含按键消抖,led和状态机)
一设计功能 1.上次状态机的练习 2这次自动售货机综设 (一)对比两次的售货机 上次售货机的关键是画出状态转移图.明确输入分几种,输出是啥,有哪些状态.如下图所示 (二)系统或综合设计的经验: 既然这 ...
- 使用NewLife网络库构建可靠的自动售货机Socket服务端(一)
最近有个基于tcp socket 协议和设备交互需求,想到了新生命团队的各种组件,所以决定用NewLife网络库作为服务端来完成一系列的信息交互. 第一,首先说一下我们需要实现的功能需求吧 1,首先客 ...
- YTU 2598: 编程题B-小平智斗自动售货机
2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec 内存限制: 128 MB 提交: 268 解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...
- C#骏鹏自动售货机接口
MachineJP类: 第1部分:串口初始化,串口数据读写 using System; using System.Collections.Generic; using System.IO.Ports; ...
- FSM自动售货机 verilog 实现及 code 细节讲解
1.题目: 饮料1.5 元, 可投入硬币1 元 0.5 元,输出饮料 零钱 2. 画出状态机. 3.仿真结果:coin=1 --> 0.5 元 coin=2-->1元 4.关键代码分析: ...
- 使用Java注解开发自动生成SQL
使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...
- 开发环境,eclipse编辑器java代码自动提示
Eclipse+ADT+Android SDK 搭建安卓开发环境 eclipse编辑器java代码自动提示 window-->Preferences-->JAva-->Content ...
随机推荐
- CISCN final 几道web题总结
因为都有源码,所以这里直接从源码开始分析: 1.Easy web 这道题本来的意思应该是通过注入来load_file读取config.php来泄露cookie的加密密钥,从而伪造身份进行登陆再上传sh ...
- php多线程的概念
来源:http://www.cnblogs.com/zhenbianshu/p/7978835.html 多线程 线程 首先说下线程: 线程(thread) 是操作系统能够进行运算调度的最小单位.它被 ...
- 空指针/0/NULL
空指针/0/NULL 空指针是一个被赋值为0的指针,在没有被具体初始化之前,其值为0. NULL 是一个标准规定的宏定义,用来表示空指针常量. #define NULL 0 或者 #define ...
- 修改vscode终端样式
在设置中查找workbench,然后编辑setting.json: "terminal.integrated.cursorBlinking": true, "termin ...
- Oracle 中的进制转换
Oracle 中的进制转换 */--> Oracle 中的进制转换 Table of Contents 1. 进制名 2. 10进制与16进制互相转换 2.1. 10进制转换为16进制 2.2. ...
- Mysql Errors
Mysql Errors Table of Contents 1. ERROR 1044 1.1. 42000 2. ERROR 1045 2.1. 28000 2.1.1. 无登录权限 2.1.2. ...
- 有关react-native的最常用的库(文件、样式、UI组件)
一.对文件的处理 1.react-native-fs 2.react-native-file-selector 3.MaterialFilePicker 二.React-Native 样式指南 1.r ...
- python之scrapy模块scrapy-redis使用
1.redis的使用,自己可以多学习下,个人也是在学习 https://www.cnblogs.com/ywjfx/p/10262662.html官网可以自己搜索下. 2.下载安装scrapy-red ...
- Kettle源码学习(一)——把Kettle项目跑起来
kettle(pentaho data integration),是一款开源的C/S版的ETL工具,最近打算学习一下kettle源码,并自己写一个mini kettle,并改造成基于事件触发的流处理模 ...
- List自定义对象的排序,根据对象的某一列进行排序
在工作中,经常需要对List对象集合进行排序操作,下面总结下搞个通用排序对象,原理是使用JAVA的 Comparator 接口实现排序 不多说直接上“干货” 1.存在实体类: @Data @ ...