第三周作业
要求:
- 写一个签到,签退并且可以查看签到签退功能的项目
- 时间格式为 hh:mm:ss
- 要用到容器
思路如下:
确定异常
由于可能存在重复签到签退等情况,所以定义了一个输入异常:
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);
}
}
}
测试结果
正常结果
异常测试