對於學生成績的增刪查改。

來源於《java開發實戰經典》第12章課後習題5.

要求輸入學生成績,格式爲,姓名:成績|姓名:成績|姓名:成績|姓名:成績,輸入多個人的成績需要以|線分隔開,查詢的結果按照成績高低排列。可以增加,刪除,修改,查詢數據。


//下面這個是引用類InputData

import java.io.*;

public class InputData {
private BufferedReader buf=null;
public InputData(){
this.buf=new BufferedReader(new InputStreamReader(System.in));
}
public String getString(String info) {
String temp=null;
System.out.println(info);
try {
temp=this.buf.readLine();
}catch(IOException e) {
e.printStackTrace();
}
return temp;
}
public int getInt(String info,String err) {
int temp=0;
String str=null;
boolean flag=true;
while(flag) {
str=this.getString(info);
if(str.matches("^\\d+$")) {
temp=Integer.parseInt(str);
flag=false;
}else {
System.out.println(err);;
}
}
return temp;
}
public String getStudents(String info,String err) {
String temp=null;
String str=null;
boolean flag=true;

while(flag) {
str=this.getString(info);
String s[] = str.split("\\|");
int a=0;
for(int i=0;i<s.length;i++) {
if (s[i].matches("^\\S+:\\d{1,3}.?\\d?$")) {
a++;

}
}
if(a==s.length){
temp=str;
flag=false;


}else {
System.out.println(err);
}
}
return temp;


}
public float getFloat(String info,String err) {
float temp=0;
String str=null;
boolean flag=true;
while(flag) {
str=this.getString(info);
if(str.matches("^\\d+.?\\d+$")){
temp=Float.parseFloat(str);
flag=false;

}else {
System.out.println(err);
}
}
return temp;


}

}

//下面這個是引用類Operate,裏面有很多操作方法

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;


public class Operate {


static File file = new File("D:" + File.separator + "ZCA" + File.separator + "Java" + File.separator
+ "AndrewPackage" + File.separator + "TheScoresOfStudent.txt");


public boolean save(String strs) throws Exception {
boolean flag = false;
Writer out = null;


out = new FileWriter(file, true);// 向文件中追加內容
if (this.load() != null) {
out.write("|" + strs);
} else {
out.write(strs);
}
out.close();


return flag;
}


public boolean deleteAll() throws Exception {
Writer out = null;
out = new FileWriter(file);
String strd = "";// 原來用null代替經常報錯!
out.write(strd);
out.close();
return true;


}


public static void delete() throws Exception {
new Operate().deleteAll();
if (new Operate().load() == null) {
System.out.println("數據全部刪除成功!");
}


}


public String load() throws Exception {
String strl = null;
BufferedReader bufl = null;
Reader readerl = null;
readerl = new InputStreamReader(new FileInputStream(file));
bufl = new BufferedReader(readerl);
strl = bufl.readLine();
bufl.close();
return strl;
}


public static void update() throws Exception {
String strs = new Operate().load();
boolean flag = false;
String note = "請在下面輸入要修改成績學生的姓名:";
while (flag == false) {
InputData input = new InputData();


String name = input.getString(note);
String strs1 = strs.replaceAll(":", "\\|");
String s[] = strs1.split("\\|");
String stru1 = null;
for (int i = 0; i < s.length; i++) {


if (s[i].equals(name)) {
flag = true;
System.out.println("要修改的學生成績:" + s[i] + ":" + s[i + 1]);
InputData input1 = new InputData();
float score = input1.getFloat("請輸入此學生修改後的分數:", "格式不正確請重新輸入!");


stru1 = strs.replaceAll(s[i] + ":" + s[i + 1], s[i] + ":" + score);


System.out.println("數據修改成功!");
Operate.delete();
new Operate().save(stru1);
break;
}
if (i == s.length - 1) {
note = "你要找的學生不存在,請重新輸入!";
}


}
}


}


public static void search() throws Exception {
String strs = new Operate().load();// 將讀取的文件放入屬性中
InputData inputs = new InputData();// 這是個讀取鍵盤輸入的方法


String name = inputs.getString("請輸入姓名:");
String[] ss2 = new String[2];
int as = 0;
String ss[] = strs.split("\\|");
for (int i = 0; i < ss.length; i++) {
ss2 = ss[i].split(":");
as++;
if (ss2[0].equals(name)) {
System.out.println("查找學生的成績:" + ss2[0] + ":" + ss2[1]);
System.out.println("數據查找成功!");
break;
}
}
if (as == ss.length) {
System.out.println("此學生數據不存在!");
}
}


public static void deleteOne() throws Exception {
String strs = new Operate().load();
InputData input = new InputData();


String name = input.getString("請輸入要刪除成績學生的姓名:");
String strs1 = strs.replaceAll(":", "\\|");
String s[] = strs1.split("\\|");
String strsz = null;
for (int i = 0; i < s.length; i++) {


if (s[i].equals(name)) {
System.out.println("要刪除學生的成績:" + s[i] + ":" + s[i + 1]);


strsz = strs.replaceAll(s[i] + ":" + s[i + 1], "");
break;
}


}
String strs2 = strsz.replaceAll("\\|\\|", "\\|");
char s3[] = strs2.toCharArray();
String s4 = String.valueOf(s3[0]);
String s5 = String.valueOf(s3[s.length - 1]);
if (s4.equals("|")) {
strs2 = new String(s3, 1, (s.length - 1));
}
if (s5.equals("|")) {
strs2 = new String(s3, 0, (s.length - 2));
}
new Operate().deleteAll();
new Operate().save(strs2);
System.out.println("數據刪除成功!");


}


public static void arrayAll() throws Exception {
String strf = new Operate().load();
String strf1 = strf.replaceAll(":", "\\|");
String sf[] = strf1.split("\\|");
Student[] stu = new Student[sf.length / 2];
String a = null;
Float b = null;
for (int i = 0; i < sf.length / 2; i++) {
a = sf[2 * i];
b = Float.parseFloat(sf[2 * i + 1]);
stu[i] = new Student(a, b);// 因爲少了個new所以經常報錯!


}
java.util.Arrays.sort(stu);
new Operate().deleteAll();
new Operate().save(stu[0].toString());
for (int i = 1; i < stu.length; i++) {
new Operate().save(stu[i].toString());


}
String strs = new Operate().load();
System.out.println(strs);
System.out.println("數據全部查出!");
}


public static void add() {
InputData input = new InputData();
String students = input.getStudents("請在下面輸入學生姓名和成績,格式爲(姓名:成績),如有多個學生以"|"線隔開:", "格式不對,請重新輸入!");


try {
new Operate().save(students);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("數據增加成功!");
}


}


//下面這個是引用類Menu,用於連接主程序和操作方法
public class Menu {
public Menu() throws Exception {
while(true) {
this.show();
}
}
public void show() throws Exception {
System.out.println("====分數錄入系統====");
System.out.print("[1]、增加數據。  ");
System.out.print("[2]、刪除一個數據。  ");
System.out.print("[3]、更改一個數據。  ");
System.out.print("[4]、查看一個數據。  ");
System.out.print("[5]、查看全部數據。  ");
System.out.print("[6]、刪除全部數據。  ");
System.out.println("[0]、系統退出。");
InputData input=new InputData();
int i=input.getInt("請在下面選擇:", "請在下面輸入正確的選項!");
switch(i) {
case 1:{
Operate.add();//ok
break;
}
case 2:{
Operate.deleteOne();//ok
break;
}
case 3:{
Operate.update();//ok
break;
}
case 4:{
Operate.search();//ok
break;
}
case 5:{
Operate.arrayAll();
break;
}
case 6:{
Operate.delete();//ok

break;
}
case 0:{
System.exit(1);
break;
}
default:{
System.out.println("請在下面選擇正確的操作:");
}

}
}


}

//下面這個是引用類Student,用於排序

import java.io.File;


class Student implements Comparable<Student> {
private String name;
private float score;


Student(String name, float score) {
this.setName( name);
this.setScore(score);
}


public void setName(String name) {
this.name = name;
}


public String getName() {
return name;
}


public void setScore(Float score) {
this.score = score;
}


public Float getScore() {
return score;
}


public String toString() {
return name + ":" + score;
}


public int compareTo(Student stu) {
if (this.score > stu.score) {
return -1;
} else if (this.score < stu.score) {
return 1;
} else {
return 0;
}
}


}

//主程序
public class AndrewK12_195 {


public static void main(String[] args) throws Exception {
File file = new File("D:" + File.separator + "ZCA" + File.separator + "Java" + File.separator
+ "AndrewPackage" + File.separator + "TheScoresOfStudent.txt");
if(!file.exists()) {
file.createNewFile();
}


new Menu();
}


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章