第三周作业

该文章描述了一个使用Java编写的签到、签退及查看签到表的系统。系统中包含了异常处理机制,防止重复签到和签退,使用LocalTime记录时间并以hh:mm:ss格式显示。用户通过控制台输入进行操作,如签到、签退和查看已签到人员的信息。
摘要由CSDN通过智能技术生成

第三周作业

要求:

  1. 写一个签到签退并且可以查看签到签退功能的项目
  2. 时间格式为 hh:mm:ss
  3. 要用到容器

思路如下:

确定异常

由于可能存在重复签到签退等情况,所以定义了一个输入异常:

public class InputsException extends Exception{
    public InputsException (String message){
        super(message);
    }
}

定义一个 人 的类

要考虑到三个特征:姓名,签到时间,签退时间

public class WorkPeople {
    //private LocalTime time;
    private String name = null;
    private String signIn = null;
    private String signOut = null;

    public WorkPeople(String name){
        this.name = name;
    }
    public WorkPeople(){

    }
    public String getName() {
        return name;
    }


    public void setSignIn(){
        LocalTime time = LocalTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss");
        this.signIn = dateTimeFormatter.format(time);
    }
    public String getSignIn(){
        return this.signIn;
    }
    public void setSignOut(){
        LocalTime time = LocalTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss");
        this.signOut = dateTimeFormatter.format(time);
    }
    public String getSignOut(){
        return this.signOut;
    }
}

考虑三个类,分别是“签入”“签出”“查看登记表”

签入

public class SignIn_0 {

    public static void signin( ArrayList<WorkPeople> people) throws InputsException{
        System.out.println("请输入你的姓名");
        
        //键盘录入
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        
        //检查是否有签到重复
        for(int i = 0;i < people.size();i++){
            if(name.equals(people.get(i).getName())){
                //签到重复的情况
                throw new InputsException("你已经签到过啦");
            }
        }
        
        //把签到的人塞入容器之中
        WorkPeople workPeople = new WorkPeople(name);
        workPeople.setSignIn();
        people.add(workPeople);
    }

}

签出

public class SignOut_1 {
    public static void signout(ArrayList<WorkPeople> people) throws InputsException{
        int index;
        System.out.println("请输入你的姓名");
        
        //键盘录入
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        
        //检查是否在签到表之中
        for( index = 0;index < people.size();index++){
            if(name.equals(people.get(index).getName())){
               break;
            }
        }
        
        if(index== people.size()){
            //还没签到
                throw new InputsException("你还没有签到哦");
        }
        
        if(people.get(index).getSignOut()!=null){
            //签出重复
            throw new InputsException("你已经签退过啦");
        }
        
        //录入签出情况
        else{
            people.get(index).setSignOut();
        }
    }
}

查看签到表

public class CheckOut_2 {
        public static void check(ArrayList<WorkPeople> people){
            //考虑无人签到的情况
            if(people.size()==0){
                System.out.println("还没人签到捏");
            }
            else{
                for(int i = 0;i<people.size();i++){
                    
                    //如果签到没有签出
                    if(people.get(i).getSignOut()==null){
                        System.out.println(people.get(i).getName()+"在 "+people.get(i).getSignIn()+" 时候签到,还没签出呢");
                    }
                    
                    //既签到也签出了
                    else{
                        System.out.println(people.get(i).getName()+"在 "+people.get(i).getSignIn()+" 时候签到,"+people.get(i).getSignOut()+" 时候签出");
                    }
                }
            }
        }
}

编写总服务,实现处理异常,指向功能

public class FunctionAll {

        public static void function(int service,ArrayList<WorkPeople> people){
            try{
                switch (service){
                    case 0 -> {
                       SignIn_0.signin(people);
                    }
                    case 1 -> {
                        SignOut_1.signout(people);
                    }
                    case 2 -> {
                        CheckOut_2.check(people);
                    }
                    case 3 -> {
                    	//结束服务
                        System.exit(0);
                    }
                    default -> {
                        //输入了没有出现的服务
                        throw new InputsException("没有此项服务,请重新输入");
                    }
                }
            }catch(InputsException e){
                System.out.println("出现异常啦,"+e.getMessage());
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
}

测试类

public class Test {
    public static void main(String[] args) {
        ArrayList<WorkPeople> people = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("签到请按0; 签出请按1; 查看签到表请按2; 结束服务请按3");
            System.out.println("请输入你选择的服务");
            int a = scanner.nextInt();
            FunctionAll.function(a,people);
        }
    }
}

测试结果

正常结果
在这里插入图片描述
异常测试
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值