import java.util.*;
import java.io.*;
/**
* A class that represents a class score book. This
* book maps names to scores and grades. This will
* read the names, scores and grade information from a file.
*/
public class CourseData2 {
/////////////////// fields ///////////////////
private String fileName;
private Map phoneMap = new HashMap();
/////////////////// constructors ///////////////////
/**
* Constructor that takes a file name and reads
* in the names and phone numbers from a file.
* @param file the name of the file to read
*/
public CourseData2(String file) {
this.fileName = file;
// Read the map information in form the file
readInfoFromFile();
} // constructor
/////////////////// methods ///////////////////
/**
* Get the phone number for hte passed nam
* @param name the name to look up in hte map
* @return the phone number if found, else null
*/
public String getPhoneNumber(String name) {
String phoneNumber = (String) phoneMap.get(name);
return phoneNumber;
} // method getPhoneNumber()
/**
* Method to read the phone information from a
* file and use it to fill hte map
*/
public void readInfoFromFile() {
String line = null;
String[] phoneArray = null;
try {
// create the reader
BufferedReader reader =
new BufferedReader(new FileReader(fileName));
// loop reading from the file
while ((line = reader.readLine()) != null) {
while ((line = reader.readLine()) != null) {
if (line.indexOf(":") >= 0) {
phoneArray = line.split(":");
int f = (String)phoneArray[2];
if (0 <= f <= 100) {
String a = null;
if (0 <= f < 60) { a = "E";}
else if (60 <= f < 70) { a = "D";}
else if (70 <= f < 80) { a = "C";}
else if (80 <= f < 90) { a = "B";}
else if (90 <= f <= 100) { a = "A";}
}
else if ( ((int)f > 100) + ((int)f < 0) ) {
SimpleOutput.showError("Score is not an integer");
String g = new String();
g = phoneArray[2];
}//if < 0
}//while 2
phoneMap.put(phoneArray[0].trim(),
(phoneArray[1].trim() + phoneArray[2].trim() + a.trim()));
} // if
} // while
/*if (f < 60) { (String)f = "E";}
else if (60 <= f < 70) { (String)f = "D";}
else if (70 <= f < 80) { (String)f = "C";}
else if (70 <= f < 90) { (String)f = "B";}
else if (90 <= f) { (String)a = "A";}*/
// Close the reader
reader.close();
} // try
catch (FileNotFoundException ex) {
SimpleOutput.showError("Could not find the file \"" + fileName + "\".");
} // catch
catch (Exception ex) {
ex.printStackTrace();
} // catch
} // method readInfoFromFile()
public void printBook() {
// Sort hashtable.
Vector phoneVector = new Vector(phoneMap.keySet());
Collections.sort(phoneVector);
// Display (sorted) hashtable.
for (Enumeration phoneEnumeration = phoneVector.elements(); phoneEnumeration.hasMoreElements();) {
String key = (String)phoneEnumeration.nextElement();
System.out.println("Name: " + key + ", Year: " + phoneMap.get(key) + ", Score: " + phoneMap.get(key) + ", Grade: " + phoneMap.get(key));
} // for
} // method printBook()
/* main method for testing */
public static void main(String[] args) {
CourseData2 phoneBook = new CourseData2("scores12.txt");
System.out.println(phoneBook.getPhoneNumber("Lee"));
System.out.println(phoneBook.getPhoneNumber("Smith"));
phoneBook.printBook();
} // main()
} // class