重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
/**
成都创新互联公司公司2013年成立,先为当阳等服务建站,当阳等地企业,进行企业商务咨询服务。为当阳企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
* 超市管理系统
* @author Administrator
*
*/
public class ChaoShiSystemManager {
public static void main(String[] args) {
new HuoWuDomImpl().start();
}
}
class HuoWu{
public String bianhao; //商品编号
public String name; //商品名称
public double jiage; //商品价格
}
class HuoWuDomImpl{
private static java.util.ArrayListHuoWu list = new java.util.ArrayListHuoWu();
public void start(){
int num = 0;
System.out.println("--欢迎登录超市管理系统--");
System.out.println("1、添加商品");
System.out.println("2、查询商品");
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入模块名称:");
try {
num = sc.nextInt();
if(num == 1){
setHuoWu();
new HuoWuDomImpl().start();
}else if(num == 2){
getSeleteHuoWu();
new HuoWuDomImpl().start();
}else{
throw new RuntimeException("提示:抱歉,模块正在开发中");
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("提示:对不起,您的输入有误!");
}finally{
sc.close();
}
}
public void setHuoWu(){
HuoWu hw = new HuoWu();
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入商品编号:");
hw.bianhao = sc.next();
System.out.println("请输入商品名称:");
hw.name = sc.next();
System.out.println("请输入商品价格");
try {
hw.jiage = sc.nextDouble();
} catch (Exception e) {
throw new RuntimeException("提示:商品价格输入错误!");
}finally{
sc.close();
}
list.add(hw);
System.out.println("提示:恭喜,添加成功!");
}
public void getSeleteHuoWu(){
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("请输入商品编号...");
String bianhao = sc.next();
java.util.Iterator it = list.iterator();
while(it.hasNext()){
if(bianhao.equals(((HuoWu)it.next()).bianhao)){
System.out.println("商品编号:" + ((HuoWu)it.next()).bianhao);
System.out.println("商品名称:" + ((HuoWu)it.next()).name);
System.out.println("商品价格:" + ((HuoWu)it.next()).jiage);
}
}
}
}
PS:程序可能还有点问题,自己调试一下吧。改天有空我再来更新。
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.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conn {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/blog?user=rootpassword=root";
//获得数据库连接
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
if(conn != null){
System.out.print("成功");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
//关闭Resultset
public static void closeResultSet(ResultSet rs)
{
try
{
if(rs != null)
{
rs.close();
}
}catch (SQLException ex)
{
ex.printStackTrace();
}
}
//关闭Statement
public static void closeStatement(Statement st)
{
try
{
if(st != null)
{
st.close();
}
}catch (SQLException ex)
{
ex.printStackTrace();
}
}
//关闭Connection
public static void closeConnection(Connection conn)
{
try
{
if(conn != null)
{
conn.close();
}
}catch (SQLException ex)
{
ex.printStackTrace();
}
}
//测试数据库连接是否成功
public static void main(String[] args) {
Conn.getConnection();
}
}