/**
* 程序功能:簡單的簽到程序,能夠保存簽到後的結果
* 作者:丁又專
* 時間:2014.03.02
* QQ:303727350
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
@SuppressWarnings("unused")
public class RegisterApp {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//(a)使用命令行參數,輸入學生名單,和 班級名稱
// 使用格式: java RegisterApp list.txt wl121
if(args.length != 2){
System.out.println("參數輸入不對");
System.out.println("使用方法(示例):java RegisterApp 名單文件名稱 班級名稱");
System.exit(0);
}
//(b)學生簽到結果:學生到,輸入1;缺課,輸入0
System.out.println("——————————————————");
System.out.println("簡易學生簽到程序V0.1");
System.out.println("老師叫到名字,請答‘到’");
System.out.println("1:到課 0:缺課");
System.out.println("——————————————————");
//(c)取得系統當前日期時間
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHMM");//可以方便地修改日期格式
String strDate = dateFormat.format( now );
System.out.println("當前時間:"+strDate);
//(d)讀取學生名單,args[0]爲學生名單文件,args[1]爲班級名稱
String fileList = args[0];
String fileCheck = args[1] + strDate + ".txt";
File fileInput = new File(fileList);
File fileOutput = new File(fileCheck);
//(e)利用Scanner類讀取文本數據/鍵盤輸入數據; PrintWriter類把簽到結果寫入到文件
Scanner input = new Scanner(fileInput);
Scanner sc = new Scanner(System.in);
PrintWriter output = new PrintWriter(fileOutput);
//保存缺課學生名字strAbsent ,缺課學生人數nAbsent, 是否缺課標記flag
String strAbsent = "";
int nAbsent = 0;
int flag = 0;
while(input.hasNext()){ //循環讀取學生數據
String strName=input.nextLine();
//把學生名字輸出到屏幕,從而進行點名。
//老師根據學生到課情況,輸入1-到課,0-缺課,保存到flag中
if(strName.equals("溫明玉")){ //過濾掉自己,使自己不被點名
flag=1;
}
else
{
System.out.println(strName);
flag = sc.nextInt();
}
//如果缺課,則記錄下缺課學生數目 與 名字
if(flag==0){
nAbsent = nAbsent+1;
strAbsent = strAbsent + " " + strName;
}
//把考勤結果寫入名單
output.print(strName);
output.print(" ");
output.println(flag);
}
//關閉I/O管道
sc.close();
output.close();
input.close();
System.out.println("——————————————————————————");
System.out.println("考勤結束.");
System.out.printf("一共有%d個同學缺課,分別是:%s\n",nAbsent,strAbsent);
System.out.println("——————————————————————————");
}
}