/*
* Program Name:Student Information management program
* Fuctions:
* *sort all records by student numbers
* *combine the student name and graduate college with "-"
* *convert the graduate date from format"yyyymmdd" to "dd/mm/yyyy"
* *cut the student tutor name into two strings
* *convert the number from format "100000.00" to "100,000.00"
* Author:Sguy @ IBM Chengdu
* Version:1.0
* LastModified:2007/07/29
* All rights reserved!
*/
package mainFolder;
//Head files for sortByStuNum
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
//Head files for the Date format convert
import java.text.SimpleDateFormat;
import java.util.Date;
//Head files for the Number format convert
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.ParseException;
public class StuInforMan {
//The program starts here
public static void main(String args[])
throws IOException
{
String sourceDataFile = "E://sguy_dev//StuInforProcess//mainFolder//StuInfor.txt";
String targetDataFile = "E://sguy_dev//StuInforProcess//mainFolder//FinalOutputData.txt";
//Define an instance of the StuInorMan,and call the informationProcess method
StuInforMan stuInforMan = new StuInforMan();
stuInforMan.informationProcess(sourceDataFile, targetDataFile);
}
/*
* The Start of the block of records sorting
*/
//The method convert a String variable to long type
//from a record of the student data
public static long numberTokenizer(String s)
throws IOException
{
StringTokenizer st = new StringTokenizer(s,",");
return Integer.valueOf((st.nextToken())).longValue();
}
//The method Sort records by student number
public void informationProcess(String sourceDataFile,String bufferDataFile)
throws IOException
{
File sourceFile = new File(sourceDataFile);
File targetFile = new File(bufferDataFile);
/*
*Check the source file and the buffer's validation
*/
//confirm the source file's validation
if(!sourceFile.exists()) {
abort("no such source file: " + sourceDataFile);
}
if(!sourceFile.canRead()) {
abort("source file is unreadble: " + sourceDataFile);
}
//confirm the buffer file's validation
if(targetFile.exists())
{
if(!targetFile.canRead())
{
abort("destination file is unreadble: " + bufferDataFile);
}
// if the target file is overwritable?
System.out.println("Overwrite existing file " + targetFile.getName() + " ? (Y/N):");
System.out.flush();
//get user's response
BufferedReader userResponse = new BufferedReader(new InputStreamReader(System.in));
String response = userResponse.readLine();
if(!response.equalsIgnoreCase("y")) {
abort("existing file was not overwritable. ");
}
}
/*
*
*/
long stuNumber = 0;
String stuInfor = "";
try
{
DataArrayList dataArrayList = new DataArrayList();
FileReader fileReader = new FileReader(sourceFile);
FileWriter fileWriter = new FileWriter(targetFile);
BufferedReader bufferReader1 = new BufferedReader(fileReader);
while ((stuInfor=bufferReader1.readLine())!=null)
{
stuNumber=numberTokenizer(stuInfor);
dataArrayList.add(stuNumber); //add stuNumbers to the ArrayList
}
Collections.sort(dataArrayList); //call the sort method
DataMap dataMap = new DataMap();
BufferedReader bufferReader2 =
new BufferedReader(new FileReader(sourceDataFile));
String stuInfor2 = null;
while((stuInfor2=bufferReader2.readLine())!=null)
{
stuNumber=numberTokenizer(stuInfor2);
StuInforMan stuMan = new StuInforMan();
/*
* This is String and Date and number format control block
*/
StringTokenizer stringToken = new StringTokenizer(stuInfor2,",");
String studentNumber = stringToken.nextToken();
String studentName = stringToken.nextToken();
String graduateCollege = stringToken.nextToken();
String graduateTime = stringToken.nextToken();
String studentTutor = stringToken.nextToken();
String totalFees = stringToken.nextToken();
//combine the two Strings with "-" between them
String combinedString = studentName + "-" + graduateCollege;
//split the third string into two strings with " & " between them
//from the 5th char
String splitString = stuMan.splitString(studentTutor, 5);
//convert date format from "yyyymmdd" to "dd/mm/yyyy"
String processedTime = stuMan.convertDate(graduateTime);
//convert the number format from 10000.00 to 10,000.00
String processedFees = stuMan.convertNumber(totalFees);
//The final String written to the final output data text file
String processedInfor = studentNumber + ","
+ combinedString + ","
+ processedTime + ","
+ splitString + ","
+ processedFees;
//get the final stuNumber and data reflect
dataMap.putData(stuNumber,processedInfor);
}
PrintWriter printWriter = new PrintWriter ( new BufferedWriter(fileWriter));
Iterator it = dataArrayList.iterator();
long tempLong = 0;
String tempStr = "";
while (it.hasNext())
{
tempLong = Integer.valueOf((String)it.next()).longValue();
tempStr = dataMap.getData(tempLong);
printWriter.println(tempStr);
}
System.out.println("The SORTED data has written to the target file!");
printWriter.close();
}catch(IOException e){
System.out.println("The target file is unreachable!");
}
}
//Show the detailed IO exception information
private static void abort(String msg) throws IOException
{
throw new IOException("DataProcess: " + msg);
}
/*
* Author Sguy @ IBM Chengdu
* A Data Map that create a reflect with the student number
* and the related detail information
*/
class DataMap extends HashMap
{
public void putData(long stuNumber,String stuInfor)
{
put(String.valueOf(stuNumber).toString(),stuInfor);
//System.out.println("here is ok");
}
public String getData(long stuNumber)
{
String stuInfor = String.valueOf(stuNumber).toString();
if (!containsKey(stuInfor))
{
System.err.println("Not found Key: "+stuInfor);
}
return (String)get(stuInfor);
}
}
/*
* Author Sguy @ IBM Chengdu
* Sort by Student Number
*
*/
class DataArrayList extends ArrayList
{
public void add(long num)
{
String numToString = String.valueOf(num).toString();
add(numToString);
}
}
/*
* This is the end point of the sort By Stu Number block
*/
/*
* This is the start point of String Split
*/
public String splitString(String toBeSplit,int splitPosition)
{
//The length of the String is less than the splitPosition
if(toBeSplit.length()<splitPosition)
{
System.out.print("The length of the String is less than the splitPosition:"+splitPosition);
return toBeSplit;
}
else
{
byte bt[]=toBeSplit.getBytes();
if(splitPosition > 1) //Not just get the first char
{
if(bt[splitPosition]<0) //if it's the chinese or japanese chracter
{
String substrx1 = new String(bt,0,--splitPosition);
int i = splitPosition/2;
String substrx2 = new String(bt,splitPosition,(toBeSplit.length() - i) * 2);
String subStrs = substrx1 + " & " + substrx2;
return subStrs;
}
else
{
//The perfect condition,return the devided String
String substrex1=new String(bt,0,splitPosition);
String substrex2=new String(bt,splitPosition,toBeSplit.length() - splitPosition);
String subStrs = substrex1 + " & " + substrex2;
return subStrs;
}
}
else
{
if(splitPosition==1)
{
if(bt[splitPosition]<0)
{
//just cut one chinese or japanese charater
String substr1 = new String(bt,0,++splitPosition);
String substr2 = new String(bt,splitPosition,(toBeSplit.length()-1)*2);
System.out.println(toBeSplit.length());
String subStrs = substr1 + " & " + substr2;
return subStrs;
}
else
{
//Just cut the first one char down,and return it
String subStr1 = new String(bt,0,splitPosition);
String subStr2 = new String(bt,splitPosition,toBeSplit.length()-splitPosition);
String subStrs = subStr1 + " & " + subStr2;
return subStrs;
}
}
else
{
System.out.println("The splitPosition must be an integer larger or equal than 1");
return toBeSplit;
}
}
}
} //Here is the end of the String Split method
/*
* Here is the The start point of the Date format convert method
*/
public String convertDate(String toConvertDate)
{
//Create a date formatter that can parse dates of
//the format yyyymmdd.
SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyymmdd");
//the form DD-MM-YYYY
SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd/mm/yyyy");
//Create a string containing a text date to be parsed.
String dateParsedString = null;
try
{
//Parse the text version of the date.
Date date = inputDateFormat.parse(toConvertDate);
//Now convert the date format from the input format to the
//output format
dateParsedString = outputDateFormat.format(date);
return dateParsedString;
}catch (Exception ex)
{
System.out.println("The text format can not convert to a Date format,please check the data file");
System.out.println("Here is the detail information about the exception:");
System.out.println(ex.getMessage());
return null;
}
} //Here is the end point of the date format convert method
public String convertNumber(String toConvertNumber)
{
Object object = null;
DecimalFormat decimalFormat = null;
//get a NumberFormat object and convert it to a DecimalFormat Object
try
{
decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.US);
object = decimalFormat.parse(toConvertNumber);
}
catch (ClassCastException e)
{
System.err.println(e);
}
catch (ParseException e2)
{
System.out.println("Please check the data file,mack sure you are processing a number!");
System.out.println("Here is the detail information about this exception:");
System.err.println(e2);
}
// Define the number format
decimalFormat.applyPattern("#,###.000000");
// format a number
return decimalFormat.format(object);
}
}
该系统能够对学生信息进行排序、格式转换等处理,包括按学号排序、姓名与学院组合、毕业日期格式转换、导师姓名分割及费用格式化。
970





