小型电商管理系统商品模块基本信息,要求包含商品管理相关功能,其中商品类包含以下属性:id,商品名,类别名,单价,库存量,计量单位,上架时间,修改时间,销量,商品状态(1:正常销售/0:下架);
建立数据库和表Commodity
商品信息类Commodity
package l电商管理系统;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Commodity {
/**
* id,商品名,类别名,单价,库存量,计量单位,上架时间,修改时间,销量,商品状态
*/
private int id;
private String name;
private String category;
private double price;
private int amount;
private String danwei;
private String utime;
private String xtime;
private int sales;
private int state;
public Commodity( String name, String category, double d, int amount, String danwei,
int sales, int i) {
this.name = name;
this.category = category;
this.price = d;
this.amount = amount;
this.danwei = danwei;
this.utime = dateTime();
this.xtime = dateTime();
this.sales = sales;
this.state = i;
}
public String dateTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date());
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getDanwei() {
return danwei;
}
public void setDanwei(String danwei) {
this.danwei = danwei;
}
public String getUtime() {
return utime;
}
public void setUtime(String utime) {
this.utime = utime;
}
public String getXtime() {
return xtime;
}
public void setXtime(String xtime) {
this.xtime = xtime;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
@Override
public String toString() {
return "'" + name + "'"+"," +"'"+ category +"'"+ "," + price + ","
+ amount + "," +"'"+danwei +"'"+ "," +"'"+ utime +"'"+"," +"'"+ xtime +"'"+"," + sales
+ "," + state;
}
}
商品管理类 CommodityManaget
package l电商管理系统;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
public class CommodityManaget {
public void add(Commodity c){
if (ComSql.insert("insert into commodity(name,category,price,amount,danwei,utime,xtime,sales,state) values ("
+c.toString() +")"
) != 0){
System.out.println("添加成功!");
}
}
public void outSet(ResultSet set){
try {
while(set.next()){
int id=set.getInt("id");
String name=set.getString("name");
String category =set.getString("category");
double price= set.getDouble("price");
int amount = set.getInt("amount");
String danwei = set.getString("danwei");
Date utime = set.getDate("utime");
Date xtime= set.getDate("xtime");
int sales =set.getInt("sales");
int state = set.getInt("state");
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(id+"\t"+name + "\t"+ "\t"+category +"\t"+ price + "\t"
+ amount + "\t" +danwei + "\t" +utime+"\t" +xtime +"\t" + sales
+ "\t" + state);}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//显示所有商品
public void listAll() throws SQLException{
ResultSet set =ComSql.selectOut("select * from commodity");
outSet( set);
}
//根据名字查找商品
public void findName(String name){
String sql="select * from commodity where name="+"'"+name+"'";
ResultSet set =ComSql.selectOut(sql);
outSet( set);
}
//查找下架的商品
public void findState(){
String sql="select * from commodity where state=0";
ResultSet set =ComSql.selectOut(sql);
outSet( set);
}
//根据类别名查找商品
public void findCategory(String category){
String sql="select * from commodity where category="+"'"+category+"'";
ResultSet set =ComSql.selectOut(sql);
outSet( set);
}
public void editState(String name){
try {
String sql1="select * from commodity where name="+"'"+name+"'";
ResultSet set =ComSql.selectOut(sql1);
set.next();
int a = set.getInt("state");
System.out.println(a);
String sql="";
if(a==0){
sql ="update commodity set state= 1,xtime=now() where name ="+"'"+name+"'";
}else{
sql ="update commodity set state= 0,xtime=now() where name="+"'"+name+"'";
}
ComSql.insert(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//6.修改商品的单价和库存量(要求同步更新修改时间)
public void editPN(String name,double d, int n){
String sql="update commodity set xtime=now(), price ="+d+",amount ="+n+" where name= '"+name+"'";
ComSql.insert(sql);
}
//7.删除下架商品
public void deleState(){
String sql ="delete from commodity where state=0";
ComSql.insert(sql);
}
}
表操作封装类 ComSql
package l电商管理系统;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ComSql {
private static final String url="jdbc:mysql://localhost:3306/dianshang";
private static final String user="root";
private static final String password="123456";
static Connection cn=null;
static Statement sta=null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
cn =DriverManager.getConnection(url,user,password);
sta =cn.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int insert(String str){
try {
return sta.executeUpdate(str);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public static ResultSet selectOut(String str){
try {
return sta.executeQuery(str);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
程序入口Test
package l电商管理系统;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Scanner;
public class Test {
CommodityManaget cm = new CommodityManaget();
Scanner sc = new Scanner(System.in);
public void men(){
System.out.println(" 【电商管理系统】\n"
+"1.添加商品信息\n"
+ "2.列表查询所有商品数据\n"
+ "3.查询出所有下架商品\n"
+ "4.按类别查询出所有的商品\n"
+ "5.修改商品状态(要求同步更新修改时间)\n"
+ "6.修改商品的单价和库存量(要求同步更新修改时间)\n"
+ "7.删除下架商品\n"
+ "8.退出\n"
);
start();
}
private void start() {
sc = new Scanner(System.in);
int key = sc.nextInt();
switch (key) {
case 1:
addR();//1.添加商品信息
men();
break;
case 2:
listR();//2.列表查询所有商品数据
men();
break;
case 3:
findStateR();//3.查询出所有下架商品
men();
break;
case 4:
findCategoryR();//4.按类别查询出所有的商品
men();
break;
case 5:
editStateR();//5.修改商品状态(要求同步更新修改时间)
men();
break;
case 6:
editPNR();//6.修改商品的单价和库存量(要求同步更新修改时间)
men();
break;
case 7:
deleteStaR();//7.删除下架商品
men();
break;
case 8:
System.out.println("欢迎下次使用!");
return;
}
}
private void deleteStaR() {
cm.deleState();
System.out.println("删除成功!");
}
private void editPNR() {
System.out.println("请输入需要修改的商品名称");
sc = new Scanner(System.in);
String name=sc.next();
System.out.println("请输入商品单价");
double d =sc.nextDouble();
System.out.println("请输入商品库存");
int n =sc.nextInt();
cm.editPN(name, d, n);
System.out.println("修改成功!");
}
private void editStateR() {
System.out.println("请输入需要修改的商品名称");
sc = new Scanner(System.in);
String cate=sc.next();
cm.editState(cate);
}
private void findCategoryR() {
System.out.println("请输入商品类别");
sc = new Scanner(System.in);
String cate=sc.next();
System.out.println("id\t商品名\t\t类别名\t单价\t库存量\t计量单位\t上架时间\t修改时间\t销量\t商品状态");
cm.findCategory(cate);
}
private void findStateR() {
System.out.println("id\t商品名\t\t类别名\t单价\t库存量\t计量单位\t上架时间\t修改时间\t销量\t商品状态");
cm.findState();
}
private void listR() {
System.out.println("id\t商品名\t\t类别名\t单价\t库存量\t计量单位\t上架时间\t修改时间\t销量\t商品状态");
try {
cm.listAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void addR() {
System.out.println("商品名,类别名,单价,库存量,计量单位,销量,商品状态(0/1)");
sc = new Scanner(System.in);
String s =sc.nextLine();
String [] info =s.split("/");
Commodity c = new Commodity(
info[0],
info[1],
Double.parseDouble(info[2]),
Integer.parseInt(info[3]),
info[4],
Integer.parseInt(info[5]),
Byte.parseByte(info[6]));
cm.add(c);
}
public static void main(String[] args) {
Test t = new Test();
t.men();
}
}
在这里我们的教师学生日报管理系统已经写完了~~!
来看看成果吧 ^ v ^!