用Java设计一个考勤模拟程序Attendance,实现如下功能选择界面:

1--上班签到

2--下班签出

3--考勤信息查阅

4--退出

考勤程序运行后,提示用户输入上述功能选择,并验证用户输入的用户名和密码;用户信息保存在HashMap中。

如果是上班签到,记录签到信息,如果签到时间大于上午9时,则提示用户迟到,并记录该迟到信息到attendance+username.txt中(username指的是登录用户)。

如果是下班签出,记录签出信息,如果签出时间小于下午6时,则提示用户早退,并记录该早退信息到attendance+username.txt中(username指的是登录用户)。

如果用户选择考勤信息查询,则将attendance+username.txt中对应该用户的迟到早退信息查出并显示。

用户选择功能执行完,程序继续回到功能选择界面等待下一个用户进行操作。

李纯宇 22:36:58

1、完成课设前的准备工作:(1)安装JDK1.8;(2)配置环境变量;(3)安装开发工具Eclipse。

2、编写Map存储用户信息,包括用户名、密码和考勤信息。

3、编写用户登录验证方法login(如果密码错误允许重新输入直至输入正确为加分项,非必须完成)

3、编写新用户注册的方法reg(此项为加分项,非必须完成)

4、撰写相关部分的课设报告书

李纯宇 22:37:31

1、编写上班签到方法check_in()

记录签到信息,如果签到时间大于上午9时,则提示用户迟到,并记录该迟到信息到attendance+username.txt中(username指的是登录用户)。

李纯宇 22:37:43

1、下班签出方法check_out()

记录签出信息,如果签出时间小于下午6时,则提示用户早退,并记录该早退信息到attendance+username.txt中(username指的是登录用户)。

李纯宇 22:37:58

1、编写查询考勤信息方法display_record()

2、编写系统功能选择方法opera()并编写测试类Test

在main方法中创建Attendance类的对象,并调用相应的方法,

3、程序测试

Attendance.java
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Scanner;
public class Attendance {
Scanner scan = new Scanner(System.in);
static HashMap map = new HashMap<>();
private String username;
private String password;
static boolean circulation = true;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = df.format(new Date());
String time = format.substring(11, 13);
public Attendance() {
}
public Attendance(String username, String password) {
setUsername(username);
setPassword(password);
map.put(getUsername(),getPassword());
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void opera(){
while(true) {
System.out.println("-------------------------------------------------------");
System.out.println("请选择功能 1.(注册) 2.(登陆)");
System.out.println("-------------------------------------------------------");
int option = 0;
boolean b = true;
while (b){
try {
option = scan.nextInt();
b = false;
} catch (Exception e) {
System.out.println("输入错误,请重新输入!");
scan.nextLine();
}
}
switch (option) {
case 1:
reg();
circulation = true;
while (circulation) {
menu();
}
break;
case 2:
login();
circulation = true;
while (circulation) {
menu();
}
break;
default:
System.out.println("你的选择有误,请重新输入");
}
}
}
public void menu(){
System.out.println("-------------------------------------------------------");
System.out.println(" 1——上班签到");
System.out.println(" 2——下班签出");
System.out.println(" 3——考勤信息查阅");
System.out.println(" 4——退出");
System.out.println("-------------------------------------------------------");
int choice = 0;
boolean b = true;
while (b){
try {
choice = scan.nextInt();
b = false;
} catch (Exception e) {
System.out.println("输入错误,请重新输入!");
scan.nextLine();
}
}
switch(choice){
case 1:
check_in();
break;
case 2:
check_out();
break;
case 3:
display_record();
break;
case 4:
System.out.println(getUsername()+"用户已退出!");
circulation = false;
break;
default:
System.out.println("输入错误!");
}
}
public void login(){
boolean input = true;
while (input){
System.out.print("请输入用户名:");
setUsername(scan.next());
System.out.print("请输入密码:");
setPassword(scan.next());
if (map.containsKey(getUsername())){
if (map.get(getUsername()).equals(getPassword())){
input = false;
System.out.println(getUsername()+"用户,登录成功!");
}else {
System.out.println("密码错误!");
}
}else {
System.out.println("密码或用户名错误,请重新输入!");
}
}
}
public void reg(){
while(true){
System.out.println("请输入用户名:");
setUsername(scan.next());
if(map.get(getUsername())!=null){
//如果存在
System.out.println("该账号已经存在,请重新输入账号");
}else{
//不存在
break;
}
}
System.out.println("请输入密码:");
setPassword(scan.next());
//添加用户到map集合
map.put(getUsername(),getPassword());
System.out.println("注册成功!");
System.out.println("当前注册的人员:"+getUsername());
}
//上班签到
public void check_in(){
if (Integer.valueOf(time)>9){
System.out.println(getUsername()+"用户迟到,已经记录!");
//写入到attendance+username.txt文件
String saveFile = "attendance"+getUsername()+".txt";
String fileContent = format+" "+getUsername()+"迟到";
File file = new File(saveFile);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()){
fos = new FileOutputStream(file);
}else {
fos = new FileOutputStream(file,true);
}
osw = new OutputStreamWriter(fos,"utf-8");
osw.write(fileContent);
osw.write("\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
System.out.println("签到成功!");
}
System.out.println(format);
}
//下班签出
public void check_out(){
if (Integer.valueOf(time)<18){
System.out.println(getUsername()+"用户早退,已经记录!");
//写入到attendance+username.txt文件
String saveFile = "attendance"+getUsername()+".txt";
String fileContent = format+" "+getUsername()+"早退";
File file = new File(saveFile);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()){
fos = new FileOutputStream(file);
}else {
fos = new FileOutputStream(file,true);
}
osw = new OutputStreamWriter(fos,"utf-8");
osw.write(fileContent);
osw.write("\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
System.out.println("签出成功!");
}
System.out.println(format);
}
//查询考勤信息
public void display_record(){
String filePath = "attendance"+getUsername()+".txt";
try {
String encoding="utf-8";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}
Test.java
public class Test {
public static void main(String[] args) {
//新建默认用户
Attendance at = new Attendance("root","123");
at.opera();
}
}