iCarnegie Information
import java.io.*;
import java.util.*;
public class ICarnegieInfoApplication {
private static BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
private static PrintWriter stdOut =
new PrintWriter(System.out, true);
private static PrintWriter stdErr =
new PrintWriter(System.err, true);
public static void main(String[] args) throws IOException {
ICarnegieInfo companyInfo = ICarnegieInfo.getInstance();
int choice = getChoice();
while (choice != 0) {
if (choice == 1) {
stdOut.println(companyInfo.getName());
} else if (choice == 2) {
stdOut.println(companyInfo.getAddress());
} else if (choice == 3) {
stdOut.println(companyInfo.getTelephone());
} else if (choice == 4) {
stdOut.println(companyInfo.getEmail());
} else if (choice == 5) {
stdOut.println(companyInfo.getUrl());
}
choice = getChoice();
}
}
private static int getChoice() throws IOException {
/* Following is my code */
String choice=stdIn.readLine(); //stdIn.read()只能读取单个字符
try
{
int iChoice=Integer.parseInt(choice);
return iChoice;
}
catch(NumberFormatException e)
{
stdErr.println("input error.Please input again");
return -1;
}
}
}
考察:
1.处理异常。输入输出异常以及数据类型异常。
2.ICarnegieInfo companyInfo = ICarnegieInfo.getInstance();
单件模式(不能直接调用构造函数,该类只能创建一个实例)
public class className {
private static className instance = NULL;
...
private className(){...} //非public的构造函数
public static className getInstance() {
if(instance == NULL)
instance = new className();
return instance;
}