请假系统学生端的Java代码
时间: 2023-07-27 07:05:59 浏览: 161
以下是一个简单的请假系统学生端的Java代码示例,包含登录、请假和查看请假记录的功能:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentLeaveSystem {
private static List<Student> studentList = new ArrayList<>();
public static void main(String[] args) {
Student student = login();
if (student != null) {
System.out.println("欢迎使用请假系统," + student.getName() + "同学!");
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 请假");
System.out.println("2. 查看请假记录");
System.out.println("3. 退出");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();
switch (choice) {
case "1":
applyForLeave(student);
break;
case "2":
viewLeaveRecords(student);
break;
case "3":
System.out.println("再见," + student.getName() + "同学!");
return;
default:
System.out.println("请选择正确的功能!");
break;
}
}
} else {
System.out.println("登录失败!");
}
}
private static Student login() {
System.out.println("请输入学号:");
Scanner scanner = new Scanner(System.in);
String id = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
for (Student student : studentList) {
if (student.getId().equals(id) && student.getPassword().equals(password)) {
return student;
}
}
return null;
}
private static void applyForLeave(Student student) {
System.out.println("请输入请假开始时间:");
Scanner scanner = new Scanner(System.in);
String startTime = scanner.nextLine();
System.out.println("请输入请假结束时间:");
String endTime = scanner.nextLine();
System.out.println("请输入请假原因:");
String reason = scanner.nextLine();
LeaveRecord leaveRecord = new LeaveRecord(startTime, endTime, reason);
student.getLeaveRecords().add(leaveRecord);
System.out.println("请假申请已提交!");
}
private static void viewLeaveRecords(Student student) {
System.out.println("请假记录如下:");
List<LeaveRecord> leaveRecords = student.getLeaveRecords();
for (LeaveRecord leaveRecord : leaveRecords) {
System.out.println("开始时间:" + leaveRecord.getStartTime());
System.out.println("结束时间:" + leaveRecord.getEndTime());
System.out.println("请假原因:" + leaveRecord.getReason());
System.out.println("审批状态:" + leaveRecord.getStatus());
System.out.println("--------------------");
}
}
}
class Student {
private String id;
private String password;
private String name;
private List<LeaveRecord> leaveRecords = new ArrayList<>();
public Student(String id, String password, String name) {
this.id = id;
this.password = password;
this.name = name;
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
public List<LeaveRecord> getLeaveRecords() {
return leaveRecords;
}
}
class LeaveRecord {
private String startTime;
private String endTime;
private String reason;
private String status = "待审批";
public LeaveRecord(String startTime, String endTime, String reason) {
this.startTime = startTime;
this.endTime = endTime;
this.reason = reason;
}
public String getStartTime() {
return startTime;
}
public String getEndTime() {
return endTime;
}
public String getReason() {
return reason;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
```
这段代码实现了一个简单的请假系统学生端,包含登录、请假和查看请假记录的功能。在这个示例中,学生信息和请假记录都是存储在内存中的,实际的请假系统需要将数据存储在数据库或文件中以便持久化。同时,还需要对输入进行验证和过滤,确保系统的安全性。
阅读全文