📄 workspace.java
字号:
import java.io.*;
import java.util.*;
public class WorkSpace {
private HashMap<Student, Course> mClass=new HashMap<Student, Course>();
private int numberOfStudents;
private int numberOfCourses;
public void printWelcome(){
System.out.println("Welcome to the Grading System.");
}
public void printMenu(){
System.out.println(
"1.Add Score by Hand;\n" +
"2.Add Scores by file;\n" +
"3.Query Scores of a Student;\n" +
"4.Query Scores of a Course;\n" +
"5.Quit.\n" +
"Please Enter your choose(1,2,3 or 4):"
);
}
public void printGoodbye(){
System.out.println("Bye..");
}
public void enterDescription(){
System.out.println(
"Please enter your information in this form"
+"\"name, course name, score\"(without the quotation mark):");
}
public void fileDescription(){
System.out.println(
"Please guarantee the information in this form"
+"\"name, course name, score\"(without the quotation mark)!");
System.out.println("Please enter the path of your file:");
}
public boolean studentIn(String key){
return mClass.containsKey(key);
}
public boolean CourseIn(String value){
return mClass.containsValue(value);
}
public void addStuInfo(String sname,String cname,int value){
if(!studentIn(sname)){
Student bufStudent=new Student(sname);
mStudents.put(sname,bufStudent);
}
mStudents.get(sname).insertScore(cname,value);
}
public void addCourInfo(String sname,String cname,int value){
if(!CourseIn(sname)){
Course bufCourse=new Course(cname);
mCourses.put(cname,bufCourse);
}
mCourses.get(cname).insertScore(sname,value);
}
public int getNumberOfStudents(){
return numberOfStudents;
}
public int getNumberOfCourses(){
return numberOfCourses;
}
public void singleInput(){
enterDescription();
Scanner reader=new Scanner(System.in);
boolean tryAgain=true;
while(tryAgain){
String line=reader.nextLine();
String wordArray[]=line.split(",");
try{
int value=Integer.parseInt(wordArray[2]);
addStuInfo(wordArray[0],wordArray[1],value);
addCourInfo(wordArray[0],wordArray[1],value);
tryAgain=false;
}catch(RuntimeException e){
System.out.println("You should enter the information correctly!");
}
}
}
public void fileInput(){
fileDescription();
Scanner stdin=new Scanner(System.in);
String file=stdin.nextLine();
Scanner reader=new Scanner(file);
while(reader.hasNextLine()){
String line=reader.nextLine();
String wordArray[]=line.split(",");
boolean err=false;
while(!err){
try{
int value=Integer.parseInt(wordArray[2]);
addStuInfo(wordArray[0],wordArray[1],value);
addCourInfo(wordArray[0],wordArray[1],value);
}catch(RuntimeException e){
err=true;
}
}
if(err){
System.out.println("These're some errors in your file.");
break;
}
}
}
public void retrieveStudent(){
System.out.println("Please enter the student's name:");
Scanner reader=new Scanner(System.in);
double bufGPA = 0.0;
String bufName=reader.nextLine();
if(studentIn(bufName)){
bufGPA=mStudents.get(bufName).listScores();
System.out.println("GPA of "+bufName+"="+bufGPA);
}else{
System.out.println(bufName+" doesn't exist!");
}
}
public void retrieveCourse(){
System.out.println("Please enter the the name of course:");
Scanner reader=new Scanner(System.in);
String bufName=reader.nextLine();
double bufGPA = 0.0;
if(CourseIn(bufName)){
bufGPA=mCourses.get(bufName).listScores();
System.out.println("GPA of "+bufName+"="+bufGPA);
}else{
System.out.println(bufName+" doesn't exist!");
}
}
public void initialize() throws IOException{
BufferedReader reader=new BufferedReader(new FileReader("DataBase.CSV"));
int n=reader.read();
for(int i=0;i<n;i++){
String bufname=reader.readLine();
int m=reader.read();
for(int j=0;j<m;j++){
String bufcourse=reader.readLine();
int bufmark=reader.read();
addStuInfo(bufname,bufcourse,bufmark);
addCourInfo(bufname,bufcourse,bufmark);
}
}
}
public void start() throws IOException{
boolean finished=false;
initialize();
printWelcome();
while(!finished){
printMenu();
Scanner input=new Scanner(System.in);
int choose=input.nextInt();
switch(choose){
case 1:
singleInput();
break;
case 2:
fileInput();
break;
case 3:
retrieveStudent();
break;
case 4:
retrieveCourse();
break;
default:
finished=true;
break;
}
}
printGoodbye();
}
public void listStudents(){
}
public void backUp() throws IOException{
FileWriter writer=new FileWriter("DataBase.CSV");
PrintWriter out = new PrintWriter(writer);
out.println(numberOfStudents);
Iterator it = mStudents.entrySet().iterator();
while(it.hasNext()){
Map.Entry m=(Map.Entry)it.next();
out.println(((Student)m.getValue()).getName());
out.println(((Student)m.getValue()).getNumberOfCourses());
Iterator that =((Student) m.getValue()).mScores.entrySet().iterator();
while(that.hasNext()){
Map.Entry n=(Map.Entry)that.next();
out.println(((Student) n.getValue()).getName());
out.println(((Object) n.getValue()).getMark());
}
}
}
// Set<Student> sortedStudents=new TreeSet<Student>(mStudents.values());
// for(Student bufstudent:sortedStudents){
//
// bufstudent.listScores();
//}
out.close();
writer.close();
}
public static void main(String[] args) throws IOException{
WorkSpace system=new WorkSpace();
system.start();
system.backUp();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -