0
点赞
收藏
分享

微信扫一扫

Java语言实现美发店理发师排班程序

TiaNa_na 2024-10-12 阅读 8

摘要

在现代服务行业中,合理的排班系统对于提升工作效率和顾客满意度至关重要。本文将详细介绍如何使用Java语言编写一个美发店理发师排班程序。该程序能够实现理发师的排班管理、顾客预约管理、以及工作统计功能。通过示例代码和详细解释,读者将能够掌握如何设计和实现一个完整的排班系统,并学习Java编程的一些核心概念。

1. 引言

随着美发行业的快速发展,顾客对服务质量和时间的要求也日益提高。因此,合理而高效的理发师排班系统不仅可以提高美发店的运营效率,还能提升顾客的满意度。Java作为一门跨平台的编程语言,非常适合开发这样一个管理系统。

2. 开发环境准备

2.1 JDK安装

确保你的计算机上安装了Java Development Kit (JDK)。可以从Oracle官网下载最新版的JDK。

安装完成后,配置环境变量。打开命令行工具,输入以下命令以确认JDK安装成功:

java -version

2.2 IDE选择

建议使用集成开发环境(IDE),如Eclipse或IntelliJ IDEA。下载并安装一个你喜欢的IDE。

2.3 创建项目

在IDE中创建一个新的Java项目,命名为HairSalonScheduler

3. 理发师和顾客类设计与实现

3.1 理发师类的设计

理发师类Barber将包含理发师的基本信息。

public class Barber {
    private String id; // 理发师编号
    private String name; // 理发师姓名
    private String specialization; // 理发师专长
    private int workingHours; // 工作时长

    // 构造方法
    public Barber(String id, String name, String specialization, int workingHours) {
        this.id = id;
        this.name = name;
        this.specialization = specialization;
        this.workingHours = workingHours;
    }

    // Getter和Setter方法
    public String getId() { return id; }
    public String getName() { return name; }
    public String getSpecialization() { return specialization; }
    public int getWorkingHours() { return workingHours; }

    @Override
    public String toString() {
        return "Barber{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", specialization='" + specialization + '\'' +
                ", workingHours=" + workingHours +
                '}';
    }
}

3.2 顾客类的设计

顾客类Customer将包含顾客的基本信息。

public class Customer {
    private String customerId; // 顾客编号
    private String name; // 顾客姓名
    private String phoneNumber; // 顾客电话

    // 构造方法
    public Customer(String customerId, String name, String phoneNumber) {
        this.customerId = customerId;
        this.name = name;
        this.phoneNumber = phoneNumber;
    }

    // Getter和Setter方法
    public String getCustomerId() { return customerId; }
    public String getName() { return name; }
    public String getPhoneNumber() { return phoneNumber; }

    @Override
    public String toString() {
        return "Customer{" +
                "customerId='" + customerId + '\'' +
                ", name='" + name + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                '}';
    }
}

4. 排班管理

在排班管理模块中,我们需要设计一个Schedule类来管理理发师的工作安排。

4.1 Schedule类的设计

import java.util.HashMap;
import java.util.Map;

public class Schedule {
    private Map<String, Barber> barberMap; // 理发师列表
    private Map<String, String> scheduleMap; // 理发师排班

    public Schedule() {
        barberMap = new HashMap<>();
        scheduleMap = new HashMap<>();
    }

    public void addBarber(Barber barber) {
        barberMap.put(barber.getId(), barber);
    }

    public void scheduleBarber(String barberId, String day) {
        if (barberMap.containsKey(barberId)) {
            scheduleMap.put(day, barberId);
        } else {
            System.out.println("理发师不存在");
        }
    }

    public void displaySchedule() {
        for (Map.Entry<String, String> entry : scheduleMap.entrySet()) {
            System.out.println("日期: " + entry.getKey() + " - 理发师: " + barberMap.get(entry.getValue()).getName());
        }
    }
}

4.2 增加排班功能

Schedule类中增加一个方法用以查看特定日期的排班情况:

public String getBarberByDate(String day) {
    if (scheduleMap.containsKey(day)) {
        return barberMap.get(scheduleMap.get(day)).getName();
    }
    return "该日期没有排班信息";
}

5. 顾客预约管理

顾客预约管理模块用于处理顾客的预约信息。

5.1 预约类的设计

import java.util.ArrayList;
import java.util.List;

public class Appointment {
    private Customer customer;
    private Barber barber;
    private String appointmentDate; // 预约日期
    private String appointmentTime; // 预约时间

    public Appointment(Customer customer, Barber barber, String appointmentDate, String appointmentTime) {
        this.customer = customer;
        this.barber = barber;
        this.appointmentDate = appointmentDate;
        this.appointmentTime = appointmentTime;
    }

    // Getter和Setter方法
    public Customer getCustomer() { return customer; }
    public Barber getBarber() { return barber; }
    public String getAppointmentDate() { return appointmentDate; }
    public String getAppointmentTime() { return appointmentTime; }
}

5.2 预约管理类

我们需要一个AppointmentManager类来管理所有预约信息。

public class AppointmentManager {
    private List<Appointment> appointments;

    public AppointmentManager() {
        appointments = new ArrayList<>();
    }

    public void addAppointment(Appointment appointment) {
        appointments.add(appointment);
    }

    public void displayAppointments() {
        for (Appointment appointment : appointments) {
            System.out.println("顾客: " + appointment.getCustomer().getName() +
                    " - 理发师: " + appointment.getBarber().getName() +
                    " - 日期: " + appointment.getAppointmentDate() +
                    " - 时间: " + appointment.getAppointmentTime());
        }
    }
}

6. 工作统计

为了获取理发师的工作情况,我们需要设计一个统计模块。

6.1 工作统计类

import java.util.HashMap;
import java.util.Map;

public class WorkStatistics {
    private Map<String, Integer> barberWorkHours; // 理发师工作时长

    public WorkStatistics() {
        barberWorkHours = new HashMap<>();
    }

    public void addWorkHours(String barberId, int hours) {
        barberWorkHours.put(barberId, barberWorkHours.getOrDefault(barberId, 0) + hours);
    }

    public void displayStatistics() {
        for (Map.Entry<String, Integer> entry : barberWorkHours.entrySet()) {
            System.out.println("理发师ID: " + entry.getKey() + " - 工作时长: " + entry.getValue() + "小时");
        }
    }
}

7. 用户界面设计与实现

为了使系统更易于使用,我们需要设计一个简单的用户界面。这里我们将使用Java Swing来实现图形界面。

7.1 创建主界面

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

public class MainFrame extends JFrame {
    private Schedule schedule;
    private AppointmentManager appointmentManager;
    private WorkStatistics workStatistics;

    public MainFrame() {
        schedule = new Schedule();
        appointmentManager = new AppointmentManager();
        workStatistics = new WorkStatistics();

        setTitle("美发店理发师排班系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 添加按钮和面板
        JPanel panel = new JPanel();
        JButton addBarberButton = new JButton("添加理发师");
        JButton scheduleBarberButton = new JButton("排班");
        JButton addAppointmentButton = new JButton("添加预约");
        JButton viewAppointmentsButton = new JButton("查看预约");

        panel.add(addBarberButton);
        panel.add(scheduleBarberButton);
        panel.add(addAppointmentButton);
        panel.add(viewAppointmentsButton);

        add(panel, BorderLayout.SOUTH);

        // 添加按钮事件
        addBarberButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addBarber();
            }
        });

        scheduleBarberButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                scheduleBarber();
            }
        });

        addAppointmentButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addAppointment();
            }
        });

        viewAppointmentsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                viewAppointments();
            }
        });

    }

    private void addBarber() {
        // 弹出对话框从用户获取理发师信息
        String id = JOptionPane.showInputDialog("请输入理发师编号");
        String name = JOptionPane.showInputDialog("请输入理发师姓名");
        String specialization = JOptionPane.showInputDialog("请输入理发师专长");
        int workingHours = Integer.parseInt(JOptionPane.showInputDialog("请输入工作时长"));

        Barber barber = new Barber(id, name, specialization, workingHours);
        schedule.addBarber(barber);
        JOptionPane.showMessageDialog(this, "理发师添加成功");
    }

    private void scheduleBarber() {
        // 弹出对话框获取排班信息
        String barberId = JOptionPane.showInputDialog("请输入理发师编号");
        String day = JOptionPane.showInputDialog("请输入排班日期");
        
        schedule.scheduleBarber(barberId, day);
        JOptionPane.showMessageDialog(this, "排班成功");
    }

    private void addAppointment() {
        // 弹出对话框获取预约信息
        String customerId = JOptionPane.showInputDialog("请输入顾客编号");
        String customerName = JOptionPane.showInputDialog("请输入顾客姓名");
        String phoneNumber = JOptionPane.showInputDialog("请输入顾客电话");
        String barberId = JOptionPane.showInputDialog("请输入理发师编号");
        String appointmentDate = JOptionPane.showInputDialog("请输入预约日期");
        String appointmentTime = JOptionPane.showInputDialog("请输入预约时间");

        Customer customer = new Customer(customerId, customerName, phoneNumber);
        Barber barber = schedule.getBarberByDate(barberId);
        Appointment appointment = new Appointment(customer, barber, appointmentDate, appointmentTime);
        appointmentManager.addAppointment(appointment);
        JOptionPane.showMessageDialog(this, "预约成功");
    }

    private void viewAppointments() {
        appointmentManager.displayAppointments();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new MainFrame().setVisible(true);
        });
    }
}

7.2 完善用户界面

继续完善其他功能的用户界面,增加查看排班和统计功能的实现。

8. 功能测试与优化

8.1 功能测试

编写单元测试以验证每个类和方法的功能是否正常。例如,测试理发师类和顾客类的getter和setter方法,确保它们能正确返回和设置属性值。

8.2 性能优化

检测可能的性能瓶颈,优化数据结构和算法。例如,对于预约的管理,可以考虑使用HashMap来加速查询操作。

9. 总结与展望

本文详细介绍了如何使用Java语言编写一个美发店理发师排班程序,包括理发师的管理、顾客的预约、排班管理和工作统计等功能。在实际应用中,该程序可以极大地提高美发店的管理效率。

未来的扩展方向包括:

  • 增加用户权限管理功能,确保系统安全性。
  • 实现数据持久化,将数据存储在数据库中,支持数据的持久化和更复杂的查询。
  • 提供更友好的用户界面,提升用户体验。
举报

相关推荐

0 条评论