⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inputdata.java

📁 学校的学生和工人管理系统 可以完成增删改查的功能
💻 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 + -