?? inputdata.java
字號:
/*
* 創建日期 2005-9-21
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
package cn.itcareers.lxh.exercise.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import cn.itcareers.lxh.exercise.interfaces.Person;
import cn.itcareers.lxh.exercise.person.AbstractPerson;
import cn.itcareers.lxh.exercise.person.Student;
import cn.itcareers.lxh.exercise.person.Worker;
/**
* @author 白濤
*
* 此類主要用于輸入數據
*/
public class InputData {
private BufferedReader buf = null;
/**
* 構造方法實例化BufferedReader對象
*
*/
public InputData() {
buf = new BufferedReader(new InputStreamReader(System.in));
}
/**
*
* @param objectname 要操作的類,是工人還是學生
* @return Person實例化對象
* @throws Exception 可能拋出的異常
*/
public Person getPerson(String objectname) throws Exception {
System.out.print("請輸入姓名(1 — 15位):");
String name = this.getString();
System.out.print("請輸入年齡(1 — 150):");
int age = this.getInt();
String temp = objectname.substring(objectname.lastIndexOf(".") + 1);
if (temp.equalsIgnoreCase("Student")) {
System.out.print("請輸入學生成績(0 — 100):");
}
if (temp.equalsIgnoreCase("Worker")) {
System.out.print("請輸入工人工資:");
}
float other = this.getFloat();
Class c = null;
try {
c = Class.forName(objectname);
} catch (ClassNotFoundException e) {
throw e;
}
Person p = null;
Constructor cs[] = c.getConstructors();
// 此處需要對是否有構造方法進行檢驗
Object t[] = { name, new Integer(age), new Float(other) };
try {
p = (Person) cs[0].newInstance(t);
} catch (Exception e1) {
throw e1;
}
return p;
}
/**
* 更新數據操作
* @param p 要操作的Person對象
* @throws Exception 可能拋出的異常
*/
public void updatePerson(Person p) throws Exception {
String objectname = p.getClass().getName();
String temp = objectname.substring(objectname.lastIndexOf(".") + 1);
AbstractPerson ap = (AbstractPerson) p;
System.out.println("原姓名為:" + ap.getName().trim());
System.out.print("請輸入新的姓名(1 — 15位):");
ap.setName(this.getString());
System.out.println("原年齡為:" + ap.getAge());
System.out.print("請輸入新年齡(1 — 150):");
ap.setAge(this.getInt());
if (temp.equalsIgnoreCase("Student")) {
System.out.println("原成績為:" + ((Student) ap).getScore());
System.out.print("請輸入新成績(0 — 100):");
((Student) ap).setScore(this.getFloat());
}
if (temp.equalsIgnoreCase("Worker")) {
System.out.println("原工資為:" + ((Worker) ap).getSalary());
System.out.print("請輸入新工資:");
((Worker) ap).setSalary(this.getFloat());
}
}
/**
*
* @param p
* @return
*/
public Person getPerson(Person p) {
return null;
}
/**
* 用于輸入字符串的操作
* @return
*/
public String getString() {
String str = "";
boolean flag = true;
while (flag) {
try {
str = buf.readLine();
if(str.indexOf(":")!=-1)
{
throw new IOException() ;
}
flag = false;
} catch (IOException e) {
System.out.print("數據輸入錯誤?。。n請重新輸入:");
}
}
return str;
}
/**
* 此方法用于得到整型數據
* @return 返回一整數
*/
public int getInt() {
int num = 0;
boolean flag = true;
while (flag) {
try {
num = Integer.parseInt(buf.readLine());
flag = false;
} catch (Exception e) {
System.out.print("輸入數據錯誤!??!\n請重新輸入:");
}
}
return num;
}
/**
* 此方法用于得到一浮點數
* @return 返回一浮點數
*/
public float getFloat() {
float num = 0f;
boolean flag = true;
while (flag) {
try {
num = Float.parseFloat(buf.readLine());
flag = false;
} catch (Exception e) {
System.out.print("輸入數據錯誤!!!\n請重新輸入:");
}
}
return num;
}
/**
* 此方法用于關閉BufferedReader操作
*
*/
public void close() {
try {
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -