一、系统介绍
本系统实现的以下功能
管理员登录系统,对商品信息的增删改查,统计商品的总价及库存。
JDK版本:1.8
存储数据形式:TXT存储
二、系统展示
1.登录页
2.登录成功提示
3.主页面
4.增加商品
5.修改商品
6.统计信息
三、系统实现
UserDao.java
package com.sjsq.dao; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import com.sjsq.model.User; import com.sjsq.util.FileUtils; public class UserDao { private static final String fileName = "user.txt"; /** * 写文件 * * @param list * @return */ public static String addUser(User u) { StringBuffer sb = new StringBuffer(); sb.append(u.getUsername() + " " + u.getPassword() + "\r\n"); return sb.toString(); } public static List<User> StringToList(String s) { List<User> list = new ArrayList<>(); String[] lines = s.split("\r\n"); for (String line : lines) { String[] stu = line.split(" "); list.add(new User(stu[0], stu[1])); } return list; } /** * 登录验证 * * @param username * @param password * @return * @throws IOException */ public static boolean auth(String username, String password) { try { File file = new File(fileName); String content = FileUtils.readTxtFile(file); List<User> list = UserDao.StringToList(content); System.out.println(list); boolean exist = list.contains(new User(username, password)); return exist; } catch (IOException e) { return false; } } public static void addTestData() { try { File file = new File(fileName); if (!file.exists()) { String add = UserDao.addUser(new User("admin", "123456")); FileUtils.writeTxtFile(add, file, true); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
StartMain.java
package com.sjsq.frame; import com.sjsq.dao.GoodsDao; import com.sjsq.dao.UserDao; /** * 启动类 * * @author Administrator * */ public class StartMain { public static void main(String[] args) { // 启动前先创建测试数据文件(默认账号:admin 默认密码:123456) //UserDao.addTestData(); //GoodsDao.addTestData(); new LoginFrame(); } }
LoginFrame.java
package com.sjsq.frame; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import com.sjsq.dao.UserDao; /** * 登录框架 2019年6月6日18:04:57 * * @author Administrator * */ public class LoginFrame extends Frame { private static final long serialVersionUID = 1L; private JFrame jFrame = new JFrame("登录"); private Container c = jFrame.getContentPane(); private JLabel a1 = new JLabel("用户名"); // 默认账号 private JTextField username = new JTextField("admin"); private JLabel a2 = new JLabel("密 码"); // 默认密码 private JPasswordField password = new JPasswordField("123456"); private JButton okbtn = new JButton("登录"); private JButton cancelbtn = new JButton("重置"); public LoginFrame() { // 设置窗体的位置及大小 jFrame.setBounds(600, 200, 300, 220); // 设置一层相当于桌布的东西 c.setLayout(new BorderLayout());// 布局管理器 // 设置按下右上角X号后关闭 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 初始化--往窗体里放其他控件 jFrame.setLocationRelativeTo(null);// 窗 // 标题部分 JPanel titlePanel = new JPanel(); titlePanel.setLayout(new FlowLayout()); titlePanel.add(new JLabel("超市商品管理系统 ")); c.add(titlePanel, "North"); // 输入部分- JPanel fieldPanel = new JPanel(); fieldPanel.setLayout(null); a1.setBounds(50, 20, 50, 20); a2.setBounds(50, 60, 50, 20); fieldPanel.add(a1); fieldPanel.add(a2); username.setBounds(110, 20, 120, 20); password.setBounds(110, 60, 120, 20); fieldPanel.add(username); fieldPanel.add(password); c.add(fieldPanel, "Center"); // 按钮部分 JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(okbtn); buttonPanel.add(cancelbtn); c.add(buttonPanel, "South"); // 设置窗体可见 jFrame.setVisible(true); // 确认按下去获取 okbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String uname = username.getText(); String pwd = String.valueOf(password.getPassword()); boolean flag = UserDao.auth(uname, pwd); if (flag) { jFrame.setVisible(false); JOptionPane.showMessageDialog(null, "恭喜您,登录成功!"); MainFrame m = new MainFrame(); m.setVisible(true); // 窗口居中 m.setLocationRelativeTo(null); } else { JOptionPane.showMessageDialog(null, "登录失败,账号或密码错误"); } } catch (HeadlessException e1) { username.setText(""); password.setText(""); JOptionPane.showMessageDialog(null, "登录失败"); } } }); // 取消按下去清空 cancelbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { username.setText(""); password.setText(""); } }); } }
MainFrame.java
package com.sjsq.frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import com.sjsq.dao.GoodsDao; import com.sjsq.model.Goods; public class MainFrame extends JFrame { private JPanel contentPane; private JTable table; private String[] columnCount = { "序号", "编号", "名称", "单价", "数量", "总价" }; public static MainFrame frame; List<Goods> list; /** * Create the frame. */ public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("超市商品管理系统 "); setBounds(100, 100, 764, 469); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(29, 58, 692, 332); contentPane.add(scrollPane); table = new JTable(); scrollPane.setViewportView(table); JButton button = new JButton("增加"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new AddFrame(); quaryAll(); } }); button.setBounds(40, 22, 93, 23); contentPane.add(button); JButton button_1 = new JButton("删除"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); if (row == -1) { JOptionPane.showMessageDialog(null, "请选择一行"); return; } String id = list.get(row).getId(); GoodsDao.delete(id); JOptionPane.showMessageDialog(null, "删除成功"); quaryAll(); } }); button_1.setBounds(180, 22, 93, 23); contentPane.add(button_1); JButton button_2 = new JButton("修改"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); if (row == -1) { JOptionPane.showMessageDialog(null, "请选择一行"); return; } String id = (String) table.getModel().getValueAt(row, 0); String bh = (String) table.getModel().getValueAt(row, 1); String name = (String) table.getModel().getValueAt(row, 2); String price = (String) table.getModel().getValueAt(row, 3); String number = (String) table.getModel().getValueAt(row, 4); new UpdateFrame(new Goods(id, bh, name, Double.parseDouble(price), Integer.valueOf(number))); } }); button_2.setBounds(320, 22, 93, 23); contentPane.add(button_2); JButton button_3 = new JButton("查询"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { quaryAll(); } }); button_3.setBounds(460, 22, 93, 23); contentPane.add(button_3); JButton button_4 = new JButton("统计"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("统计"); new StatisticalFrame(); } }); button_4.setBounds(600, 22, 93, 23); contentPane.add(button_4); quaryAll(); } // 查询 public void quaryAll() { list = GoodsDao.query(); // 表格中的内容,是一个二维数组 int size = list.size(); String[][] data = new String[size][6]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { data[i][0] = list.get(i).getId(); data[i][1] = list.get(i).getBh(); data[i][2] = list.get(i).getName(); data[i][3] = list.get(i).getPrice() + ""; data[i][4] = list.get(i).getNumber() + ""; data[i][5] = list.get(i).getTotal() + ""; } } table.setModel(new DefaultTableModel(data, columnCount)); } }
AddFrame.java
package com.sjsq.frame; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.print.Book; import java.util.Random; import java.util.UUID; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import com.sjsq.dao.GoodsDao; import com.sjsq.model.Goods; import com.sjsq.model.User; import com.sjsq.util.StrUtils; /** * 图书修改框架 2019年6月6日18:04:53 * * @author Administrator * */ public class AddFrame extends Frame { private static final long serialVersionUID = 1L; public AddFrame() { JFrame jFrame = new JFrame("新增"); Container c = jFrame.getContentPane(); JLabel a2 = new JLabel("商品名称"); JTextField mc = new JTextField(); JLabel a3 = new JLabel("商品单价"); JTextField dj = new JTextField(); JLabel a4 = new JLabel("商品数量"); JTextField sl = new JTextField(); JButton okbtn = new JButton("新增"); JButton cancelbtn = new JButton("重置"); // 设置窗体的位置及大小 jFrame.setBounds(600, 200, 300, 250); // 设置一层相当于桌布的东西 c.setLayout(new BorderLayout());// 布局管理器 // 设置按下右上角X号后关闭 jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 初始化--往窗体里放其他控件 jFrame.setLocationRelativeTo(null);// 窗 /*标题部分--North*/ JPanel titlePanel = new JPanel(); titlePanel.setLayout(new FlowLayout()); titlePanel.add(new JLabel("新增商品")); c.add(titlePanel, "North"); /*输入部分--Center*/ JPanel fieldPanel = new JPanel(); fieldPanel.setLayout(null); a2.setBounds(50, 20, 100, 20); a3.setBounds(50, 60, 100, 20); a4.setBounds(50, 100, 100, 20); fieldPanel.add(a2); fieldPanel.add(a3); fieldPanel.add(a4); mc.setBounds(110, 20, 120, 20); dj.setBounds(110, 60, 120, 20); sl.setBounds(110, 100, 120, 20); fieldPanel.add(mc); fieldPanel.add(dj); fieldPanel.add(sl); c.add(fieldPanel, "Center"); /*按钮部分--South*/ JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(okbtn); buttonPanel.add(cancelbtn); c.add(buttonPanel, "South"); // 设置窗体可见 jFrame.setVisible(true); // 确认按下去获取 okbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String mc1 = mc.getText(); String dj1 = dj.getText(); String sl1 = sl.getText(); if (" ".equals(mc1) || " ".equals(dj1) || "".equals(sl1)) { JOptionPane.showMessageDialog(null, "请求参数不能为空"); return; } int sl2 = Integer.parseInt(sl1); double dj2 = Double.parseDouble(dj1); Goods g = new Goods(StrUtils.id(), StrUtils.uuid(), mc1, dj2, sl2); boolean flag = GoodsDao.add(g); if (flag) { JOptionPane.showMessageDialog(null, "添加成功"); jFrame.setVisible(false); } else { JOptionPane.showMessageDialog(null, "添加失败"); } } catch (HeadlessException e1) { } } }); // 取消按下去清空 cancelbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { mc.setText(""); dj.setText(""); sl.setText(""); } }); } }
UpdateFrame.java
package com.sjsq.frame; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import com.sjsq.dao.GoodsDao; import com.sjsq.model.Goods; /** * 图书修改框架 2019年6月6日18:04:53 * * @author Administrator * */ public class UpdateFrame extends Frame { private static final long serialVersionUID = 1L; public UpdateFrame(Goods g) { JFrame jFrame = new JFrame("修改"); Container c = jFrame.getContentPane(); JLabel a1 = new JLabel("商品编号"); JTextField bh = new JTextField(g.getBh()); JLabel a2 = new JLabel("商品名称"); JTextField mc = new JTextField(g.getName()); JLabel a3 = new JLabel("商品单价"); JTextField dj = new JTextField(g.getPrice() + ""); JLabel a4 = new JLabel("商品数量"); JTextField sl = new JTextField(g.getNumber() + ""); JButton okbtn = new JButton("修改"); JButton cancelbtn = new JButton("重置"); // 设置窗体的位置及大小 jFrame.setBounds(600, 200, 300, 320); // 设置一层相当于桌布的东西 c.setLayout(new BorderLayout());// 布局管理器 // 设置按下右上角X号后关闭 jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 初始化--往窗体里放其他控件 jFrame.setLocationRelativeTo(null);// 窗 /*标题部分--North*/ JPanel titlePanel = new JPanel(); titlePanel.setLayout(new FlowLayout()); titlePanel.add(new JLabel("修改商品信息")); c.add(titlePanel, "North"); /*输入部分--Center*/ JPanel fieldPanel = new JPanel(); fieldPanel.setLayout(null); a1.setBounds(50, 20, 100, 20); a2.setBounds(50, 60, 100, 20); a3.setBounds(50, 100, 100, 20); a4.setBounds(50, 140, 100, 20); fieldPanel.add(a1); fieldPanel.add(a2); fieldPanel.add(a3); fieldPanel.add(a4); bh.setBounds(110, 20, 120, 20); mc.setBounds(110, 60, 120, 20); dj.setBounds(110, 100, 120, 20); sl.setBounds(110, 140, 120, 20); fieldPanel.add(bh); fieldPanel.add(mc); fieldPanel.add(dj); fieldPanel.add(sl); c.add(fieldPanel, "Center"); /*按钮部分--South*/ JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(okbtn); buttonPanel.add(cancelbtn); c.add(buttonPanel, "South"); // 设置窗体可见 jFrame.setVisible(true); // 确认按下去获取 okbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String bh1 = bh.getText(); String mc1 = mc.getText(); String dj1 = dj.getText(); String sl1 = sl.getText(); String id = g.getId(); Goods g = new Goods(id, bh1, mc1, Double.parseDouble(dj1), Integer.valueOf(sl1)); boolean flag = GoodsDao.update(g); if (flag) { JOptionPane.showMessageDialog(null, "修改成功"); jFrame.setVisible(false); } else { JOptionPane.showMessageDialog(null, "修改失败"); } } catch (HeadlessException e1) { } } }); // 取消按下去清空 cancelbtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { bh.setText(""); mc.setText(""); dj.setText(""); sl.setText(""); } }); } }
StatisticalFrame.java
package com.sjsq.frame; import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import com.sjsq.dao.GoodsDao; /** * 统计 * * @author Administrator * */ public class StatisticalFrame { private static final long serialVersionUID = 1L; public StatisticalFrame() { JFrame jFrame = new JFrame("商品统计信息"); Container c = jFrame.getContentPane(); JLabel a2 = new JLabel("商品资金总额:"); JLabel mc = new JLabel(GoodsDao.tongji().get("totalMoney") + "元"); JLabel a3 = new JLabel("库存最多的商品:"); JLabel dj = new JLabel(GoodsDao.tongji().get("maxGoodsName") + ",库存过多,请时促销处理~"); JLabel a4 = new JLabel("库存不足的商品:"); String kc = (String) GoodsDao.tongji().get("minGoodsName"); String tip = ""; if (kc == null || "".equals(kc)) { tip = "库存很健康,继续保持哟~"; } else { tip = "名称:" + kc + ",库存不足,记得进货哟~"; } JLabel sl = new JLabel(tip); // 设置窗体的位置及大小 jFrame.setBounds(600, 200, 480, 250); // 设置一层相当于桌布的东西 c.setLayout(new BorderLayout());// 布局管理器 // 设置按下右上角X号后关闭 jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 初始化--往窗体里放其他控件 jFrame.setLocationRelativeTo(null);// 窗 /*标题部分--North*/ JPanel titlePanel = new JPanel(); titlePanel.setLayout(new FlowLayout()); titlePanel.add(new JLabel("商品统计")); c.add(titlePanel, "North"); /*输入部分--Center*/ JPanel fieldPanel = new JPanel(); fieldPanel.setLayout(null); a2.setBounds(50, 20, 160, 20); a3.setBounds(50, 60, 160, 20); a4.setBounds(50, 100, 160, 20); fieldPanel.add(a2); fieldPanel.add(a3); fieldPanel.add(a4); mc.setBounds(180, 20, 300, 20); dj.setBounds(180, 60, 300, 20); sl.setBounds(180, 100, 300, 20); fieldPanel.add(mc); fieldPanel.add(dj); fieldPanel.add(sl); c.add(fieldPanel, "Center"); /*按钮部分--South*/ JPanel buttonPanel = new JPanel(); c.add(buttonPanel, "South"); // 设置窗体可见 jFrame.setVisible(true); } }
User.java
package com.sjsq.model; public class User { @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } private String username; private String password; public User(String username, String password) { super(); this.username = username; this.password = password; } 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() { super(); // TODO Auto-generated constructor stub } }
四、其他
1.其他系统实现
Java+Swing实现仓库管理系统
Java+Swing实现学生信息管理系统
Java+Swing实现学生宿舍管理系统
Java+Swing实现学生选课管理系统
Java+Swing实现电子相册管理系统
Java+Swing实现图书管理系统
Java+Swing实现斗地主游戏
Java+Swing实现宠物商店管理系统
Java+Swing实现学生成绩管理系统
Java+Swing实现企业人事管理系统
Java+Swing实现学校教材管理系统
Java+Swing实现学校教务管理系统