码迷,mamicode.com
首页 > 其他好文 > 详细

Serializable

时间:2014-08-15 20:55:19      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   os   io   for   ar   

 

    Create a class Employee which extends from a class Person. The attributes of Person class are name, address, age. The attributes of the Employee class are id, department, grade and basic. Also write the appropriate constructors for these classes. Write a menu based

program which shows the following options:

a. Create

b. Search

c. Add

d. Print

e. Exit

On selecting create the user should be asked to enter Employee details and these should be stored in a file.

On selecting search the user should be asked to enter a name and the appropriate record from the file should be retrieved and displayed .

On selecting add the user should be able to add an object in the file.

On selecting print all the records of the file should be displayed .

Handle all exceptions appropriately.

 

 1 package Aug14.IO;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Person implements Serializable{  //实现 Serializable 接口
 6     protected String name;
 7     public String address;
 8     public int age;
 9 
10     public Person(String name, String address, int age) {
11         this.name = name;
12         this.address = address;
13         this.age = age;
14 
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public String getAddress() {
26         return address;
27     }
28 
29     public void setAddress(String address) {
30         this.address = address;
31     }
32 
33     public int getAge() {
34         return age;
35     }
36 
37     public void setAge(int age) {
38         this.age = age;
39     }
40 
41 }

 

 

 1 package Aug14.IO;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Employee extends Person implements Serializable{
 6     public int id;
 7     public String department;
 8     public String grade;
 9     public int basic;
10     /*public Employee (){
11         
12     }*/
13     public Employee (int id,String name,int age,String address,String department,String grade,int basic){
14         super(name,address,age);
15         this.id=id;
16         this.department=department;
17         this.grade=grade;
18         this.basic=basic;
19     }
20 
21     public int getId() {
22         return id;
23     }
24 
25     public void setId(int id) {
26         this.id = id;
27     }
28 
29     public String getDepartment() {
30         return department;
31     }
32 
33     public void setDepartment(String department) {
34         this.department = department;
35     }
36 
37     public String getGrade() {
38         return grade;
39     }
40 
41     public void setGrade(String grade) {
42         this.grade = grade;
43     }
44 
45     public int getBasic() {
46         return basic;
47     }
48 
49     public void setBasic(int basic) {
50         this.basic = basic;
51     }
52     
53 }

 

 1 package Aug14.IO;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileOutputStream;
 6 import java.io.ObjectInputStream;
 7 import java.io.ObjectOutputStream;
 8 import java.util.Iterator;
 9 import java.util.Map;
10 import java.util.Set;
11 
12 public class DataStoreInFile {
13 
14     /**
15      * write
16      */
17     public void objectWriter(Map<String,Employee> m) {
18         try {
19             File f = new File("D:\\Employee.txt");
20             FileOutputStream fo=new FileOutputStream(f);
21             ObjectOutputStream oos = new ObjectOutputStream(fo);
22             oos.writeObject(m);
23             oos.flush();
24             oos.close();
25             System.out.println("Save Success");
26         } catch (Exception e) {
27             e.printStackTrace();
28         }
29     }
30 
31     /**
32      * reader
33      */
34     public  void objectReader() {
35         try {
36             File f = new File("D:\\Employee.txt");
37             ObjectInputStream ois = new ObjectInputStream(
38                     new FileInputStream(f));
39 
40             Map<String, Employee> m = (Map<String,Employee>) ois.readObject();
41             
42             Set <String> key=m.keySet();
43            /* for(String k:key){
44                 Employee emp=m.get(k);
45                 System.out.println(emp.getId()+" "+emp.getName()+" "+emp.getAddress()+" "+emp.getAge()+" "+emp.getDepartment()+" "+emp.getGrade()+" "+emp.getBasic());
46             }*/
47             
48             Iterator<String> it = key.iterator();
49             while(it.hasNext()){
50                 String em =  it.next();
51                 Employee emp=(Employee)m.get(em);
52                 System.out.println(emp.getId()+" "+emp.getName()+" "+emp.getAddress()+" "+emp.getAge()+" "+emp.getDepartment()+" "+emp.getGrade()+" "+emp.getBasic());
53             }
54             
55             ois.close();
56         } catch (Exception e) {
57             e.printStackTrace();
58         }
59         System.out.println("File Reading Success");
60     }
61     
62     public  void objectSearch(String name) {
63         try {
64             File f = new File("D:\\Employee.txt");
65             ObjectInputStream ois = new ObjectInputStream(
66                     new FileInputStream(f));
67 
68             Map<String, Employee> m = (Map<String,Employee>) ois.readObject();
69             Boolean find=false;
70             Set <String> key=m.keySet();
71             for(String k:key){
72                 if(name.equals(k)){
73                    Employee emp=m.get(k);
74                 System.out.println(emp.getId()+" "+emp.getName()+" "+emp.getAddress()+" "+emp.getAge()+" "+emp.getDepartment()+" "+emp.getGrade()+" "+emp.getBasic());
75                 find=true;
76                 break;
77                 }
78                 }
79             if(find==false)
80                 System.out.println("There has no such Person!");
81             ois.close();
82         } catch (Exception e) {
83             e.printStackTrace();
84         }
85     }
86 
87 }

 

 1 package Aug14.IO;
 2 
 3 import java.util.Map;
 4 import java.util.Scanner;
 5 import java.util.TreeMap;
 6 
 7 public class TestEmployee {
 8     
 9     Scanner reader=new Scanner(System.in);
10     
11     public static void main(String[] args) {
12         TestEmployee tm=new TestEmployee();
13         String choice="0";
14         
15         while(true){
16             tm.menu();
17             choice=tm.reader.next();
18             switch(choice){
19             case "a":tm.create();break;
20             case "b":tm.search();break;
21             case "c":tm.add();break;
22             case "d":tm.print();break;
23             case "e":tm.exit();break;
24             default: System.out.println("Please enter the right choice! Try again:");
25             main(args);
26             break;
27             }
28             
29         }
30     }
31 
32     private void  menu(){
33         System.out.println("\n---------------------------------");
34         System.out.println("---    Create  ----------a   ----");
35         System.out.println("---    Search  ----------b   ----");
36         System.out.println("---    Add     ----------c   ----");
37         System.out.println("---    Print   ----------d   ----");
38         System.out.println("---    Exit    ----------e   ----");
39         System.out.println("---------------------------------");
40         System.out.print("Enter your choice:");
41     }
42     private void exit() {
43       System.out.println("Welcome to use again!");
44         System.exit(0);
45     }
46 
47     private void search() {
48          System.out.println("Enter the name you want to search:");
49          String name=reader.next();
50          DataStoreInFile ds=new DataStoreInFile();
51          ds.objectSearch(name);
52     }
53 
54     private void add() {
55         
56          DataStoreInFile ds=new DataStoreInFile();
57          ds.objectWriter(map); 
58          //map.clear();  //after add, clear map
59         
60     }
61 
62     private void print() {
63          DataStoreInFile ds=new DataStoreInFile();
64          ds.objectReader();
65         
66     }
67     
68     TreeMap<String, Employee> map=new TreeMap();
69     
70     private   void  create() {
71          System.out.println("Enter name:");
72          String name=reader.next();
73          System.out.println("Enter ID:");
74          int id=reader.nextInt();
75          System.out.println("Enter age:");
76          int age=reader.nextInt();
77          System.out.println("Enter address:");
78          String address=reader.next();
79          System.out.println("Enter department:");
80          String dep=reader.next();
81          System.out.println("Enter grade:");
82          String grade=reader.next();
83          System.out.println("Enter basic:");
84          int  basic=reader.nextInt();
85          Employee emp = new Employee(id,name,age,address, dep, grade, basic);
86          map.put(name,emp);
87     }
88 
89 }
90 

 

Serializable,布布扣,bubuko.com

Serializable

标签:style   blog   color   java   os   io   for   ar   

原文地址:http://www.cnblogs.com/thrive/p/3915548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!