Attendence Taking program design

Attendence Taking program design

Background

Mr.Lartuno give us the project that we need to design a Attendence software for him. (Because he is lazy to finish it himself) So here is how it is designed.

Design the structure

Use FileIO to help me get the students list save and passward saving (Mr.Lartuno is so lazy that he wants students to input their information to finish attendence themselves). And use regular expression to help me find out if the password is complicating enough (Make our work complecate enough).

Start the project

FileIO guide

  • Import settings
1
2
import java.io.*;
import java.util.*;
  • Get Input
1
2
File filename = new File("/Users/maverick/Desktop/1.txt");//Asolute path to the file you are going to open
Scanner s=new Scanner(filename);
  • Print output
1
2
3
FileOutputStream fout = new FileOutputStream("a", true);
PrintWriter fou = new PrintWriter(fout);
fou.println("datas");
  • Other important staffs
1
2
3
4
public static void main(String[] args)throws FileNotFoundException{//Judge if error accur
//code segments.....
fou.close();//Remember to close the printwriter after
}

Regular expression guide

  • Import settings
1
2
3
4
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//Or only one
import java.util.regex.*;
  • Use example #1 (Find the last number in a sequence)
1
2
3
4
5
String s = "A876X";//Whatever sequence
Pattern pattern = Pattern.compile("(\\d)[^\\d]*$");//(\\d):a number,[^\\d]*:letters which are equal to or more than 0,$:End leter
Matcher matcher = pattern.matcher(s);//Find the right string based on the regular expression
if(matcher.find())//if we find the one
System.out.println(matcher.group(1));//print out the number or (\\d)
  • The use of Mathcer.start()/ Matcher.end()/ Matcher.group()
1
2
3
4
5
6
Pattern p=Pattern.compile(“\d+”);
Matcher m=p.matcher(“aaa2223bb”);
m.find();//matching2223 (matches(),lookingAt(),find() has the same function)
m.start();//return 3 (the poistion that start) Attention!!!: Sequence's first letter's position is 0
m.end();//return 7 (the poistion that end)
m.group();//return 2233 (The letters u were tring to find)
  • Useful Regular expression in different situation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 //1 Email:
^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
//2 Website:
[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?
//3 InternetURL:
[a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
// 4 phone number:
^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$//or
^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
//5 Account(Start with letter,4-15bytes,Allow letters,numbers,underline):
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
//6 Weak password(Start with letter,5-17bytes,Allow letters,numbers,underline):
^[a-zA-Z]\w{5,17}$
//7 Strong password(Start with letter,8-10bytes,Allow letters,numbers,underline):
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
//8 date:
^\d{4}-\d{1,2}-\d{1,2}
//9 Chinese letter
[\u4e00-\u9fa5]

Break the whole program into small functions add together

I will explain the details that will easily cause bugs in my code, so don’t worry.

  • At first we should design the main function
1
2
3
4
5
public static void main(String[] args)throws FileNotFoundException { 
initialize();
welcomepage();
Attendence();
}
  • Then we need to Initialize the settings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public static void initialize()throws FileNotFoundException{
if(!dir.exists()){//Make folder to put attendence file
dir.mkdir();
}
getfilename();//Get the attendence file name (Date)
for(int i=0;i<50;i++)
for(int j=0;j<10;j++){
students[i][j]="Absent";//Initialize the students array to Abesent, so that later we can use it
}
Scanner s=new Scanner(fin);
while(s.hasNextLine()){
String str=s.nextLine();
String[] m;
m=str.split(" ");
cnt++;
for(String ch : m){
if(ch!=null&&students[cnt][x[cnt]]!=null){//Bug#1
x[cnt]++;students[cnt][x[cnt]]=ch;
}
else
continue;
}
}
for(int i=1;i<cnt;i++){
if(x[i]==4){//No password
System.out.println("System detect that"+students[i][2]+"are using this for the first time");
System.out.println("Please go to "+"\""+fin.getAbsolutePath()+"\""+"to set up passwords");
System.out.println("Password should contain biggercase and smallcase and numbers and were 8 to 10 bytes long");
System.exit(0);
}
else if(x[i]==5){//have password
//Then use the regular expression to find out if password met the request
Pattern pattern = Pattern.compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$");
Matcher matcher = pattern.matcher(students[i][5]);
if(!matcher.find()){
System.out.println("System detect that "+students[i][2]+"'s password is set wrong");
System.out.println("Please go to "+"\""+fin.getAbsolutePath()+"\""+"to reset passwords");
System.out.println("Password should contain at least one bigger case and one lower case and numbers");
System.exit(0);
}
}
}
}
  • Welcome page
1
2
3
4
5
6
7
8
public static void welcomepage(){
System.out.println(filename+" Attendence Taking");
System.out.println("Please input Your name(First name, Capitalize) to take attendence");
System.out.println("Here is the students' names and their number");
System.out.println("If you want to add or delete students");
System.out.println("Please go to "+"\""+fin.getAbsolutePath()+"\""+"to make changes");
System.out.println("If you want to add or delete students");
}
  • Get date
1
2
3
4
5
6
7
8
public static void getfilename(){
Calendar cal=Calendar.getInstance();//Get from internet this method
int y=cal.get(Calendar.YEAR);
int m=cal.get(Calendar.MONTH)+1;
int d=cal.get(Calendar.DATE);
filename = String.valueOf(y)+"."+String.valueOf(m)+"."+String.valueOf(d);
//System.out.print(filename);
}
  • Take Attendance
1
2
3
4
5
6
7
8
public static void Attendence() throws FileNotFoundException{
Scanner s2=new Scanner(System.in);
String a="X",b;
while(a!="E"){//"E"is the end code
a=getname();
b=getpassword();
}
}

Functions inside Attendence()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
		public static String getpassword(){//Only show the getpassword()Because getname works the same way
Boolean h=true;
String a="H";
int m=-1,x=0;
while(h){
System.out.println("Please input your password: ");
a=s.nextLine();
for(int i=1;i<=cnt;i++){
// System.out.println(students[i][5]);
if(a.equals(students[i][5])){//Java's comparation is different from C++
m=1;
students[i][6]="Attended";
break;//This break only break the inside for()loop
}
if(a.equals("E")){//End
System.exit(0);//No need to continue
}
}
if(m==1){//Break the while loop
break;
}
else{
x++;
System.out.println("Please input the right password!");
System.out.println("U have "+(3-x)+" chances left to put the password right");
if(3-x==0){
System.out.println("U failed to input your password!");
System.exit(0);
}
}
}
return a;
}

From here we have done most about the program! But we still need to:

Rewiew the bugs we met!

#Bug1

1
2
3
4
5
6
7
for(String ch : m){
if(ch!=null&&students[cnt][x[cnt]]!=null){//Bug#1
x[cnt]++;students[cnt][x[cnt]]=ch;
}
else
continue;
}

When we declare new variables, we need to be careful wether it is declared or not. Because we can’t put null value to a variable. That’s a very common bug called:Null Pointer Exception (java.lang.NullPointerException).

You can go to StackOverFlow to have a look.

Now we are done

Although there can be more useful functions in it, but I chose to stop (Because I am lazy as Mr. Lartuno).

Here is some joke about today’s lesson:

“I don’t care about warning, I only care about error!”

Welcome to my other publishing channels