用Java写出一个学生请假管理系统
时间: 2023-09-09 19:13:05 浏览: 91
好的,以下是一个简单的学生请假管理系统的Java代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class LeaveManagementSystem {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("1. 添加学生");
System.out.println("2. 查看所有学生");
System.out.println("3. 请假");
System.out.println("4. 退出");
System.out.print("请输入你的选择: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
viewAllStudents(students);
break;
case 3:
takeLeave(students);
break;
case 4:
System.out.println("谢谢使用!");
break;
default:
System.out.println("无效的选择,请重新输入!");
break;
}
} while (choice != 4);
}
private static void addStudent(ArrayList<Student> students) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生姓名: ");
String name = scanner.nextLine();
Student student = new Student(name);
students.add(student);
System.out.println("添加成功!");
}
private static void viewAllStudents(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("当前没有学生!");
return;
}
for (int i = 0; i < students.size(); i++) {
System.out.println(i + 1 + ". " + students.get(i).getName());
}
}
private static void takeLeave(ArrayList<Student> students) {
Scanner scanner = new Scanner(System.in);
viewAllStudents(students);
System.out.print("请选择请假的学生编号: ");
int index = scanner.nextInt();
if (index < 1 || index > students.size()) {
System.out.println("无效的学生编号!");
return;
}
Student student = students.get(index - 1);
System.out.print("请输入请假天数: ");
int days = scanner.nextInt();
if (days <= 0) {
System.out.println("无效的请假天数!");
return;
}
student.takeLeave(days);
System.out.println(student.getName() + "请假成功!");
}
}
class Student {
private String name;
private int leaveDays;
public Student(String name) {
this.name = name;
this.leaveDays = 0;
}
public String getName() {
return name;
}
public void takeLeave(int days) {
this.leaveDays += days;
}
public int getLeaveDays() {
return leaveDays;
}
}
```
该系统包含以下基本功能:
1. 添加学生
2. 查看所有学生
3. 请假
4. 退出
学生类包含姓名和已请假天数两个属性,以及请假方法。主程序中使用ArrayList来存储所有学生的信息,用户可以通过选择菜单来进行相应的操作。
阅读全文