實現(xiàn)的功能:添加、修改和瀏覽記錄(包括姓名,年齡,職位,薪酬)。
運行程序顯示結(jié)果如下:
1、添加記錄:
2、修改記錄:
3、瀏覽記錄:
4、退出系統(tǒng):
各類之間調(diào)用關(guān)系如下圖所示:
程序代碼如下:
import com.dr.demo.menu.Menu;
public class Main {
public static void main(String[] args) {
new Menu();
}
}
public class Menu {
InputData input = null;
public Menu(){
this.input = new InputData();
//循環(huán)出現(xiàn)菜單
while(true){
this.show();
}
}
//要定義的菜單的內(nèi)容
public void show(){
System.out.println("\t\t\t\t1~增加人員信息");
System.out.println("\t\t\t\t2~修改人員信息");
System.out.println("\t\t\t\t3~瀏覽人員信息");
System.out.println("\t\t\t\t4~退出本系統(tǒng)!");
System.out.println("\n\n請選擇要使用的操作:(1.2.3.4)");
int temp = input.getInt();
switch(temp){
case 1:{//增加人員信息
new PersonOperate().add();//業(yè)務(wù)處理層
break;
}
case 2:{//修改人員信息
new PersonOperate().update();
break;
}
case 3:{//瀏覽人員信息
new PersonOperate().show();
break;
}
case 4:{//退出系統(tǒng)
System.out.println("選擇的是退出本系統(tǒng)!");
System.out.println("成功退出本系統(tǒng)!");
System.exit(1);
}
default:{
System.out.println("輸入的內(nèi)容不正確,請重新輸入!");
break;
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputData {
public 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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public int getInt(){
int temp = 0;
String str = null;
boolean flag = true;
while(flag){
//輸入數(shù)據(jù)
str = this.getString();
if(!(str.matches("\\d+"))){
//輸入的不是1-4的數(shù),提示重新輸入
System.out.println("輸入的數(shù)字有誤,請重新輸入!(必須是整數(shù)1-4)");
}
else {//數(shù)據(jù)輸入正確,進行轉(zhuǎn)換
temp = Integer.parseInt(str);
//退出循環(huán)
flag = false;
}
}
return temp;
}
public String getpsString(){
String str = null;
try {
str = buf.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public float getFloat(){
float f = 0;
String str = null;
boolean flag = true;
while(flag){
str = this.getString();
if(!(str.matches("\\d+?.\\d{1,2}"))){
//如果輸入的不是兩位的小數(shù)則提示重新輸入
System.out.println("輸入的數(shù)必須是float類型,請重新輸入!");
}
else{
f = Float.parseFloat(str);
flag = false;//退出循環(huán)哈
}
}
return f;
}
}
public class PersonOperate {
private InputData input = null;
public PersonOperate(){
this.input = new InputData();
}
//增加記錄
public void add(){
String name = null;
int age = 0;
String position = null;
float salary = 0;
System.out.println("請輸入姓名:");
name = this.input.getString();
System.out.println("請輸入年齡:");
age = this.input.getInt();
System.out.println("請輸入職位:");
position = this.input.getpsString();
System.out.println("請輸入薪酬:");
salary = this.input.getFloat();
//生成對象并保存在文件中
Person p = new Person(name,age,position,salary);
try{
new FileOperate().save(p);//IO操作層
System.out.println("數(shù)據(jù)保存成功!");
}catch(Exception e){
System.out.println("數(shù)據(jù)保存失敗!");
}
}
//瀏覽記錄
public void show(){
Person p = null;
try{
p = (Person)new FileOperate().read();
}catch(Exception e){
System.out.println("顯示數(shù)據(jù)失敗,請檢查數(shù)據(jù)是否存在!");
}
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("顯示數(shù)據(jù)失敗,請檢查數(shù)據(jù)是否存在!");
}
if(p!=null){
String name = null;
int age = 0;
String position = null;
float salary = 0.0f;
System.out.println("請輸入新姓名:(原姓名:"+p.getName()+")");
name = this.input.getString();
System.out.println("請輸入新年齡:(原年齡"+p.getAge()+")");
age = this.input.getInt();
System.out.println("請輸入新職位:(原職位"+p.getPosition()+")");
position = this.input.getpsString();
System.out.println("請輸入新工資:(原工資"+p.getSalary()+")");
salary = this.input.getFloat();
//信息重置
p.setName(name);
p.setAge(age);
p.setPosition(position);
p.setSalary(salary);
try{
new FileOperate().save(p);
System.out.println("數(shù)據(jù)更新成功!");
}catch(Exception e){
System.out.println("數(shù)據(jù)更新失敗!");
}
}
}
}
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:"+File.separator+"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.flush();
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;
}
}
import java.io.Serializable;
public class Person implements Serializable{
public String toString(){
return "姓名:"+this.name+"\t年齡:"+this.age+" \t職位:"+this.position+"\t薪酬:"+this.salary;
}
private String name;
private int age;
private String position;
private float salary;
public Person(){}
public Person(String name,int age,String position,float salary){
this.name = name;
this.age = age;
this.position = position;
this.salary = salary;
}
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 String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
本程序優(yōu)點在于各層之間耦合性不大,獨立性比較強,重在理解Menu層、文件操作層、IO操作層三層之間的調(diào)用關(guān)系。存在的缺陷希望讀者可以完善。