022-《小型淘宝管理系统》,代码纯享版,(020、021姊妹篇)

com.yann.classes.Commodity

package com.yann.classes;

public abstract class Commodity {
    private int id;
    private String name;
    private double price;
    private int stock;

    public Commodity(int id, String name, double price, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    public Commodity(String name, double price, int stock) {
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

com.yann.classes.Meat

package com.yann.classes;

public class Meat extends Commodity {
    private String name;
    private double price;
    private int stock;

    public Meat(String name, double price, int stock) {
        super(name, price, stock);
    }


    @Override
    public String toString() {
        return "Milk{" +
                "name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", stock='" + stock + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }
}


com.yann.classes.Milk

package com.yann.classes;

public class Milk extends Commodity {
    private int id;
    private String name;
    private double price;
    private int stock;

    public Milk(String name, double price, int stock) {
        super(name, price, stock);
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    public Milk(int id, String name, double price, int stock) {
        super(id, name, price, stock);
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Milk{" +
                "name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", stock='" + stock + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }
}


com.yann.classes.Tea

package com.yann.classes;

public class Tea extends Commodity {
    private String name;
    private double price;
    private int stock;

    public Tea(String name, double price, int stock) {
        super(name, price, stock);
    }


    @Override
    public String toString() {
        return "Milk{" +
                "name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", stock='" + stock + '\'' +
                '}';
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public double getPrice() {
        return price;
    }

    @Override
    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }
}

com.yann.interfaces.IDelete

package com.yann.interfaces;

public interface IDelete {
    void delete(int id);
}


com.yann.dao.CommodityImpl

package com.yann.dao;

import com.yann.classes.Commodity;
import com.yann.classes.Milk;
import com.yann.interfaces.IAdd;
import com.yann.interfaces.IDelete;
import com.yann.interfaces.IResult;
import com.yann.interfaces.IUpdate;
import com.yann.sql.JDBCUtil;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class CommodityImpl implements IAdd, IDelete, IUpdate, IResult {
    Connection connection = JDBCUtil.getConnection();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    @Override
    public void add(Commodity commodity) {
        String sql = "INSERT INTO commodity VALUES(null,?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, commodity.getName());
            preparedStatement.setDouble(2, commodity.getPrice());
            preparedStatement.setInt(3, commodity.getStock());
            preparedStatement.executeUpdate();
            System.out.println("添加成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(int id) {
        String sql = "DELETE FROM commodity WHERE id = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
            System.out.println("删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Commodity> result() {
        ArrayList<Commodity> list = new ArrayList<>();
        String sql = "SELECT * FROM commodity";
        try {
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Commodity commodity = new Milk(
                        resultSet.getString("name"),
                        resultSet.getDouble("price"),
                        resultSet.getInt("stock")
                );
                list.add(commodity);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void update(Commodity commodity) {
        String sql = "UPDATE commodity SET price = ? WHERE id = ?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setDouble(1, commodity.getPrice());
            preparedStatement.setInt(2, commodity.getId());
            preparedStatement.executeUpdate();
            System.out.println("更新成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


com.yann.service.CommodityImplService

package com.yann.service;

import com.yann.classes.Commodity;
import com.yann.dao.CommodityImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component("commodityImplService")
public class CommodityImplService {

    @Resource
    CommodityImpl commodityImpl = new CommodityImpl();

    public void add(Commodity commodity) {
        commodityImpl.add(commodity);
    }

    public void delete(int id) {
        commodityImpl.delete(id);
    }

    public void update(Commodity commodity) {
        commodityImpl.update(commodity);
    }

    public void result() {
        List<Commodity> result = commodityImpl.result();
        for (int i = 0; i < result.size(); i++) {
            System.out.println(result.get(i));
        }
    }
}


com.yann.interfaces.IUpdate

package com.yann.interfaces;

import com.yann.classes.Commodity;

public interface IUpdate {
    void update(Commodity commodity);
}


com.yann.interfaces.IResult

package com.yann.interfaces;

import com.yann.classes.Commodity;

import java.util.List;

public interface IResult {
    List<Commodity> result();
}


com.yann.interfaces.IAdd

package com.yann.interfaces;

import com.yann.classes.Commodity;

public interface IAdd {
    void add(Commodity commodity);
}


com.yann.sql.JDBCUtil

package com.yann.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCUtil {
    static Connection connection = null;

    public static Connection getConnection() {
        String jdbc = "jdbc:mysql://localhost:3306/taobao?useUnicode=true" +
                "&characterEncoding=utf8";
        String name = "root";
        String password = "root";
        try {
            connection = DriverManager.getConnection(jdbc, name, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}


com.yann.Test

package com.yann;

import com.yann.classes.Commodity;
import com.yann.classes.Meat;
import com.yann.classes.Milk;
import com.yann.service.CommodityImplService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {

        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        CommodityImplService bean = (CommodityImplService) context.getBean("commodityImplService");
        Commodity meat = new Meat("里脊", 23.5, 90);
        bean.add(meat);
        Commodity milk2 = new Milk("特仑苏", 7.5, 90);
        bean.add(milk2);

        for (int i = 3; i <= 4; i++) {
            bean.delete(i);
        }
        Commodity milk = new Milk(5, "酸奶", 24, 90);
        bean.update(milk);
        bean.result();

    }
}


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.yann"></context:component-scan>
</beans>

猜你喜欢

转载自blog.csdn.net/qq_31698195/article/details/89736140
今日推荐