JAVA小项目

超市管理系统package ui;import service.WareService;public class WareUi { public static void main(String[] args) throws Exception { new WareService().Load(); }}//主函数package util;imp...
摘要由CSDN通过智能技术生成

超市管理系统

package ui;

import service.WareService;

public class WareUi {
    public static void main(String[] args) throws Exception {
        new WareService().Load();
    }
}
//主函数
package util;

import java.sql.*;


public class DBUtil {
    public String url = "jdbc:mysql://localhost:3306/db5";
    public String username = "root";
    public String password = "123456";

    /**
     * 1. 实现数据库连接的方法
     */
    public Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(url, username, password);
            System.out.println("连接数据库成功.");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     * 2. 释放数据库连接
     */
    public void closeConn(ResultSet rs, PreparedStatement pstm, Connection conn) throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (pstm != null) {
            pstm.close();
        }
        if (conn != null) {
            conn.close();
        }
    }

}
package service;

import daoimpl.WareDao;
import daoimpl.UserDao;
import model.Ware;
import model.User;
import util.WareUtil;
import util.UserUtil;

import java.util.Scanner;

public class WareService {

    public Scanner input = new Scanner(System.in);

    public void Load() throws Exception {
        System.out.println("请登录账户");
        System.out.print("请输入用户名:");
        String uname = input.next();
        System.out.print("请输入密码:");
        String upwd = input.next();

        User user = new User(uname, upwd);
        boolean b = new UserDao().getHaveUser(user);
        if (b) {
            System.out.println("登录成功");
            System.out.println("欢迎使用超市管理系统");
            new WareService().Menu();
        } else {
            System.out.println("没有此用户!");
            System.exit(0);
        }
    }

    public void Menu() throws Exception {

        boolean flag = true;
        while (flag) {
            int id = 0;

            System.out.println("1.用户管理\n2.超市管理\n3.退出系统");

            System.out.print("请选择:");
            int choice = input.nextInt();
            if (choice == 2) {
                while(flag){
                    System.out.println("1.增加商品\n2.删除商品\n3.修改商品\n4.单查商品\n5.全部商品\n6.返回上级");
                    System.out.print("请选择:");

                    choice = input.nextInt();

                    switch (choice) {
                        case 1:
                            Ware newWare = new WareUtil().getWare("增加");
                            new WareDao().insertWare(newWare);
                            break;
                        case 2:
                            id = new WareUtil().getId("删除");
                            new WareDao().deleteWare(id);
                            break;
                        case 3:
                            id = new WareUtil().getId("修改");
                            Ware newWare2 = new WareUtil().getWare("修改后");
                            new WareDao().updateWare(id, newWare2);
                            break;
                        case 4:
                            id = new WareUtil().getId("查询");
                            new WareDao().selectByID(id);
                            break;
                        case 5:
                            new WareDao().selectAllWare();
                            break;
                        case 6:
                            new WareService().Menu();
                            break;
                    }
                }


            } else if (choice == 1) {  // 用户管理
                while(flag){
                    System.out.println("1.增加用户\n2.删除用户\n3.修改用户\n4.查寻用户\n5.返回上级");
                    System.out.print("请选择:");

                    choice = input.nextInt();

                    switch (choice) {
                        case 1:
                            User newUser = new UserUtil().getUser("增加");
                            new UserDao().insertUser(newUser);
                            break;
                        case 2:
                            id = new UserUtil().getId("删除");
                            new UserDao().deleteUser(id);
                            break;
                        case 3:
                            id = new UserUtil().getId("修改");
                            User newUser2 = new UserUtil().getUser("修改后");
                            new UserDao().updateUser(id, newUser2);
                            break;
                        case 4:
                            id = new UserUtil().getId("查询");
                            new UserDao().selectByID(id);
                            break;
                        case 5:
                            new WareService().Menu();
                            break;
                    }
                }

            } else if (choice == 3) {  // 退出系统
                System.exit(0);
            }
        }
    }
}

package dao;

import model.User;

public interface IUser {
    void insertUser(User newUser) throws Exception;  // 添加用户的方法

    void deleteUser(int id) throws Exception;  // 删除用户的方法

    void updateUser(int id,User modUser) throws Exception;  // 更新用户的方法

    void selectByID(int id) throws Exception;   // 根据id查询用户
    
}
package dao;

import model.Ware;

public interface IWare {
    void insertWare(Ware newWare) throws Exception;

    void deleteWare(int id) throws Exception;

    void updateWare(int id, Ware modWare) throws Exception;

    void selectByID(int id) throws Exception;

    void selectAllWare() throws  Exception;
}
package model;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}

package model;

public class Ware {
    // 1. 属性字段
    private int id;
    private String name;
    private String type;

    // 2. 生成getter/setter方法
    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 getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    // 3. 生成构造方法
    public Ware() {
    }

    public Ware(int id, String name, String type) {
        this.id = id;
        this.name = name;
        this.type = type;
    }
}
package daoimpl;

import dao.IUser;
import model.User;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao implements IUser {
    DBUtil db = new DBUtil();
    private Connection conn;

    @Override
    public void insertUser(User newUser) throws Exception {
        // 将用户信息添加到后台数据库表中
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_insert = "insert into user(username,password) values(?,?)";
        pstm = conn.prepareStatement(sql_insert);

        pstm.setString(1, newUser.getUsername());
        pstm.setString(2, newUser.getPassword());

        int row = pstm.executeUpdate();
        System.out.println("新增用户成功" + row + "行受到影响");
        db.closeConn(null, pstm, conn);
    }

    @Override
    public void deleteUser(int id) throws Exception {
        // 从数据表中删除用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_delete = "delete from user where id=?";
        pstm = conn.prepareStatement(sql_delete);

        pstm.setInt(1,id);

        int row = pstm.executeUpdate();
        System.out.println("删除用户成功"+row+"行受到影响");
        db.closeConn(null, pstm, conn);
    }

    @Override
    public void updateUser(int id, User modUser) throws Exception {
        // 从数据库中修改用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update = "update user set username=?,password=? where id=?";
        pstm = conn.prepareStatement(sql_update);

        pstm.setString(1,modUser.getUsername());
        pstm.setString(2,modUser.getPassword());
        pstm.setInt(3,id);

        int row = pstm.executeUpdate();
        System.out.println("修改用户成功"+row+"行受到影响");
        db.closeConn(null, pstm, conn);
    }

    @Override
    public void selectByID(int id) throws Exception {
        // 根据id单查用户信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectById = "select * from user where id=?";
        pstm = conn.prepareStatement(sql_selectById);

        pstm.setInt(1,id);

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("用户编号\t\t用户名称\t\t用户密码\t\t");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("username")+"\t\t"+rs.getString("password")+"\t\t");
        }
        db.closeConn(rs, pstm, conn);
    }

    // 判断是否存在该用户,存在返回值为true,不存在返回值为false
    public boolean getHaveUser( User user) throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_selectHave = "select * from user where username=? and password=?";
        pstm = conn.prepareStatement(sql_selectHave);

        pstm.setString(1,user.getUsername());
        pstm.setString(2,user.getPassword());

        ResultSet rs = pstm.executeQuery();
        while(rs.next()){
            return true;
        }
        return false;
    }
}
package daoimpl;

import dao.IWare;
import model.Ware;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class WareDao implements IWare {

    DBUtil db = new DBUtil();

    @Override
    public void insertWare(Ware newWare) throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_insert = "insert into goods(name,type) values(?,?)";

        pstm = conn.prepareStatement(sql_insert);
        pstm.setString(1, newWare.getName());
        pstm.setString(2, newWare.getType());

        int row = pstm.executeUpdate();
        System.out.println("添加成功," + row + "行受到影响.");

        db.closeConn(null, pstm, conn);
    }

    @Override
    public void deleteWare(int id) throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_delete = "delete from goods where id=?";

        pstm = conn.prepareStatement(sql_delete);
        pstm.setInt(1, id);

        int row = pstm.executeUpdate();
        System.out.println("删除成功," + row + "行受到影响");

        db.closeConn(null, pstm, conn);
    }

    @Override
    public void updateWare(int id, Ware modWare) throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        String sql_update = "update goods set name=? where id=?";

        pstm = conn.prepareStatement(sql_update);
        pstm.setString(1,modWare.getName());
        pstm.setInt(2,id);

        int row = pstm.executeUpdate();
        System.out.println("更新成功,"+row+"行受到影响");

        db.closeConn(null, pstm, conn);
    }

    @Override
    public void selectByID(int id) throws Exception {
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectById = "select * from goods where id=?";

        pstm = conn.prepareStatement(sql_selectById);
        pstm.setInt(1, id);

        rs = pstm.executeQuery();
        System.out.println("查询成功,信息如下:");
        System.out.println("商品编号\t\t" + "商品名称\t\t\t" + "商品类型");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("type"));
        }
        db.closeConn(rs, pstm, conn);
    }

    @Override
    public void selectAllWare() throws Exception {
        // 查询所有的音乐信息
        Connection conn = db.getConn();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String sql_selectAll = "select * from goods";

        pstm = conn.prepareStatement(sql_selectAll);

        rs = pstm.executeQuery();
        System.out.println("所有音乐信息如下:");
        System.out.println("商品编号\t\t" + "商品名称\t\t\t" + "商品类型");
        while(rs.next()){
            System.out.println(rs.getInt("id")+"\t\t\t"+rs.getString("name")+"\t\t\t"+rs.getString("type"));
        }
        db.closeConn(rs, pstm, conn);
    }

}

package util;

import model.User;

import java.util.Scanner;

public class UserUtil {
    public Scanner input = new Scanner(System.in);

    /**
     * 1. 提供User类参数
     */
    public User getUser(String str){
        User newUser = new User();
        System.out.println("请输入"+str+"用户名称:");
        newUser.setUsername(input.next());
        System.out.println("请输入"+str+"用户密码:");
        newUser.setPassword(input.next());

        return newUser;
    }

    /**
     * 2. 提供id参数
     */
    public int getId(String str){
        System.out.println("请输入需要"+str+"的用户id:");
        int id  = input.nextInt();

        return id;
    }
}

package util;

import model.Ware;

import java.util.Scanner;

public class WareUtil {
    Scanner input = new Scanner(System.in);

    public Ware getWare(String str) {
        Ware newWare = new Ware();
        System.out.println("请输入"+str+"商品的名称:");
        newWare.setName(input.next());
        System.out.println("请输入"+str+"商品的类型:");
        newWare.setType(input.next());

        return newWare;
    }

    public int getId(String str) {
        System.out.println("请输入需要" + str + "的商品编号:");
        int id = input.nextInt();
        return id;
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值