?? studentmanager.txt
字號:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
/*
* Created on 2005-1-11
*/
/**
* @author 讓煒
* @since 1.0
*
* TODO 學(xué)生成績管理系統(tǒng)
* 通過學(xué)號查找,修改,刪除數(shù)據(jù)
*
*/
public class LittleProgram
{
static boolean isDelete = true;
static boolean isFind = true;
public static void main(String [] args)//主方法,程序從這里開始運行
throws IOException,NumberNotFoundException
{
int choice=-1;
do{
LittleProgram lp = new LittleProgram();
System.out.println();
System.out.println("\t####################################");
System.out.println();
System.out.println("\t\t Java學(xué)生成績管理系統(tǒng)1.1");
System.out.println("\t\t請用學(xué)號查找,修改,刪除數(shù)據(jù)");
System.out.println();
System.out.println("\t####################################\n");
System.out.print("1.增加數(shù)據(jù):\n"+
"2.查找數(shù)據(jù):\n"+
"3.刪除數(shù)據(jù):\n"+
"4.清除所有數(shù)據(jù):\n"+
"5.把數(shù)據(jù)全部打印到屏幕\n"+
"6.把成績按學(xué)號排序\n"+
"7.修改數(shù)據(jù)\n"+
"8.統(tǒng)計已記錄成績學(xué)生數(shù)\n"+
"9.關(guān)于作者\n"+
"0.退出程序.\n" +
"輸入:");
BufferedReader in = //從終
new BufferedReader( //端接
new InputStreamReader(System.in));//收數(shù)
String inputLine = in.readLine(); //字選
choice= Integer.valueOf(inputLine).intValue();//項;
switch(choice)
{
case 1: {//1.增加數(shù)據(jù)
String str = lp.inputData();
lp.addData(str);
System.out.println("增加數(shù)據(jù)成功.");
timeOut(1);
}break;
case 2: {//2.查找數(shù)據(jù)
long find = 0;
System.out.print("請輸入你要查找的學(xué)生學(xué)號:");
BufferedReader inn =
new BufferedReader(
new InputStreamReader(System.in));
String inputLi = inn.readLine();
find = Integer.valueOf(inputLi).longValue();
lp.findData(find);
timeOut(2);
}break;
case 3: {//3.刪除數(shù)據(jù)
long deleteNumber = 0;
System.out.print("請輸入你想刪除的同學(xué)的學(xué)號:");
BufferedReader bf =
new BufferedReader (
new InputStreamReader(System.in));
String inputL = bf.readLine();
deleteNumber = Integer.valueOf(inputL).longValue();
lp.deleteData(deleteNumber);
if(isDelete)
System.out.println("刪除數(shù)據(jù)成功!");
timeOut(1);
}break;
case 4: {
lp.clearData();//4.清除所有數(shù)據(jù)
timeOut(1);
}break;
case 5: {
print();//5.把數(shù)據(jù)全部打印到屏幕
timeOut(2);
}break;
case 6: {
lp.numSort();//6.把成績按學(xué)號排序
System.out.println("按照學(xué)號從小到大排序成功!\n"+
"排序后:\n");
print();
timeOut(2);
}break;
case 7: {
lp.rewrite();//7.修改數(shù)據(jù)
timeOut(2);
}break;
case 8: {
int count = lp.count();
System.out.println("共有"+count+"個學(xué)生已經(jīng)記錄.");
timeOut(2);
}break;
case 9: {
System.out.print("\t\t讓煒\n"+
"\t\t上海電力學(xué)院通信工程系\n"+
"\t\tQQ:254482170\n");
timeOut(4);
}break;
}while (choice != 0);
System.out.println("Bye! ^-^");
System.exit(0);
}
public String inputData()//從終端接收數(shù)據(jù)的方法,返回字符串
throws IOException,NumberFormatException
{
System.out.print("請依次輸入 :學(xué)號 姓名 性別 成績\n" +
"每項數(shù)據(jù)請用空格隔開:");
String all = "";
try{
BufferedReader in = //從終
new BufferedReader ( //端接
new InputStreamReader(System.in)); //收數(shù)
String inputLine = in.readLine(); //據(jù)
StringTokenizer str =
new StringTokenizer(inputLine," ");//接收的數(shù)據(jù)用空格隔開,這個類用來提取每個字符串
long num = Integer.valueOf(str.nextToken()).longValue();//學(xué)號
String name = (String)str.nextToken(); //姓名
String sex = (String)str.nextToken(); //性別
double mark = Integer.valueOf(str.nextToken()).doubleValue();//分?jǐn)?shù)
all = String.valueOf(num) +" , "+
name +" , "+
sex +" , "+
String.valueOf(mark);//把所有的數(shù)據(jù)用" , "隔開然后在連起來放進(jìn)字符串a(chǎn)ll
}catch (IOException e){}
catch (NumberFormatException e){}
return all;//返回字符串a(chǎn)ll
}
public void addData(String str)//增加數(shù)據(jù)的方法
throws IOException
{
String s1 ="",s2="" ,s3= "";
File file = new File("data.txt");
if (file.exists())//如果文件data.txt存在
{
try{
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s1=in.readLine())!=null)
s2+=s1+"\n";//把文件中的每行數(shù)據(jù)全部放進(jìn)一個字符串s2
s2+=str+"\n"; //再把s2于形參str相連放進(jìn)s2
BufferedReader in2 = //把字符
new BufferedReader( //串s2也
new StringReader(s2)); //就是原
PrintWriter out = //文件+
new PrintWriter( //形參str(新輸入的一行數(shù)據(jù))
new BufferedWriter( //重新寫進(jìn)data.txt
new FileWriter("data.txt"))); //覆蓋原來的數(shù)據(jù)
while ((s3=in2.readLine())!= null)
{
out.println(s3);
}
out.close();
//System.out.println("write data true.");
}catch (IOException e){}
}else{
System.err.println("File \"data\" Missing!");
}
}
public void clearData()//清除data.txt的所有數(shù)據(jù)的方法
throws IOException
{
File file = new File("data.txt");
if(file.exists())//如果文件在
{
try{
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter(file)));
out.print("");//在文件data.txt里寫進(jìn)一個空字符,所以清除了原來的內(nèi)容
out.close(); //關(guān)閉文件
System.out.println("clear data true!");
}catch(IOException e){}
}else{
System.err.println("File \"data\" Missing!");
}
}
public void deleteData(long deleteNumber)//刪除某條數(shù)據(jù)
throws IOException,FileNotFoundException
{
isDelete = true;
try{
DataMap mp = new DataMap();//生成一個自己編寫的容器
long j=0;
String s1="",s2="",s3="";
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s1=in.readLine())!=null)
{
j=numberTokenizer(s1);
mp.put(j,s1);
}
try{
if(mp.containsKey( String.valueOf(deleteNumber).toString()))
{
mp.remove(deleteNumber);
}else
throw new NumberNotFoundException();
Collection c = mp.values();
Iterator iter = c.iterator();
while(iter.hasNext())
{
s1 = (String)iter.next();
s3 +=s1+"\n";
}
BufferedReader in2 =
new BufferedReader(
new StringReader(s3));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new FileWriter("data.txt")));
//System.out.println("delete No"+deleteNumber);
while( (s1 = in2.readLine())!=null)
{
out.println(s1);
}
out.close();
}catch (NumberNotFoundException e)
{
isDelete = false;
System.out.println(deleteNumber+" no found :(");
}
}catch(IOException e){}
}
public long numberTokenizer(String s)
throws IOException
{
StringTokenizer st =
new StringTokenizer(s," ");
return Integer.valueOf((st.nextToken())).longValue();
}
public void findData(long find)//查找數(shù)據(jù)
throws IOException,NumberNotFoundException
{
isFind = true;
String s = "",findString ="";
long i;
DataMap dm = new DataMap();
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
//in.close();
try{
if(dm.containsKey( String.valueOf(find).toString()))
{
findString = dm.get(find);
System.out.println("學(xué)號"+find+"學(xué)生的資料是:");
System.out.println(findString);
}else
throw new NumberNotFoundException();
}catch (NumberNotFoundException e){
System.out.println(find+" no found :(");
isFind = false;
}
}
public static void print()//讀取文本文件把數(shù)據(jù)打印到終端的方法
throws IOException
{
try{
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
String read = "";
while ((read = in.readLine())!=null)
System.out.println(read);
}catch(IOException e){}
}
public static void timeOut(double sec)//停頓短暫時間的一個方法完全可以不要這個功能
{
double seconds = sec;
long t = System.currentTimeMillis()+(int)(seconds*1000);
while ((System.currentTimeMillis())<t)
;
}
public void numSort()//按學(xué)號排序
throws IOException
{
long i = 0;
String s = "";
try{
DataArrayList dal = new DataArrayList();
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
Collections.sort(dal);
DataMap dm = new DataMap();
BufferedReader in2 =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in2.readLine())!=null)
{
i=numberTokenizer(s);
dm.put(i,s);
}
PrintWriter out =
new PrintWriter (
new BufferedWriter(
new FileWriter("data.txt")));
Iterator it = dal.iterator();
long temp = 0;
String tempStr = "";
while (it.hasNext())
{
temp = Integer.valueOf((String)it.next()).longValue();
tempStr = dm.get(temp);
out.println(tempStr);
}
out.close();
}catch(IOException e){}
}
public void rewrite()
throws IOException,NumberNotFoundException
{
try{
System.out.print("請輸入你要修改的學(xué)生學(xué)號:");
BufferedReader in =
new BufferedReader (
new InputStreamReader(System.in));
String inputLine = in.readLine();
long num = Integer.valueOf(inputLine).longValue();
findData(num);
if(isFind)
{
deleteData(num);
System.out.print("請重新輸入該學(xué)生的資料:");
String str = inputData();
addData(str);
System.out.println("rewrite true!");
}
}catch(IOException e){}
catch(NumberNotFoundException e){}
}
public int count()
throws IOException
{
DataArrayList dal = new DataArrayList();
try{
String s = "";
long i =0;
BufferedReader in =
new BufferedReader(
new FileReader("data.txt"));
while ((s=in.readLine())!=null)
{
i=numberTokenizer(s);
dal.add(i);
}
}catch(IOException e){}
return dal.size();
}
}
/*
*
* @author RangWei
* TODO 這是個我們寫的一個容器,繼承公共類HashMap
* 大概的功能就相當(dāng)一個數(shù)組
*
*/
class DataMap extends HashMap//一個存儲數(shù)據(jù)的Map
{
public void put(long i,String str)//把學(xué)號和數(shù)據(jù)放進(jìn)這個Map
{ //以后一個學(xué)號(key)對應(yīng)的是一個人的數(shù)據(jù)(value)
put(String.valueOf(i).toString(),str);
}
public void remove(long i)//接收學(xué)號,然后刪除學(xué)號(key)和它對應(yīng)的數(shù)據(jù)(value)
{
remove(String.valueOf(i).toString().toString());
}
public String get(long i)//接收一個學(xué)號,然后返回這個key對應(yīng)的value
{
String s = String.valueOf(i).toString();
if (!containsKey(s))
{
System.err.println("Not found Key: "+s);
}
return (String)get(s);
}
}
/*
*
* @author RangWei
*
* TODO 這個類繼承ArrayList
* 用來按數(shù)字排序,在用學(xué)號排序時要用到它
*
*/
class DataArrayList extends ArrayList
{
public void add(long num)
{
String numToString = String.valueOf(num).toString();
add(numToString);
}
}
/*
*
* @author RangWei
*
* TODO 增加的一個Exception,主要是在文件里沒有要找
* 的學(xué)號就拋出
*
*/
class NumberNotFoundException extends Exception
{
public NumberNotFoundException()
{}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -