the name of this project is ProjecTest:
class ProjectTest
//Written by Tianmo
import java.util.*;
/*
* package file include write and read
*/
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
/*
* package IOE try and catch
*/
import java.io.IOException;
import java.awt.Desktop;
import java.lang.*;
/*
* the project:projectTest.java
*
* the access of the whole project
*/
public class ProjectTest {
protected Member ms[] = null;
protected int num = 0;//接收用户输入,将产生的对象
protected String fileName = "test.html";//默认存储HTML文件名字
protected String savePath = "D:" + File.separator + fileName;//默认存储于HTML文件路径
public static void main(String[] args) {
//global pointer
ProjectTest p = new ProjectTest();
//the choice which you want to
char choice;
while(true){
p.showMenue();//display menu
choice = ScreenIO.promptForChar("Enter H/?,G,S,I,L ,D or ?(for help):");
switch(Character.toUpperCase(choice)){
case 'G':p.generateMembers();break;
case 'S':p.sortMembers();break;
case 'I':try {
p.saveIntoHTMLFile();
} catch (Exception e) {
e.printStackTrace();
}break;
case 'L':try {
p.lauchBrowserFile();
} catch (IOException e) {
e.printStackTrace();
}break;
case '?':p.showMenue();break;
case 'H':p.showMenue();break;
case 'D':p.display();break;
case 'E':System.exit(0);
System.out.println("exit !");
return;
}//end switch
}//end while
}//end main
//generate Members
public void generateMembers(){
num = ScreenIO.promptForInt("\tHow many members to be generaterd: ");
ms = new Member[ num ];
Random rdn = new Random();
int randNum = 0;
for(int i = 0; i < num; i++){
randNum = rdn.nextInt(5);
switch( randNum ){
case 0:ms[i] = new Member();;break;
case 1:ms[i] = new Student();break;
case 2:ms[i] = new Employee();break;
case 3:ms[i] = new Staff();break;
case 4:ms[i] = new Faculty();break;
}//end switch
}//end for
}//end generateMembers
//sort members
public void sortMembers(){
//按ID 姓名排序
if(null == ms){
System.out.println("Please run the gernerate function first.");
return;
}//end if
java.util.Arrays.sort(ms);
}//end sortMembers
public void saveIntoHTMLFile()throws Exception{
//第1步、声明File对象
File f = new File(savePath);// 声明File对象
// 第2步、通过子类实例化父类对象
OutputStream out = null ; // 准备好一个输出的对象
out = new FileOutputStream(f) ; // 通过对象多态性,进行实例化
// 第3步、进行写操作.准备输入的内容
String head = "<!DOCTYPE html><html><head><title>saveIntoHtml</title></head><body><table border=1><tr>" +
"<td>ClassName</td><td>ID</td><td>firtName</td><td>lastName</td><td>Major</td>" +
"<td>GPA</td><td>Department</td><td>yearHired</td><td>Degree</td><td>jobTitle</td></tr>" ;
String body = "" ;
String className = "";
for(int i = 0; i < num; i++){
body += "<tr>" ;
className = ms[i].getClass().getName();//get className
if("Member" == className)
body += "<td>Member</td>" + ms[i].toString() + "<td>null</td><td>null</td><td>null</td><td>null</td><td>null</td><td>null</td>";
else if("Employee" == className)
body += "<td>Employee</td>" + ms[i].toString()+ "<td>null</td><td>null</td>";
else if("Student" == className)
body += "<td>Student</td>" + ms[i].toString();
else
body += ms[i].toString();
//end if
//body += ms[i].toString();
body += "</tr>";
}
String tail = "</table></body></html>" ;
String str = head + body + tail;
byte b[] = str.getBytes();// 只能输出byte数组,所以将字符串变为byte数组
for(int i = 0; i < b.length; i++){// 采用循环方式写入
out.write(b[i]); // 每次只写入一个内容
}
// 第4步、关闭输出流
out.close() ;
}//end saveIntoHTMLFile
//使用浏览器打开已经保存的网页
public void lauchBrowserFile()throws IOException{
//使用默认浏览器打开
//环境:需要jdk1.6以上版本
//判断当前系统是否支持Java AWT Desktop扩展
if(java.awt.Desktop.isDesktopSupported()){
try {
//创建一个URI实例
java.net.URI uri = java.net.URI.create(savePath.replace('\\', '/')); //生成uri,将文件分隔符'\'替换成'/'
//获取当前系统桌面扩展
java.awt.Desktop dp = java.awt.Desktop.getDesktop();
//判断系统桌面是否支持要执行的功能
if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
//获取系统默认浏览器打开链接
dp.browse(uri);
}//end if
} catch(java.lang.NullPointerException e){
//此为uri为空时抛出异常
} catch (java.io.IOException e) {
//此为无法获取系统默认浏览器
}//end try
}//end if
}//lauchBrowserFile
//display menu
public void showMenue(){
String menu = "";
menu += "\t============== CS 294 Assignment 1 ===============\n";
menu += "\tG/g: Ask for a N, and generate N members of mixed \n"+
"\tMember class's objects, and store in Vector Class Object.\n";
menu += "\tS/s/ : Sort the memebr in the array in ascending order.\n";
menu += "\tI/i/ : Save objects inside vector into a HTML file.\n";
menu += "\tL/l : Launch the default internet browser to\n"+ "\t display the generated HTML file.\n";
menu += "\t--------------------------------------------------\n";
menu += "\tH/h/?: Show menu.\n";
menu += "\tD/d : Display all members.\n";
menu += "\tE/e : Exit\n";
menu += "\t=================================================\n";
System.out.println(menu);//show menu
}
//print the object which already generate
public void display(){
for(int i = 0;i < num;i ++){
System.out.println("\tThe object is :" + i+ " " + ms[i]);
}//end for
}//end display
}//end the projectTest
public interface Names {
// Fields in interface are always and implicitly public, static and final.
// All methods in interface are implicitly public and abstract.
public static final String sport [] = {
"Basketball", "Table Tennis", "Swiming", "Tennis", "Golf", "Hockey",
"Target Shooting", "VolleyBall", "Soccer", "FootBall"};
public static final String department [] = {
"Math", "Business", "Moden Language", "Dentistry", "Optomistry",
"Computer Sci.", "Pharmacy", "Engineering", "Fine Arts", "MIS",
"Biology", "Nursing"};
public static final String title [] = {
"Accountant", "Adm. Assit.", "Techician", "Manager" };
public static final String position[] = {
"Lecturer", "Assoc. Prof", "Assit. Prof.", "Professor" };
public static final String degree[] = { "B.S.", "M.S.", "Ph.D.", "M.D." };
public static final String skill[] = {
"Typist", "Admin", "Admin. Assist.", "PC Main.", "Elec.", "Account.", "Purch." };
public static final String lastName [] = {
"Abdalla", "Andres", "Anzai", "Armstrong", "Arvig", "Ash", "Baca", "Badine",
"Baehr", "Bair", "Baligad", "Barlow", "Barrett", "Becker", "Bell",
"Benbow", "Biggs", "Blevins", "Blood", "Bohan", "Bond", "Bonner",
"Bosch", "Bryson", "Buechele", "Bullis", "Burk", "Burns", "Byron",
"Calderon", "Careuthers", "Carr", "Carson", "Carter", "Castle", "Chan",
"Charney", "Christenson", "Cisneros", "Cliton", "Coldewey", "Coodey",
"Cook", "Cooper", "Coy", "Currie", "Curtis", "Dirkse", "Dirscoll",
"Dodrill", "Ehlert", "Elias", "Elliott", "Enterline", "Ericsson",
"Fernandez", "Fisher", "Flory", "Freese", "Fruzza", "Fu", "Fuhrman", "Gage",
"Garcia", "Garmon", "Giffith", "Gill", "Gillen", "Glascock", "Gomez",
"Goraya", "Greer", "Hansen", "Hartley", "Hawkins", "Hemme", "Hensley",
"Hentges", "Herron", "Hightower", "Hines", "Holloway", "Holmes",
"Issac", "Jackson", "Jagodin", "Jiang", "Johnson", "Jordan", "Jouda", "Joven",
"Kalpesh", "Katz", "Kaur", "Kegley", "Kimsey", "King", "Kohn", "Kone",
"LaCasse", "Lackey", "Lathrop", "Le", "Levy", "Lynos", "Maas", "Mackall",
"Madsen", "Magee", "Maldonado", "McDaneld", "McMillian", "McNeill",
"Meadors", "Medina", "Metz", "Millikin", "Minner", "Mondragon", "Monge", "Moore",
"Moreno", "Murkland", "Musick", "Myers", "Neal", "Ngo", "Nolan", "Nunez",
"O'Neil", "Oropeza", "Owen", "Patel", "Pfister", "Philips", "Picato",
"Porras", "Porter", "Pratt", "Pryor", "Ragle", "Rai", "Raja", "Rill",
"Roberts", "Roddy", "Rose", "Roux", "Rubio", "Ruiz", "Saito", "Samidin",
"Sampson", "Sanchez", "Scanlan", "Schmidt", "Schoberg", "Sheppard",
"Simmons", "Sison", "Smith", "Snow", "Soto", "Sozinho", "Stevens",
"Stidham", "Strange", "Sy", "Tamayo", "Taylor", "Thomas", "Tran", "Vincent",
"Walker", "Wandke", "Ward", "Warner", "Waugh", "Webb", "Wells", "White",
"Wilson", "Winther", "Wuertz", "Yong" };
public static final String firstName [] = {
"Aaron","Alan", "Ali", "Alicia","Andrew", "Anne", "Anthony", "Archie", "Ariel",
"Arlene", "Barbara", "Bebedict", "Benjamin", "Bey-Fen", "Bikram", "Brain",
"Brandon", "Brent", "Brian", "Brock", "Candice", "Cathleen", "Chad",
"Charles", "Charlie", "Christopher", "Cindy", "Clinton", "Craig", "Daniel",
"Danna", "David", "Dean", "Denise", "Dexter", "Diane", "Dinna", "Donna",
"Douglas", "Eduardo", "Edward", "Elsa", "Eric", "Ernesto", "Fernando",
"Francisco", "Frank", "Gabby", "Garmund", "Garvin", "Genevieve", "George",
"Gil", "Gregory", "Gurinder", "Hemant", "Herman", "Hernando", "Isabel",
"James", "Janet", "Janice", "Jarret", "Jason", "Javier", "Jeffrey",
"Jennifer", "Jeremy", "Jerold", "Jerrie", "Jim", "Joanna", "Joe", "Joel", "John",
"Jon", "Jonathan", "Jose", "Joseph", "Josephine", "Josh", "Joshua",
"Julie", "Kenneth", "Kenny", "Kent", "Kristi", "Kurt", "Kyle", "Lance",
"Laura", "Leslie", "Malcolm", "Maneje", "Manja", "Marco", "Marcos",
"Marcus", "Marianna", "Maritza", "Mark", "Marques", "Masaaki", "Mathew", "Matt",
"Mayur", "Melissa", "Michael", "Miera", "Mike", "Mina", "Mohamed", "Moises",
"Morgan", "Muntaga", "Myron", "Nathan", "Nilli", "Norma", "Olga",
"Paradorn", "Patel", "Patrick", "Paul", "Pedro", "Peter", "Philip",
"Preet", "Ralf", "Ralph", "Raymond", "Reuben", "Ricardo", "Richard",
"Robert", "Roberto", "Rohit", "Ronald", "Ronne", "Rosa", "Roy", "Rushabh",
"Ryan", "Samat", "Sara", "Sarah", "Scott", "Sean", "Sergio", "Shahzad", "Shawn",
"Shi-Wei", "Silvia", "Simon", "Stacy", "Stephanie", "Stephen", "Steven",
"Sukhum", "Susan", "Suzanne", "Tahnee", "Tahra", "Tam", "Tammy", "Tate",
"Terry", "Thomas", "Thonas", "To", "Todd", "Tommy", "Travis", "Trevor",
"Troy", "Valerie", "Ven", "Victor", "Vidya", "Vikramjwed", "Wernher",
"Wesley", "William", "Yu" } ;
}
import java.util.*;
/*
* Member class : In file Member.java
*
* Team Member:
*
* A based class for Student, Employee, Staff, and Faculty classes.
*/
/*
* Access specficiers:public, protect, private, default (package-wide accessable)
*/
public class Member implements Comparable/*<Member>*/ {
protected long ID;//100000000~999999999
protected String fName;
protected String lName;
static Random rdn = new Random();
//constructor is function with following function:
//1. no return value;
//2. name the same with class name
//3. default constructor with no Parameter
public Member(){
generate();
}
/*
public int compareTo( Member m ){
//return (int)(ID - m.ID); //sort by ID
//sort by name
int k = lName.compareTo( m.lName );
return k!=0 ? k:fName.compareTo ( m.fName );
}
*/
// copy constructor : a constructor with one parameter of this class type
public void generate(){
//ID = (long) (Math.random() % (1000000000 -100000000 +1)+100000000);
ID = rdn.nextInt(1000000000 -100000000) + 100000000;
fName = Names.firstName[(int) ( rdn.nextInt(Names.firstName.length) )];
//fName = new String (Names.firstName[Math.rand() % Names.firstName.length]);
lName = Names.lastName[(int) ( rdn.nextInt(Names.lastName.length))];
}// end generate
//输出类的数据信息
public String toString(){
return toString(true);
}
//输出类的数据信息 lab用于控制显示表头
public String toString( boolean Lab){
//String.format
//-: 指定为左对齐,默认右对齐
//0: 指定缺字符时用0填充
//return String.format("<td>%s</td> <td>%03d-%02d-%04d</td> <td>%15s</td> <td>%-15s</td>",
//Lab ? "Member":" ",
return String.format("<td>%03d-%02d-%04d</td><td>%15s</td><td>%-15s</td>",
ID/1000000,
ID%1000000/10000,
ID%10000,
fName,
lName);
}// end toString
//实现compareTo() 接口,用于排序
public int compareTo(Object o) {
Member m = (Member)o;
int result = 0;
result = (int)(ID - m.ID);
if(result == 0){
result = fName.compareTo(m.fName);
if(result == 0){
result = lName.compareTo(m.lName);
}//end if
}// end if
return result;
}//end compareTo
}//end class Member
public class Student extends Member {
protected String major;
protected float GPA;
//constructor
public Student(){
generate();
}
//generate data
public void generate(){
super.generate();
//major=Names.department[(int)(Math.random())%Names.department.length];
major = Names.department[(int) ( rdn.nextInt(Names.department.length) )];
GPA = ( rdn.nextInt(300) + 100 ) / 100.0f;
}//end generate
//print the datas
public String toString(){
return toString(true);
}
//输出类的数据信息 ,lab用于控制显示表头
public String toString( boolean lab){
//return String.format("<td>%3s<td> %45s <td>%15s</td> <td>%-2.2f</td> <td>null</td><td>null</td><td>null</td><td>null</td>",
//lab?"Student":" ",
return String.format("%45s<td>%15s</td><td>%-2.2f</td><td>null</td><td>null</td><td>null</td><td>null</td>",
super.toString(false),
major,
GPA);
}//end toString
//实现compareTo() 接口,用于排序
public int compareTo(Object o) {
return super.compareTo(o);
}//end compareTo
}//end class Student
/*
* Employee class : In file Employee.java
*
* Team Member:
*
* A based class for Staff and Faculty classes.
*/
/*
* Access specficiers:public, protect, private, default (package-wide accessable)
*/
public class Employee extends Member{
protected String department;
protected int yearHired;
//constructor is function with following function:
//1. no return value;
//2. name the same with class name
//3. default constructor with no Parameter
public Employee(){
generate();
}
// copy constructor : a constructor with one parameter of this class type
public void generate(){
super.generate();
//department=Names.department[(int)(Math.random())%Names.department.length];
department = Names.department[rdn.nextInt(Names.department.length)];
yearHired = rdn.nextInt(30);
}//end generate
//输出类的数据信息
public String toString(){
return toString(true);
}
//输出类的数据信息lab控制输出表头
public String toString( boolean Lab){
//return String.format("<td>%s</td> %45s <td>null</td><td>null</td><td>%15s</td> <td>%2d</td> <td>null</td><td>null</td>",
//Lab ? "Employee":" ",
return String.format("%45s <td>null</td><td>null</td><td>%15s</td> <td>%2d</td>",
super.toString(false),
department,
yearHired);
}//end toString
public int compareTo(Object o) {
return super.compareTo(o);
}//end compareTo
}//end class Employee
public class Faculty extends Employee{
protected String degreeHeld;
public Faculty(){
generate();
}
public void generate(){
super.generate();
//degreeHeld=Names.degree[(int)(Math.random())%Names.degree.length];
degreeHeld = Names.degree[rdn.nextInt(Names.degree.length)];
}//end generate
public String toString(){
return toString(true);
}
public String toString( boolean Lab){
return String.format("<td>%s</td> %45s <td>%15s</td> <td>null</td>",
Lab ? "Faculty":" ",
super.toString(false),
degreeHeld);
}//end toString
public int compareTo(Object o) {
return super.compareTo(o);
}//compareTo
}//end class Faculty
public class Staff extends Employee{
protected String jobTitle;
public Staff(){
generate();
}
public void generate(){
super.generate();
//jobTitle=Names.title[(int)(Math.random()%Names.title.length];
jobTitle = Names.title[rdn.nextInt(Names.title.length)];
}//end generate
public String toString(){
return toString(true);
}
public String toString( boolean Lab){
return String.format("<td>%s</td> %45s <td>null</td><td>%15s</td>",
Lab ? "Staff":" ",
super.toString(false),
jobTitle);
}//end toString
public int compareTo(Object o) {
return super.compareTo(o);
}//end compareTo
}//end class Staff