超市管理系统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 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;
}
}