//获取系统时间的方法:Date date=new Date(new java.util.Date().getTime());
/*
* 考勤管理系统:实现数据在文件的读取功能;两个模块,功能如下:
* ----管理员模块:
* 1.注册新用户
* 2.查询所用用户
* ----用户模块:
* 1.上班打卡
* 2.下班打卡
* 3.查询历史打卡记录
* */
public class TestUser {
static Scanner input=new Scanner(System.in);
static File file=new File("userInfo.txt");
static List<User> list=new ArrayList<User>();
//管理员查询所有用户信息
public static void userInformation() throws IOException{
ArrayList<User> array=new ArrayList<User>();
array=read();
System.out.println("用户名 真实姓名");
for(int i=0;i<array.size();i++){
System.out.println(array.get(i).getUserName()+" "+array.get(i).getName());
}
System.out.println("-----------------------------------------------------------");
}
//管理员注册新用户
public static void registerUser() throws IOException{
User user=new User();
int n=5;
System.out.println("请输入要添加的用户的信息");
while(n>0){
System.out.println("请输入用户名:");
String userName=input.next();
System.out.println("请输入用户的真是姓名:");
String name=input.next();
System.out.println("请输入密码:");
String password=input.next();
System.out.println("请再次输入密码:");
String password1=input.next();
if(password.equals(password1)){
user.setUserName(userName);
user.setName(name);
user.setPassword(password);
user.setSign_in_first(null);
user.setSign_in_last(null);
list.add(user);
write();
System.out.println("用户信息添加成功!!!");
break;
}
else{
System.out.println("重新输入信息!!");
}
}
System.out.println("----------------------------------------------------------------");
}
//管理员模块
public static void manager() throws IOException{
int m=5;//输入密码的错误机会为5次
String str1="java";
boolean b=false;
while(m>0){
System.out.println("请输入管理员密码:");
String str=input.next();
if(str.equals(str1)){
System.out.println("登陆成功!");
System.out.println("-----------------------------------------------------------");
System.out.println("1-注册新用户信息");
System.out.println("2-查询所有用户信息");
System.out.println("3-添加信息完毕退出");
System.out.println("-----------------------------------------------------------");
System.out.println("请选择您需要的操作:");
int n=input.nextInt();
switch(n){
case 1:registerUser();break;
case 2:userInformation();break;
case 3: b=true;System.out.println("退出成功!");break;
default:b=true;System.out.println("输入错误,请重新操作!");
}
if(b!=false){
break;
}
}
else{
System.out.println("管理员密码错误");
m--;
System.out.println("您还有"+m+"此机会!!");
if(m<0){
break;
}
}
}
}
//用户模块
public static void userT() throws IOException{
ArrayList<User> array=new ArrayList<User>();
array=read();
User user=new User();
//设置日期格式 System.out.println(df.format(new Date()));
// new Date()为获取当前系统时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date(new java.util.Date().getTime());
int n=array.size();
int m=5;//五次机会输入密码和用户名
boolean d=false;
boolean b=false;
while(m>0){
System.out.println("请输入您的真实姓名:");
String name=input.next();
System.out.println("请输入您的密码:");
String password=input.next();
for(int i=0;i<array.size();i++){
if(array.get(i).getName().equals(name) && array.get(i).getPassword().equals(password)){
System.out.println("登陆成功!");
System.out.println(array.get(i).getName()+" 您好 ,今天是 "
+new Date(new java.util.Date().getTime()));
while(true){
System.out.println("---------------------------------------------------");
System.out.println("1-上班打卡 2-下班打卡 3.查询打卡记录 4-退出");
System.out.println("请选择您要操作的选项:");
int t=input.nextInt();
switch(t){
case 1://上班打卡记录
String time=df.format(date);
user.setSign_in_first(time);
user.setSign_in_last(null);
user.setName(name);
user.setUserName(null);
user.setPassword(password);
list.add(user);
write();
System.out.println("打卡成功,退出系统");
System.exit(0);
case 2://下边打卡记录
String time1=df.format(date);
user.setSign_in_last(time1);
user.setSign_in_first(null);
user.setName(name);
user.setUserName(null);
user.setPassword(password);
list.add(user);
write();
System.out.println("打卡成功,推出系统");
System.exit(0);
case 3://查询记录
System.out.println("类型 时间");
for(int j=0;j<array.size();j++){
if(array.get(j).getName().equals(name) && array.get(j).getPassword().equals(password)){
System.out.println(name+"打卡记录:");
System.out.println("上班: "+array.get(j).getSign_in_first());
System.out.println("下班: "+array.get(j).getSign_in_last());
}
}
break;
case 4:d=true;System.out.println("退出!!!");break;
default :System.out.println("您输入错误,请从新输入!!!");
}
if(d!=false){
break;
}
}
}
b=true;
if(d!=false){
break;
}
}
}
}
//将文件中数据被程序读取
public static ArrayList<User> read() throws IOException{
ArrayList<User> array=null;
ObjectInputStream ois=null;
try {
array=new ArrayList<User>();
ois=new ObjectInputStream(new FileInputStream(file));
array=(ArrayList<User>) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}finally {
ois.close();
}
return array;
}
//将程序中的数据写入文件的方法
public static void write() throws IOException{
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
oos.close();
}
}
public static void main(String[] args) throws IOException {
System.out.println("*************************************************************");
System.out.println("* 欢迎进入宏远考勤系统 *");
System.out.println("*************************************************************");
while(true){
System.out.println("请选择登陆类型:1-管理员登陆 2.用户登陆 0-退出系统 (输入完毕后按回车确定!!)");
int n=input.nextInt();
switch(n){
case 1:manager();break;
case 2:userT();
case 0:System.out.println("系统退出!!!");System.exit(0);
default :System.out.println("您输入错误,请从新输入!!!");
}
}
}
}
-------------------------------------------------------------------------------------
User类:
/*
*考勤管理系统的用户类:
* */
public class User implements Serializable{
private String userName;//用户名称
private String name;//真实姓名
private String password;
private String sign_in_first;//上班打卡
private String sign_in_last;//下班打卡
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String userName, String name, String password, String sign_in_first, String sign_in_last) {
super();
this.userName = userName;
this.name = name;
this.password = password;
this.sign_in_first = sign_in_first;
this.sign_in_last = sign_in_last;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSign_in_first() {
return sign_in_first;
}
public void setSign_in_first(String sign_in_first) {
this.sign_in_first = sign_in_first;
}
public String getSign_in_last() {
return sign_in_last;
}
public void setSign_in_last(String sign_in_last) {
this.sign_in_last = sign_in_last;
}
@Override
public String toString() {
return "User [userName=" + userName + ", name=" + name + ", password=" + password + ", sign_in_first="
+ sign_in_first + ", sign_in_last=" + sign_in_last + "]";
}
}
--------------------------------------------------------------------------------------------------------------------
另外需要建立一个文件userInfo.txt;