定义一个Person类,产生一个对象,要求里面有姓名、年龄、成绩属性,之后把此对象保存在文件之中,可以在屏幕上显示人员的信息,也可以修改信息,但是要求有一个完整的菜单出现。
如果完成此程序需要有几个类呢?
1、Person类
2、文件的操作类
3、输入数据的类
4、具体的操作Person的类
使用Eclipse进行开发
先分别定义以下几个类:
Person类
定义Person类属性
PersonOperate类
InputData输入数据类
定义三个方法:
Menu菜单显示类:
FileOperate文件操作类
Main主类测试客户端:
定义好这五个类后我们就要分别写代码哈~
首先是Person类
我们要写上Setter和Getter方法
然后写上两个构造方法
去掉黄色警告CTRL+1
这样Person类就写完了哈~~~
InputData类写代码
所有的输入数据的代码实际上都是很相似的,所以在此处设计一个InputData类,此类专门用于取得输入的各种数据类型。
这样就完成了输入数字的功能,我们在Test中进行验证
Person类完整代码:
package org.redking.demo.vo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Person implements Serializable{
private String name;
private int age;
private float score;
public Person(){}
public Person(String name, int age, float score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public String toString(){
return "姓名:"+this.name+",年龄:"+this.age+",成绩:"+this.score;
}
}
PersonOperate类完整代码:
package org.redking.demo.op;
import org.redking.demo.util.FileOperate; import org.redking.demo.util.InputData; import org.redking.demo.vo.Person;
public class PersonOperate {
private InputData input = null;
public PersonOperate(){
this.input = new InputData();
}
//完成具体的Person对象操作
public void add(){
//要使用输入数据的类
String name = null;
int age = 0;
float score = 0.0f;
System.out.print("输入姓名:");
name = this.input.getString();
System.out.print("输入年龄:");
age = this.input.getInt();
System.out.print("输入成绩:");
score = this.input.getFloat();
//生成Person对象,把对象保存在文件中
Person p = new Person(name,age,score);
try{
new FileOperate().save(p);
System.out.println("数据保存成功!");
}catch(Exception e){
System.out.println("数据保存失败!");
}
}
public void show(){
//从文件中把内容读进来
Person p = null;
try{
p = (Person) new FileOperate().read();
}catch(Exception e){
System.out.println("内容显示失败,请确定数据是否存在!");
}
if(p != null){
System.out.println(p);
}
}
public void update(){
//先将之前的信息查出来
Person p = null;
try{
p = (Person) new FileOperate().read();
}catch(Exception e){
System.out.println("内容显示失败,请确定数据是否存在!");
}
if(p != null){
String name = null;
int age = 0;
float score =0.0f;
System.out.print("请输入新的姓名(原姓名:"+p.getName()+")");
name = this.input.getString();
System.out.print("请输入新的年龄(原年龄:"+p.getAge()+")");
age = this.input.getInt();
System.out.print("请输入新的成绩(原成绩:"+p.getScore()+")");
score = this.input.getFloat();
//信息重新设置
p.setName(name);
p.setAge(age);
p.setScore(score);
try{
new FileOperate().save(p);
System.out.println("数据更新成功!");
}catch(Exception e){
System.out.println("数据更新失败!");
}
}
}
}
|
InputData类完整代码:
package org.redking.demo.util;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class InputData {
private BufferedReader buf =null;
public InputData(){
buf = new BufferedReader(new InputStreamReader(System.in));
};
public String getString(){
String str = null;
try {
str = buf.readLine();
} catch (IOException e) {}
return str;
}
public int getInt(){
int temp = 0;
//如果输入的不是数字,至少应该有一个提示,告诉用户输入错了~
//可以使用正则验证
String str = null;
boolean flag = true;
while(flag){
//输入数据
str = this.getString();
if (!(str.matches("\\d+"))){
//如果输入的不是一个数字,则必须重新输入
System.out.print("输入的内容必须是整数,请重新输入:");
}else{
//输入的是一个正确的数字,则可以进行转换
temp = Integer.parseInt(str);
//表示退出循环
flag = false;
}
}
return temp;
}
public float getFloat(){
float f = 0.0f;
//如果输入的不是数字,至少应该有一个提示,告诉用户输入错了~
//可以使用正则验证
String str = null;
boolean flag = true;
while(flag){
//输入数据
str = this.getString();
if (!(str.matches("\\d+.?\\d{1,2}"))){
//如果输入的不是一个数字,则必须重新输入
System.out.print("输入的内容必须是小数(小数点后两位),请重新输入:");
}else{
//输入的是一个正确的数字,则可以进行转换
f = Float.parseFloat(str);
//表示退出循环
flag = false;
}
}
return f;
}
}
|
FileOperate文件操作类完整代码:
package org.redking.demo.util;
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;
public class FileOperate {
public static final String FILENAME = "E:\\person.ser";
//把对象保存在文件之中
public void save(Object obj){
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME)));
//写入对象
out.writeObject(obj);
}catch(Exception e){
try {
throw e;
} catch (Exception e1) {}
}finally {
try {
out.close();
}catch(Exception e){}
}
}
//把对象从文件之中读出来
public Object read() throws Exception{
Object obj = null;
ObjectInputStream input =null;
try {
input = new ObjectInputStream(new FileInputStream(new File(FILENAME)));
obj = input.readObject();
} catch (Exception e) {
throw e;
}finally{
try{
input.close();
}catch(Exception e){}
}
return obj;
}
}
|
Menu菜单类完整代码:
package org.redking.demo.menu;
import org.redking.demo.op.PersonOperate; import org.redking.demo.util.InputData;
public class Menu {
InputData input = null;
public Menu(){
this.input = new InputData();
//循环出现菜单
while(true){
this.show();
}
}
//需要定义的菜单内容
public void show(){
System.out.println("\t\t\t1、增加人员信息");
System.out.println("\t\t\t2、浏览人员信息");
System.out.println("\t\t\t3、修改人员信息");
System.out.println("\t\t\t4、退出系统");
System.out.print("\n\n请选择要使用的操作:");
int temp = input.getInt();
switch(temp){
case 1:{
new PersonOperate().add();
break;
}
case 2:{
new PersonOperate().show();
break;
}
case 3:{
new PersonOperate().update();
break;
}
case 4:{
System.out.println("选择的是退出系统");
System.out.println("系统退出!");
System.exit(1);
}
default:{
System.out.println("输入的内容不正确");
break;
}
}
}
}
|
Test测试类完整代码:
package org.redking.demo.main;
import org.redking.demo.menu.Menu;
public class Test {
public static void main(String[] args) {
new Menu();
}
}
|
测试:
测试成功~~~
总结
在整个程序中某一块的修改不会影响其他部分。
注意:
在编写代码中,类的形成是逐步完成的,不是一下子就一定可以设计一个非常完美的类。
########################################
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/134645,如需转载请自行联系原作者