import java.io.*;
import java.util.*;
/*
* Created on 2008-9
*/
/**
* @author 叶润华
* TODO 学生成绩分析程序
* 通过学号查找,修改,删除数据
*
*/
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学生成绩管理系统1.1");
System.out.println("\t\t请用学号查找,修改,删除数据");
System.out.println();
System.out.println("\t####################################\n");
System.out.print("1.增加数据:\n"+
"2.查找数据:\n"+
"3.删除数据:\n"+
"4.清除所有数据:\n"+
"5.把数据全部打印到屏幕\n"+
"6.把成绩按学号排序\n"+
"7.修改数据\n"+
"8.统计已记录成绩学生数\n"+
"9.关于作者\n"+
"0.退出程序.\n" +
"输入:");
BufferedReader in = //从终
new BufferedReader( //端接
new InputStreamReader(System.in));//收数
String inputLine = in.readLine(); //字选
choice= Integer.valueOf(inputLine).intValue();//项;
switch(choice)
{
case 1: {//1.增加数据
String str = lp.inputData();
lp.addData(str);
System.out.println("增加数据成功.");
timeOut(1);
}break;
case 2: {//2.查找数据
long find = 0;
System.out.print("请输入你要查找的学生学号:");
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.删除数据
long deleteNumber = 0;
System.out.print("请输入你想删除的同学的学号:");
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("删除数据成功!");
timeOut(1);
}break;
case 4: {
lp.clearData();//4.清除所有数据
timeOut(1);
}break;
case 5: {
print();//5.把数据全部打印到屏幕
timeOut(2);
}break;
case 6: {
lp.numSort();//6.把成绩按学号排序
System.out.println("按照学号从小到大排序成功!\n"+"排序后:\n");
print();
timeOut(2);
}break;
case 7: {
lp.rewrite();//7.修改数据
timeOut(2);
}break;
case 8: {
int count = lp.count();
System.out.println("共有"+count+"个学生已经记录.");
timeOut(2);
}break;
case 9: {
System.out.print("\t\t让炜\n"+
"\t\t上海电力学院通信工程系\n"+
"\t\tQQ:254482170\n");
timeOut(4);
}break;
}while (choice != 0);
System.out.println("Bye! ^-^");
System.exit(0);
}
public String inputData()//从终端接收数据的方法,返回字符串
throws IOException.NumberFormatException
{
System.out.print("请依次输入 :学号 姓名 性别 成绩\n" +
"每项数据请用空格隔开:");
String all = "";
try{
BufferedReader in = //从终
new BufferedReader ( //端接
new InputStreamReader(System.in)); //收数
String inputLine = in.readLine(); //据
StringTokenizer str =
new StringTokenizer(inputLine," ");//接收的数据用空格隔开,这个类用来提取每个字符串
long num = Integer.valueOf(str.nextToken()).longValue();//学号
String name = (String)str.nextToken(); //姓名
String sex = (String)str.nextToken(); //性别
double mark = Integer.valueOf(str.nextToken()).doubleValue();//分数
all = String.valueOf(num) +" , "+
name +" , "+
sex +" , "+
String.valueOf(mark);//把所有的数据用" , "隔开然后在连起来放进字符串all
}catch (IOException e){}
catch (NumberFormatException e){}
return all;//返回字符串all
}
public void addData(String str)//增加数据的方法
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";//把文件中的每行数据全部放进一个字符串s2
s2+=str+"\n"; //再把s2于形参str相连放进s2
BufferedReader in2 = //把字符
new BufferedReader( //串s2也
new StringReader(s2)); //就是原
PrintWriter out = //文件+
new PrintWriter( //形参str(新输入的一行数据)
new BufferedWriter( //重新写进data.txt
new FileWriter("data.txt"))); //覆盖原来的数据
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的所有数据的方法
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里写进一个空字符,所以清除了原来的内容
out.close(); //关闭文件
System.out.println("clear data true!");
}catch(IOException e){}
}else{
System.err.println("File \"data\" Missing!");
}
}
public void deleteData(long deleteNumber)//删除某条数据
throws IOException,FileNotFoundException
{
isDelete = true;
try{
DataMap mp = new DataMap();//生成一个自己编写的容器
long j=0;
String s1="",s2="",s3="";
评论0