【Java 】便利店管理信息系统

介绍:便利店管理信息系统是一个典型的商用系统。在生活中便利店中的角色通常包括店主也就是管理员、店内员工以及来购买的顾客。所以整个系统应分为三个层次:

1、管理员:管理员使用权限最高的管理界面来实现对全店信息的整体管理。对于店内员工,管理员可以查看所有的店内员工信息,并可以进行提拔,录用和开除操作。对于便利店中的商品,管理员可以查看店内所有商品的基本信息,并根据实际情况对指定商品进行补货操作。除此之外在管理员操作界面的首页应设计一个区域用来显示便利店的总收入以及顾客信息,更加方便管理员对便利店进行管理。

2、店员:店员在便利店中的的权限相比于管理员就要少很多。店员可以查看店内的商品信息来决定商品是否需要补货。

3、顾客:因为每天的顾客不固定,而且来便利店的目的只有购买商品,所以只需要向其展示便利店的特色以及店中的商品信息满足顾客的购买需求。

代码:

//Administrators类


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.*;
public class Administrators {
    public JTextArea at;
    public Administrators() {
        at = new JTextArea();
        at.setEditable(false); // 设置为不可编辑
    }
    public void start(String name) {   //初始化管理员界面
        JFrame f1 = new JFrame("管理界面");
        JPanel p1 = new JPanel();//北
        JPanel p2 = new JPanel();//西
        JPanel p3 = new JPanel();//中
        //JPanel p4 = new JPanel();
        JLabel l1 = new JLabel("管理员"+name+"欢迎回来!");
        JScrollPane scrollPane = new JScrollPane(at);
        String way = "D:\\顾客信息.csv";//顾客信息表路径
        JButton b1 = new JButton("首页");
        JButton b2 = new JButton("商品");
        JButton b3 = new JButton("员工信息");    
        Csv c1 = new Csv();
        double sum = c1.sumColumn(way,3);
        JLabel l2 = new JLabel("总收入"+sum+"元");
        loadAndDisplayCSV(way);
        b2.addActionListener(new ActionListener() {//商品按钮,跳转商品类的管理
                public void actionPerformed(ActionEvent e) {
                    Product p1 = new Product();
                    p1.productor(name);
                   f1.dispose();
                   }
            });
         
        b3.addActionListener(new ActionListener(){//员工按钮,跳转员工类管理
             public void actionPerformed(ActionEvent e) {
                 new Salesperson().Display(name);
                 f1.dispose();
             }
         });
        p3.add(at);
        p3.add(scrollPane);
        p2.setLayout(new GridLayout(3,1));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        f1.setSize(600, 400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        p1.add(l2);
        f1.add(p1,BorderLayout.NORTH);
        f1.add(p2,BorderLayout.WEST);
        f1.add(p3,BorderLayout.CENTER);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void loadAndDisplayCSV(String filePath) {//读文件方法
        BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(filePath));
                   String line;
                    while ((line = br.readLine()) != null) {
                        // 去除逗号,并整齐显示
                        String[] fields = line.split(",");
                        StringBuilder formattedLine = new StringBuilder();
                        for (String field : fields) {
                            formattedLine.append(field).append("\t"); // 使用制表符作为字段之间的分隔符
                        }
                        at.append(formattedLine.toString().trim() + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
}

//Csv类,实现对csv文件的一些基本操作


import java.io.*;

import java.util.ArrayList;
import java.util.List;
public class Csv {
    
    //删除指定单元格
     public void modifyCSV(String csvFile, String targetData, int targetColumn, int modifyColumn, String newData)throws IOException {
            // 读取CSV文件内容到二维数组中
            String[][] data = readCSV(csvFile);

            // 查找给定数据所在的单元格位置
            int rowToUpdate = -1;
            for (int row = 0; row < data.length; row++) {
                if (data[row][targetColumn].equals(targetData)) {
                    rowToUpdate = row;
                    break;
                }
            }

            if (rowToUpdate == -1) {
                throw new IllegalArgumentException("未找到指定的目标数据:" + targetData);
            }

            // 修改同一行中指定列的单元格内容
            if (modifyColumn >= 0 && modifyColumn < data[rowToUpdate].length) {
                data[rowToUpdate][modifyColumn] = newData;
            } else {
                throw new IllegalArgumentException("要修改的列超出范围!");
            }

            // 将修改后的数据写回CSV文件
            writeCSV(csvFile, data);
        }

        // 读取CSV文件并返回二维数组
        private  String[][] readCSV(String csvFile) throws IOException {
            BufferedReader reader = new BufferedReader(new FileReader(csvFile));
            String line;
            int lines = 0;
            int fields = 0;

            // 统计行数和列数
            while ((line = reader.readLine()) != null) {
                lines++;
                String[] parts = line.split(",");
                if (fields == 0) {
                    fields = parts.length;
                }
            }

            reader.close();

            // 创建二维数组
            String[][] data = new String[lines][fields];
            // 重新读取CSV文件并填充二维数组
            reader = new BufferedReader(new FileReader(csvFile));
            int row = 0;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                for (int col = 0; col < parts.length; col++) {
                    data[row][col] = parts[col];
                }
                row++;
            }
            reader.close();
            return data;
        }

        // 将二维数组写回CSV文件
        private void writeCSV(String csvFile, String[][] data) throws IOException {
            BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    sb.append(data[i][j]);
                    if (j < data[i].length - 1) {
                        sb.append(",");
                    }
                }
                sb.append("\n");
            }
            writer.write(sb.toString());
            writer.close();
        }
    //删一行
     public void deleteCSVRow(String csvFile, int rowToDelete) throws IOException {
            List<List<String>> rows = new ArrayList<>();

            // 读取CSV文件
            try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
                String line;
                while ((line = br.readLine()) != null) {
                    String[] data = line.split(","); // CSV文件的列使用逗号分隔
                    List<String> rowData = new ArrayList<>();
                    for (String datum : data) {
                        rowData.add(datum);
                    }
                    rows.add(rowData);
                }
            }

            // 删除指定的行
            if (rowToDelete < rows.size()) {
                rows.remove(rowToDelete);
            } else {
                throw new IndexOutOfBoundsException("Row index is out of bounds.");
            }

            // 将更新后的数据写回CSV文件
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(csvFile))) {
                for (List<String> rowData : rows) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < rowData.size(); i++) {
                        if (i > 0) {
                            sb.append(",");
                        }
                        sb.append(rowData.get(i));
                    }
                    bw.write(sb.toString());
                    bw.newLine();
                }
            }
        }
     
     //追加一行
     public void appendToCSV(String csvFile, String[] data) throws IOException {
            BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile, true)); // true 表示追加模式
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.length; i++) {
                if (i > 0) {
                    sb.append(",");
                }
                sb.append(data[i]);
            }
            sb.append("\n"); 
            // 写入数据行到文件
            writer.write(sb.toString());
            writer.close();
        }
    //某列除第一个单元格相加
     public double sumColumn(String filename, int columnIndex) {
            double sum = 0;

            try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
                String line;
                while ((line = br.readLine()) != null) {
                    String[] fields = line.split(",");
                    if (columnIndex < fields.length && fields[columnIndex].matches("-?\\d+(\\.\\d+)?")) {
                        try {
                            double value = Double.parseDouble(fields[columnIndex]);
                            sum += value;
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return sum;
        }
     
     //获取已知单元格同行的其他单元格
     public String[] getValuesInSameRowToArray(String csvFilePath, String targetCellValue, int targetColumnIndex) {
            int targetRowIndex = -1;
            String[] result = new String[0]; // 默认返回空数组

            try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {
                String line;
                int rowIndex = 0;
                String[] columns = null; // 声明,初始化为 null

                while ((line = br.readLine()) != null) {
                    columns = line.split(","); // 假设CSV文件是逗号分隔的
                    if (columns.length > targetColumnIndex && columns[targetColumnIndex].equals(targetCellValue)) {
                        targetRowIndex = rowIndex; // 找到目标行
                        break;
                    }

                    rowIndex++;
                }

                if (targetRowIndex != -1 && columns != null) { // 检查columns是否为null
                    List<String> valuesList = new ArrayList<>();
                    for (int colIndex = 0; colIndex < columns.length; colIndex++) {
                        if (colIndex != targetColumnIndex) {
                            valuesList.add(columns[colIndex]); // 将目标行除了目标列的值添加到列表中
                        }
                    }
                    result = valuesList.toArray(new String[0]); // 转换成字符串数组并返回
                } else {
                    System.out.println("Target value not found in the CSV file.");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
     
     //编号处理方法
     public int getLastValueFromCsv(String csvFilePath) {
            int lastValue = 0; //如果未找到数据或者转换失败,返回0
            try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {
                String line;
                while ((line = br.readLine()) != null) {
                    String[] columns = line.split(","); // CSV文件是逗号分隔的
                    if (columns.length > 0) {
                        try {
                            lastValue = Integer.parseInt(columns[0]); // 将第一列数据转换为int型
                        } catch (NumberFormatException e) {
                            // 异常处理
                            System.err.println("Failed to parse integer from CSV file.");
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return lastValue;
        }
     
     //对指定单元格进行修改
     public void modifyCSVCell(String csvFilePath, int rowIndex, int colIndex, int numberToAdd) {
            // 临时文件路径
            String tempCsvFilePath = csvFilePath + ".tmp";
            // 读取和修改CSV文件
            try (BufferedReader reader = new BufferedReader(new FileReader(csvFilePath));
                 BufferedWriter writer = new BufferedWriter(new FileWriter(tempCsvFilePath))) {

                String line;
                int currentRow = 0;
                while ((line = reader.readLine()) != null) {
                    if (currentRow == rowIndex) {
                        // 找到指定行
                        String[] cells = line.split(",");
                        if (colIndex >= 0 && colIndex < cells.length) {
                            // 找到指定列
                            try {
                                int oldValue = Integer.parseInt(cells[colIndex].trim());
                                int newValue = oldValue + numberToAdd;
                                cells[colIndex] = Integer.toString(newValue);
                            } catch (NumberFormatException e) {
                                // 处理非数字情况
                                e.printStackTrace();
                            }
                        }
                        // 构建修改后的行
                        StringBuilder newLine = new StringBuilder();
                        for (int i = 0; i < cells.length; i++) {
                            newLine.append(cells[i]);
                            if (i < cells.length - 1) {
                                newLine.append(",");
                            }
                        }
                        line = newLine.toString();
                    }
                    // 写入到临时文件
                    writer.write(line);
                    writer.newLine();
                    currentRow++;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 替换原始CSV文件
            replaceOriginalCsvFile(csvFilePath, tempCsvFilePath);
        }

        private void replaceOriginalCsvFile(String originalFilePath, String tempFilePath) {
            // 删除原始文件
            java.io.File originalFile = new java.io.File(originalFilePath);
            if (!originalFile.delete()) {
                System.err.println("Failed to delete the original file: " + originalFilePath);
                return;
            }
            // 重命名临时文件为原始文件名
            java.io.File tempFile = new java.io.File(tempFilePath);
            if (!tempFile.renameTo(originalFile)) {
                System.err.println("Failed to rename the temporary file: " + tempFilePath + " to " + originalFilePath);
            }
        }
        
        public void modifyCSVValue(String csvFile, int rowIndex, int colIndex) throws IOException {
            // 读取CSV文件
            BufferedReader reader = new BufferedReader(new FileReader(csvFile));
            String line;
            StringBuilder sb = new StringBuilder();
            int currentRow = 0;
            while ((line = reader.readLine()) != null) {
                if (currentRow == rowIndex) {
                    String[] parts = line.split(",");
                    if (colIndex < parts.length) {
                        try {
                            int oldValue = Integer.parseInt(parts[colIndex].trim()); // 原始值
                            int newValue = oldValue - 1;
                            parts[colIndex] = String.valueOf(newValue); // 更新值
                        } catch (NumberFormatException e) {
                            System.err.println("Invalid number format in CSV.");
                            e.printStackTrace();
                        }
                    } else {
                        System.err.println("Column index is out of bounds.");
                    }
                    line = String.join(",", parts);
                }
                sb.append(line).append("\n");
                currentRow++;
            }
            reader.close();

            // 写回CSV文件
            BufferedWriter writer = new BufferedWriter(new FileWriter(csvFile));
            writer.write(sb.toString());
            writer.close();
        }
}

//Customer类,顾客类


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Customer {
    public void start(String name) {//初始化顾客界面
            JFrame f1 = new JFrame("顾客管理界面");
            JPanel p1 = new JPanel();//北
            JPanel p2 = new JPanel();//西
            JPanel p3 = new JPanel();//中
            JLabel l1 = new JLabel(name+"欢迎光临!");
            JLabel l2 = new JLabel();
            JButton b1 = new JButton("首页");
            JButton b2 = new JButton("商品");    
            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                       new Product().Cproductor(name);
                       f1.dispose();
                       }
                });
            ImageIcon icon = new ImageIcon("D:\\OIP-C (1).jpg");//顾客首页图片路径
             icon.setImage(icon.getImage().getScaledInstance(600,400,Image.SCALE_DEFAULT));
             l2.setIcon(icon);
            p2.setLayout(new GridLayout(3,1));
            p2.add(b1);
            p2.add(b2);
            f1.setSize(600, 400);
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            p1.add(l1);
            p3.add(l2);
            f1.add(p1,BorderLayout.NORTH);
            f1.add(p2,BorderLayout.WEST);
            f1.add(p3,BorderLayout.CENTER);
            f1.setLocationRelativeTo(null);
            f1.setVisible(true);
        }
    }
    

//主类

public class Main {
    public static void main(String[] args) {
        Sign_in s1 = new Sign_in();
        s1.start();
    }
}

//Product商品类


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
public class Product {
    public JTextArea at;
    public Product() {
        at = new JTextArea();
        at.setEditable(false); // 设置为不可编辑
    }
    public void productor(String name) {//面向管理员的管理接口
        JFrame f1 = new JFrame("商品管理界面");
        JPanel p1 = new JPanel();//北
        JPanel p2 = new JPanel();//西
        JPanel p3 = new JPanel();//中
        JLabel l1 = new JLabel(name+"欢迎回来!");
        JButton b1 = new JButton("返回");
        JButton b2 = new JButton("管理");
        Csv c1 = new Csv();
        String way = "D:\\商品信息.csv";//商品信息表路径
        JScrollPane scrollPane = new JScrollPane(at);
        
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               Administrators a1 = new Administrators();
               a1.start(name);
               f1.dispose();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 创建c1.input窗口
                JFrame f21 = new JFrame("补货界面");
                JPanel p21 = new JPanel();
                JLabel l21 = new JLabel("商品编号:");
                JLabel l31 = new JLabel("补货数量:");

                JTextField t21 = new JTextField(10);
                JTextField t31 = new JTextField(10);

                Csv c21 = new Csv();
                JButton b21 = new JButton("补货");
                JButton b31 = new JButton("返回");
                String way11 = "D:\\商品信息.csv";//商品信息路径
                b21.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int linesign= Integer.parseInt(t21.getText());
                        int number = Integer.parseInt(t21.getText());
                        c21.modifyCSVCell(way11, linesign, 3, number);
                        JOptionPane.showMessageDialog(f21, "补货成功!");
                    }
                });
                b31.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                            Administrators a1 = new Administrators();
                            a1.start(name);
                            f1.dispose();
                    }
                });
                f21.setSize(400, 200);
                f21.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                p21.add(l21);
                p21.add(t21);
                p21.add(l31);
                p21.add(t31);
                p21.add(b21);
                p21.add(b31);
                f21.add(p21);
                f21.setLocationRelativeTo(null);
                f21.setVisible(true);
                f21.addWindowListener(new WindowAdapter() {
                    public void windowClosed(WindowEvent e) {
                        // 当c1.input窗口关闭时才创建a1.start(name)窗口
                        Administrators a2 = new Administrators();
                        a2.start(name);
                        f21.dispose(); // 关闭主窗口
                    }
                });
                // 显示c1.input窗口
                f21.setVisible(true);
            }
        });
        
        p2.setLayout(new GridLayout(3,1));
        p2.add(b2);
        p2.add(b1);
        p3.add(at);
        p3.add(scrollPane);
        f1.setSize(600, 400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        f1.add(p1,BorderLayout.NORTH);
        f1.add(p2,BorderLayout.WEST);
        f1.add(p3);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void loadAndDisplayCSV(String filePath) {//打印csv表
            BufferedReader br = null;

            try {
                br = new BufferedReader(new FileReader(filePath));
                String line;
                while ((line = br.readLine()) != null) {
                    String[] fields = line.split(",");
                    StringBuilder formattedLine = new StringBuilder();
                    for (String field : fields) {
                        formattedLine.append(field).append("\t"); //使用制表符作为字段之间的分隔符
                    }
                    at.append(formattedLine.toString().trim() + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    public void pproductor(String name) {//面向店员的界面
        JFrame f1 = new JFrame("商品管理界面");
        JPanel p1 = new JPanel();//北
        JPanel p2 = new JPanel();//西
        JPanel p3 = new JPanel();//中
        JLabel l1 = new JLabel(name);
        JButton b1 = new JButton("返回");
        JButton b2 = new JButton("管理");
        Csv c1 = new Csv();
        String way = "D:\\商品信息.csv";//商品信息路径
        JScrollPane scrollPane = new JScrollPane(at);
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               Salesperson s1 = new Salesperson();
               s1.start(name);
               f1.dispose();
            }
        });
        
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 创建c1.input窗口
                JFrame f21 = new JFrame("补货界面");
                JPanel p21 = new JPanel();
                JLabel l21 = new JLabel("商品编号:");
                JLabel l31 = new JLabel("补货数量:");

                JTextField t21 = new JTextField(10);
                JTextField t31 = new JTextField(10);

                Csv c21 = new Csv();
                JButton b21 = new JButton("补货");
                JButton b31 = new JButton("返回");
                String way11 = "D:\\商品信息.csv";//商品信息路径
                b21.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        int linesign= Integer.parseInt(t21.getText());
                        int number = Integer.parseInt(t21.getText());
                        c21.modifyCSVCell(way11, linesign, 3, number);
                        JOptionPane.showMessageDialog(f21, "补货成功!");
                    }
                });
                b31.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                            Salesperson a1 = new Salesperson();
                            a1.start(name);
                            f1.dispose();
                    }
                });
                f21.setSize(400, 200);
                f21.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                p21.add(l21);
                p21.add(t21);
                p21.add(l31);
                p21.add(t31);
                p21.add(b21);
                p21.add(b31);
                f21.add(p21);
                f21.setLocationRelativeTo(null);
                f21.setVisible(true);
                f21.addWindowListener(new WindowAdapter() {
                    public void windowClosed(WindowEvent e) {
                        // 当c1.input窗口关闭时才创建a1.start(name)窗口
                        Administrators a2 = new Administrators();
                        a2.start(name);
                        f21.dispose(); // 关闭主窗口
                    }
                });

                // 显示c1.input窗口
                f21.setVisible(true);
            }
        });
        
        p2.setLayout(new GridLayout(3,1));
        p2.add(b2);
        p2.add(b1);
        p3.add(at);
        p3.add(scrollPane);
        f1.setSize(600, 400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        f1.add(p1,BorderLayout.NORTH);
        f1.add(p2,BorderLayout.WEST);
        f1.add(p3);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void Cproductor(String name) {//面向顾客的界面
        JFrame f1 = new JFrame("商品界面");
        JPanel p1 = new JPanel();//北
        JPanel p2 = new JPanel();//西
        JPanel p3 = new JPanel();//中
        JLabel l1 = new JLabel();
        JButton b1 = new JButton("返回");
        JButton b2 = new JButton("购买");
        String way = "D:\\商品信息.csv";//商品信息路径
        JScrollPane scrollPane = new JScrollPane(at);
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Customer c1 = new Customer();
                c1.start(name);
                f1.dispose();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Buy(name);
               f1.dispose();
            }
        });
        p2.setLayout(new GridLayout(3,1));
        p2.add(b1);
        p2.add(b2);
        p3.add(at);
        p3.add(scrollPane);
        f1.setSize(600, 400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        f1.add(p1,BorderLayout.NORTH);
        f1.add(p2,BorderLayout.WEST);
        f1.add(p3);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void Buy(String name) {//顾客购买操作
        JFrame f1 = new JFrame("待支付");
        JPanel p1 = new JPanel();
        JLabel l1 = new JLabel("输入产品编号完成购买:");
        JTextField t1 = new JTextField(10);
        Csv c1 = new Csv();
        JButton b1 = new JButton("购买");
        JButton b2 = new JButton("返回");
        String way = "D:\\商品信息.csv";//商品信息路径
        String way2 = "D:\\顾客信息.csv";//顾客信息路径
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String []data = c1.getValuesInSameRowToArray(way, t1.getText(), 0);
                String []data2 = new String[4];
                int num = c1.getLastValueFromCsv(way2)+1;
                data2[0] = Integer.toString(num);
                data2[1] = name;
                data2[2] = data[0];
                data2[3] = data[1];
                try {
                    c1.modifyCSVValue(way,num,3);
                    c1.appendToCSV(way2, data2);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                JOptionPane.showMessageDialog(f1, "购买成功!");
                Customer a1 = new Customer();
                a1.start(name);
                f1.dispose();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Customer a1 = new Customer();
                a1.start("name");
                f1.dispose();
            }
        });
        f1.setSize(400, 200);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        p1.add(b1);
        p1.add(t1);
        p1.add(b1);
        p1.add(b2);
        f1.add(p1);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
}
}

//Salesperson店员类

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Salesperson {
    public JTextArea at;
    public Salesperson() {
        at = new JTextArea();
        at.setEditable(false); // 设置为不可编辑
    }
    public void start(String name) {//初始化员工界面
        JFrame f1 = new JFrame("顾客管理界面");
        JPanel p1 = new JPanel();//北
        JPanel p2 = new JPanel();//西
        JLabel l1 = new JLabel(name+"欢迎登录!");
        JLabel l2 = new JLabel();
        JButton b1 = new JButton("首页");
        JButton b2 = new JButton("商品信息");    
         b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   new Product().pproductor(name);
                   f1.dispose();
                   }
            });
         ImageIcon icon = new ImageIcon("D:\\OIP-C.jpg");//店员首页图片
         icon.setImage(icon.getImage().getScaledInstance(600,400,Image.SCALE_DEFAULT));
         l2.setIcon(icon);
         
        f1.add(l2,BorderLayout.CENTER);
        p2.setLayout(new GridLayout(3,1));
        p2.add(b1);
        p2.add(b2);
        f1.setSize(600, 400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        f1.add(p1,BorderLayout.NORTH);
        f1.add(p2,BorderLayout.WEST);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void impore() {//提拔方法
        JFrame f1 = new JFrame("员工信息界面");
        JPanel p1 = new JPanel();
        JLabel l1 = new JLabel("员工编号:");
        JTextField t1 = new JTextField(10);
        Csv c1 = new Csv();
        JButton b1 = new JButton("提拔");
        JButton b2 = new JButton("返回");
        String way = "D:\\人员信息.csv";//人员信息表路径
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                 c1.modifyCSV(way, t1.getText(), 0, 4, "管理员");;
                 JOptionPane.showMessageDialog(f1, "提拔成功!");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Administrators a1 = new Administrators();
                a1.start("张三");
                f1.dispose();;
            }
        });
        f1.setSize(400, 200);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        p1.add(t1);
        p1.add(b1);
        p1.add(b2);
        f1.add(p1);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
}
    
    public void manages() {//管理,面向管理员。实现的操作有增加员工,开除员工,提拔员工
        JFrame f1 = new JFrame("员工信息管理界面");
        JPanel p1 = new JPanel();//北
        JPanel p2 = new JPanel();//西
        JPanel p3 = new JPanel();//中
        JLabel l1 = new JLabel();
        JButton b1 = new JButton("录用员工");
        JButton b2 = new JButton("开除员工");
        JButton b3 = new JButton("提拔员工");
        JScrollPane scrollPane = new JScrollPane(at);
        b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            addSalesperson();
            f1.dispose();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //跳转到本类的新界面
                deleteSalesperson();
                f1.dispose();
                }
            });
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //跳转到本类的新界面
                impore();
                f1.dispose();
            }
            });
        p2.setLayout(new GridLayout(3,1));
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p3.add(scrollPane);
        f1.setSize(600, 400);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        f1.add(p1,BorderLayout.NORTH);
        f1.add(p2,BorderLayout.WEST);
        f1.add(p3);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void deleteSalesperson() {//开除员工操作
        JFrame f1 = new JFrame("员工信息界面");
        JPanel p1 = new JPanel();
        JLabel l1 = new JLabel("员工编号:");
        JTextField t1 = new JTextField(10);
        Csv c1 = new Csv();
        JButton b1 = new JButton("删除");
        JButton b2 = new JButton("返回");
        String way = "D:\\人员信息.csv";//人员信息路径
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int linshi= Integer.parseInt(t1.getText());
                 c1.deleteCSVRow(way,linshi);
                 JOptionPane.showMessageDialog(f1, "开除成功!");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Administrators a1 = new Administrators();
                a1.start("张三");
                f1.dispose();;
            }
        });
        f1.setSize(400, 200);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        p1.add(t1);
        p1.add(b1);
        p1.add(b2);
        f1.add(p1);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
}
    
    public void addSalesperson() {//添加员工方法
        JFrame f1 = new JFrame("员工信息界面");
        JPanel p1 = new JPanel();//北
        JLabel l1 = new JLabel("员工编号:");
        JLabel l2 = new JLabel("员工姓名:");
        JLabel l3 = new JLabel("员工年龄:");
        JLabel l4 = new JLabel("员工性别:");
        JLabel l5 = new JLabel("员工级别:");
        JLabel l6 = new JLabel("员工密码:");
        JTextField t1 = new JTextField(10);
        JTextField t2 = new JTextField(10);
        JTextField t3 = new JTextField(10);
        JTextField t4 = new JTextField(10);
        JTextField t5 = new JTextField(10);
        JTextField t6 = new JTextField(10);
        Csv c1 = new Csv();
        JButton b1 = new JButton("添加");
        JButton b2 = new JButton("返回");
        String way = "D:\\人员信息.csv";//人员信息路径
        loadAndDisplayCSV(way);
        b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                String []data = new String[6];
                data[0] = t1.getText();
                data[1] = t2.getText();
                data[2] = t3.getText();
                data[3] = t4.getText();
                data[4] = t5.getText();
                data[5] = t6.getText();
                c1.appendToCSV(way,data);
                JOptionPane.showMessageDialog(f1, "录用成功!");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Administrators a1 = new Administrators();
                a1.start("张三");
                f1.dispose();;
                }
            });
        f1.setSize(200, 400);
           f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1.add(l1);
        p1.add(t1);
        p1.add(l2);
        p1.add(t2);
        p1.add(l3);
        p1.add(t3);
        p1.add(l4);
        p1.add(t4);
        p1.add(l5);
        p1.add(t5);
        p1.add(l6);
        p1.add(t6);
        p1.add(b1);
        p1.add(b2);
        f1.add(p1);
        f1.setLocationRelativeTo(null);
        f1.setVisible(true);
    }
    
    public void Display(String name) {          //显示员工信息,面向管理员
            JFrame f1 = new JFrame("员工信息界面");
            JPanel p1 = new JPanel();//北
            JPanel p2 = new JPanel();//西
            JPanel p3 = new JPanel();//中
            //JPanel p4 = new JPanel();
            JLabel l1 = new JLabel(name+"欢迎回来!");
            //JLabel l2 = new JLabel();
            JButton b1 = new JButton("返回");
            JButton b2 = new JButton("管理");
            String way = "D:\\人员信息.csv";//人员信息路径
            JScrollPane scrollPane = new JScrollPane(at);
            loadAndDisplayCSV(way);
            b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 Administrators a1 = new Administrators();
                   a1.start(name);
                   f1.dispose();
                }
            });
            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //跳转到本类的新界面
                    manages();
                    f1.dispose();
                    }
                });
            p2.setLayout(new GridLayout(3,1));
            p2.add(b1);
            p2.add(b2);
            p3.add(at);
            p3.add(scrollPane);
            f1.setSize(600, 400);
               f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            p1.add(l1);
            f1.add(p1,BorderLayout.NORTH);
            f1.add(p2,BorderLayout.WEST);
            f1.add(p3);
            f1.setLocationRelativeTo(null);
            f1.setVisible(true);
    }
    
    public void loadAndDisplayCSV(String filePath) {//读文件方法
        BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(filePath));
                   String line;
                    while ((line = br.readLine()) != null) {
                        // 去除逗号,并整齐显示
                        String[] fields = line.split(",");
                        StringBuilder formattedLine = new StringBuilder();
                        for (String field : fields) {
                            formattedLine.append(field).append("\t"); // 使用制表符作为字段之间的分隔符
                        }
                        at.append(formattedLine.toString().trim() + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    }

//Sign_in登录界面

import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Sign_in {
        public void start() {//初始化登录界面
            JFrame f1 = new JFrame("用户登录界面");
            JPanel p1 = new JPanel();
            JComboBox c1;
            JTextField t1 = new JTextField(20);
            JPasswordField pw1 = new JPasswordField(20);
            JButton b1 = new JButton("登录");
            JButton b2 = new JButton("清空密码");
            String way = "D:\\人员信息.csv";//人员信息路径
            String[] users = {"管理员", "店员", "顾客"};
            f1.setSize(600, 400);
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            p1.setLayout(new GridLayout(4, 2));
            c1 = new JComboBox<>(users);
            p1.add(new JLabel("    用户名:"));
            p1.add(c1);
            p1.add(new JLabel("    账户:"));
            p1.add(t1);
            p1.add(new JLabel("    密码:"));
            p1.add(pw1);
            b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String selectedUser = (String) c1.getSelectedItem();
                    String name = t1.getText();
                    String password = new String(pw1.getPassword());
                    if(selectedUser == "顾客") {
                        Customer p1 = new Customer();
                        p1.start(name);
                        f1.dispose();
                        return;
                    }
                    try {
                        if(isMatch(way,name,selectedUser,password)) {
                            JOptionPane.showMessageDialog(f1, "登陆成功!");
                            if(selectedUser == "管理员") {
                                Administrators a1 = new Administrators();
                                a1.start(name);
                            }
                            if(selectedUser == "店员") {
                                Salesperson s1 = new Salesperson();
                                s1.start(name);
                            }
                        }else {
                            JOptionPane.showMessageDialog(f1, "用户名或密码错误,请重试", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (HeadlessException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            p1.add(b1);
            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    t1.setText("");
                    pw1.setText("");
                }
            });
            p1.add(b2);
            f1.add(p1);
            f1.setLocationRelativeTo(null);
            f1.setVisible(true);
        }

        //判断登录信息是否正确
        public boolean isMatch(String csvFile, String name, String level, String password) throws IOException {
            BufferedReader br = null;
            String line;
            String cvsSplitBy = ",";

            try {
                br = new BufferedReader(new FileReader(csvFile));
                while ((line = br.readLine()) != null) {
                    String[] data = line.split(cvsSplitBy);
                    // CSV文件中的字段顺序为 序号, 姓名, 性别, 年龄, 级别, 密码
                    if (data.length >= 6) {
                        String csvName = data[1].trim(); // 姓名所在的索引为1
                        String csvLevel = data[4].trim(); // 级别所在的索引为4
                        String csvPassword = data[5].trim(); // 密码所在的索引为5

                        if (csvName.equals(name) && csvLevel.equals(level) && csvPassword.equals(password)) {
                            return true;
                        }
                    }
                }
            } finally {
                if (br != null) {
                    br.close();
                }
            }
            return false;
        }
    }
———————————————————————————————————————————

csv文件:

商品信息.csv

顾客信息.csv

人员信息.csv

———————————————————————————————————————————测试方法:

运行程序,使用管理员身份登录

点击商品按钮可以管理商品数量,进行适当补货

点击员工信息按钮,可以对员工进行管理,执行录用、提拔、开除操作

回到登录界面,以店员身份登录,实行对商品的管理

以顾客身份登录不需要密码,登录后可以购买商品

———————————————————————————————————————————

最后叭叭两句:程序中所实现的功能都是我一个人想出来的,可能有些考虑不周,所以只当是一个经验分享吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值