编程打卡:面向对象程序设计

import java.util.*;

public class StaffManagementSystem {

    private static List<Staff> staffList = new ArrayList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("Welcome to the Staff Management System. What would you like to do?");
            System.out.println("1. Add a staff member");
            System.out.println("2. Remove a staff member");
            System.out.println("3. List all staff members");
            System.out.println("4. Quit");

            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    addStaffMember(scanner);
                    break;
                case 2:
                    removeStaffMember(scanner);
                    break;
                case 3:
                    listAllStaffMembers();
                    break;
                case 4:
                    System.out.println("Goodbye!");
                    System.exit(0);
            }
        }
    }

    private static void addStaffMember(Scanner scanner) {
        System.out.println("Enter the staff member's name:");
        String name = scanner.nextLine();

        System.out.println("Enter the staff member's department:");
        String department = scanner.nextLine();

        System.out.println("Enter the staff member's job title:");
        String jobTitle = scanner.nextLine();

        staffList.add(new Staff(name, department, jobTitle));
        System.out.println("Staff member added successfully!");
    }

    private static void removeStaffMember(Scanner scanner) {
        System.out.println("Enter the staff member's name:");
        String name = scanner.nextLine();

        Staff staffToRemove = null;
        for (Staff staff : staffList) {
            if (staff.getName().equals(name)) {
                staffToRemove = staff;
            }
        }

        if (staffToRemove == null) {
            System.out.println("Staff member not found!");
        } else {
            staffList.remove(staffToRemove);
            System.out.println("Staff member removed successfully!");
        }
    }

    private static void listAllStaffMembers() {
        for (Staff staff : staffList) {
            System.out.println(staff);
        }
    }
}

class Staff {

    private String name;
    private String department;
    private String jobTitle;

    public Staff(String name, String department, String jobTitle) {
        this.name = name;
        this.department = department;
        this.jobTitle = jobTitle;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", department='" + department + '\'' +
                ", jobTitle='" + jobTitle + '\'' +
                '}';
    }
}

posted @ 2023-05-24 19:18  satou_matsuzaka  阅读(7)  评论(0编辑  收藏  举报

This is a Test

メイドノココロハ アヤツリドール