//Example类文件Example.java
我们提供的服务有:成都网站制作、成都网站设计、外贸营销网站建设、微信公众号开发、网站优化、网站认证、雨城ssl等。为上1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的雨城网站制作公司
package cn.zhouhan;
import java.util.Scanner;
public class Example {
static Drink cola, fanta, blacktea, greentea;
static int money;
public static void main(String[] args) {
// TODO Auto-generated method stub
cola = new Drink("可乐", 50, 2.0, 1);
fanta = new Drink("芬达", 40, 3.0, 2);
blacktea = new Drink("红茶", 30, 4.0, 3);
greentea = new Drink("绿茶", 20, 5.0, 4);
money = 500;
for(;;)
{
System.out.println("-------------自动售货机-------------");
cola.UserPrintDrinkInfo();
fanta.UserPrintDrinkInfo();
blacktea.UserPrintDrinkInfo();
greentea.UserPrintDrinkInfo();
System.out.println("----------------------------------");
System.out.println("1.购买饮料");
System.out.println("2.管理员查询");
System.out.println("3.返回");
System.out.println("请输入要执行的操作序号:");
Scanner scannerIndex = new Scanner(System.in);
int indexScanner = scannerIndex.nextInt();
switch(indexScanner)
{
case 1:
System.out.println("请选择购买的饮料(1.可乐,2.芬达,3红茶,4.绿茶):");
Scanner numberScanner = new Scanner(System.in);
int buyIndex = numberScanner.nextInt();
Drink drink = getDrink(buyIndex);
System.out.println("请输入购买的数量:");
Scanner countScanner = new Scanner(System.in);
int buyCount = countScanner.nextInt();
if (buyCount 0)
{
System.out.println("您选择了" + buyCount + "瓶" + drink.getName() + "." );
System.out.println("总价格:" + drink.getPrice() * buyCount + "元,请投入1元或5元纸币。");
System.out.println("确定购买?(1.确定,2.退款):");
Scanner sureScanner = new Scanner(System.in);
int sure = sureScanner.nextInt();
if (sure == 1)
{
drink.sellDrink(buyCount);
money += drink.getPrice() * buyCount;
System.out.println("您购买了" + buyCount + "瓶" + drink.getName() + ",交易成功." );
}
if (sure ==2 )
{
System.out.println("退款成功!");
}
}
else
System.out.println("输入的数量有误!");
break;
case 2:
System.out.println("请输入管理员密码:");
Scanner pwScanner = new Scanner(System.in);
int password = pwScanner.nextInt();
if (password == 123)
{
System.out.println("密码正确,目前的余额为:" + money + "元。");
}
else
System.out.println("密码错误。");
break;
case 3:
break;
default:
System.out.println("请输入正确的数字。");
break;
}
}
}
public static Drink getDrink(int drinkIndex)
{
Drink drink = null;
switch(drinkIndex)
{
case 1:
drink = cola;
System.out.println("您选择了可乐。");
break;
case 2:
drink = fanta;
System.out.println("您选择了芬达。");
break;
case 3:
drink = blacktea;
System.out.println("您选择了红茶。");
break;
case 4:
drink = greentea;
System.out.println("您选择了绿茶。");
break;
default:
//drink = null;
System.out.println("输入错误,请输入1~4的数字!");
}
return drink;
}
}
//创建的Drink类文件Drink.java
package cn.zhouhan;
public class Drink {
private String name;
private int number;
private double price;
private double drinkMoney;
private int index;
public Drink(String name, int number, double price, int index)
{
this.name = name;
this.number = number;
this.price = price;
this.drinkMoney = number * price;
this.index = index;
}
public String getName()
{
return this.name;
}
public int getNumber()
{
return this.number;
}
public double getPrice()
{
return this.price;
}
public void sellDrink(int count)
{
this.number -= count;
this.drinkMoney += this.price * count;
}
public void UserPrintDrinkInfo()
{
System.out.println("品名:"+ this.name +",单价:" + this.price + "元,剩余数量:" + this.number + "瓶。");
}
}
商品最好存储在数据库中, 商业应用哪有存在文件里的, 也就是学校练习使用文件存储。
这是一段增删改查的代码, 将里面的MSC类修改成商品类就行了.(既然学过java, 你应该会修改吧)
数据库使用mysql(小型) 或sql server(大型) 均可:建议使用mysql即可
代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
}
}
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "123"; //密码
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
try {
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
}
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("删除表格MSC信息中 ID = "+MscID+"的记录");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("记录删除失败!!!");
}
System.out.println("记录删除成功!!!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败!!!");
}
System.out.println("插入成功!!!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失败!!!");
}
System.out.println("修改成功!!!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "联想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中兴" ," 中兴", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
package entity;
public class Market {
private int id;//id
private int num;//数量
private String goods;//商品
private double price;//价格
public Market(int id, int num, String goods, double price) {
super();
this.id = id;
this.num = num;
this.goods = goods;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getGoods() {
return goods;
}
public void setGoods(String goods) {
this.goods = goods;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double calc( ){
double sum=price*num;
System.out.println("您消费共计:"+sum+"¥");
return sum;
}
}
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import entity.Market;
public class Test {
private static MapInteger,Market goods=new HashMapInteger, Market();
public static void main(String[] args) {
System.out.println("-------超市计价系统-------");
String goods1="可口可乐";
String goods2="爆米花";
String goods3="益达";
printTable("编号","商品","价格");
printTable("1",goods1,"3.0¥");
printTable("2",goods2,"5.0¥");
printTable("3",goods3,"10.0¥");
goods.put(1, new Market(1, 1, goods1, 3.0));
goods.put(2, new Market(2, 1, goods2, 5.0));
goods.put(3, new Market(3, 1, goods3, 10.0));
Scanner input = new Scanner(System.in);
System.out.println("请输入商品的编号:");
int num = input.nextInt();
System.out.println("请输入商品的数量");
int amount = input.nextInt();
Market market = goods.get(num);
market.setNum(amount);
market.calc();
}
private static void printTable(String row1,String row2,String row3 ) {
System.out.print(row1);
int times=12;
if (row2!="商品") {
times=5;
}
for (int i = 0; i times; i++) {
System.out.print(" ");
}
System.out.print(row2);
for (int i = 0; i 10; i++) {
System.out.print(" ");
}
System.out.print(row3);
System.out.println("\n");
}
}
//测试结果:
-------超市计价系统-------
编号 商品 价格
1 可口可乐 3.0¥
2 爆米花 5.0¥
3 益达 10.0¥
请输入商品的编号:
3
请输入商品的数量
5
您消费共计:50.0¥
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 自动生产编号
* @author
*chengeng
*/
public class AutoNum {
/**
*
* 在用此类时,必须先明确自己模块编号的开头
* @param start编号开头,
* @param num从数据库查询出的最大编号
* @return
*/
public String getNum(String start,String num){
String n=num.substring(0,3);
if(!n.equals(start)){
System.out.println("编号的开头与从数据库取得的编号开头不相同");
return "";
}
String strDate="";//流水号时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
//格式化当前时间,并按字符'-'分割
String[] sd=sdf.format(date).split("-");
//截取编号中的日期
String time=num.substring(3,11).toString();
for(int i=0;isd.length;i++){
strDate+=sd[i];
}
//如果当天或者表中没有记录,返回当天第一条记录
if(!time.equals(strDate)||num.equals("")){
return start+strDate+"0001";
}
// 截取编号最后的流水号
String end=num.substring(11, 15);
String s2="";
int lg=Integer.parseInt(end);
//对流水号结尾的四位数字进行判断,以便增加
if(lg0lg9){
s2="000"+(lg+1);
}else if(lg=9lg99){
s2="00"+(lg+1);
}else if(lg=99lg999){
s2="0"+(lg+1);
}else if(lg=999lg9999){
s2=""+(lg+1);
}
//返回自动生成后的流水号
return start+=strDate+s2;
}
}
就是让人输入一些数字,然后让程序输出最小的数是哪个对吗?是的话回复一下,我追答给你