to toonycheng

来源:互联网 发布:闺蜜网络语 编辑:程序博客网 时间:2024/04/30 08:45
 User类:
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package wind.angel.app.entry;
  6. /**
  7.  *
  8.  * @author Hawkeyes
  9.  */
  10. public class User implements Cloneable{
  11.     
  12.     private String id;
  13.     private String name;
  14.     private String password;
  15.     public String getId() {
  16.         return id;
  17.     }
  18.     public boolean isInit() {
  19.         if(id.equals("") && name.equals("") && password.equals(""))
  20.             return true;
  21.         return false;
  22.     }
  23.     public void setId(String id) {
  24.         this.id = id;
  25.     }
  26.     public String getName() {
  27.         return name;
  28.     }
  29.     public void setName(String name) {
  30.         this.name = name;
  31.     }
  32.     public String getPassword() {
  33.         return password;
  34.     }
  35.     public void setPassword(String password) {
  36.         this.password = password;
  37.     }
  38.     public User() {
  39.         this.id = "";
  40.         this.name = "";
  41.         this.password = "";
  42.     }
  43.     public User(String id, String name, String password) {
  44.         this.id = id;
  45.         this.name = name;
  46.         this.password = password;
  47.     }
  48.     @Override
  49.     public String toString() {
  50.         return "id: " + id + " name: " + name + " password: " + password + ".";
  51.     }
  52.     @Override
  53.     protected Object clone() throws CloneNotSupportedException {
  54.         return super.clone();
  55.     }
  56. }

Manager类:
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package wind.angel.app.util;
  6. import java.util.ArrayList;
  7. import wind.angel.app.entry.User;
  8. /**
  9.  *
  10.  * @author Hawkeyes
  11.  */
  12. public class Manager {
  13.     
  14.     private ArrayList<User> userlist;
  15.     private User user = new User();
  16.     public Manager() {
  17.         userlist = new ArrayList<User>();
  18.     }
  19.     public Manager(ArrayList<User> userlist) {
  20.         this.userlist = userlist;
  21.     }
  22.     public void initUser(User u){
  23.         u=null;
  24.     }
  25.     public void list() {
  26.         for(User u:userlist){
  27.             System.out.println(u);
  28.         }
  29.     }
  30.     
  31.     public User searchById(String id){
  32.         initUser(user);
  33.         for(User u:userlist){
  34.             if(u.getId().equals(id)){
  35.                 user=u;
  36.                 break;
  37.             }
  38.         }
  39.         return user;
  40.     }
  41.     
  42.     public User searchByName(String name){
  43.         initUser(user);
  44.         for(User u:userlist){
  45.             if(u.getName().equals(name)){
  46.                 user=u;
  47.                 break;
  48.             }
  49.         }
  50.         return user;
  51.     }
  52.     
  53.     public boolean add(User u){
  54.         return userlist.add(u);
  55.     }
  56.     
  57.     public boolean remove(User u){
  58.         return userlist.remove(u);
  59.     }
  60. }
Main:

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package wind.angel.app;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import wind.angel.app.entry.User;
  12. import wind.angel.app.util.Manager;
  13. /**
  14.  *
  15.  * @author Hawkeyes
  16.  */
  17. public class Main {
  18.     static private boolean flag = true;
  19.     static String str;
  20.     static InputStreamReader isr = new InputStreamReader(System.in);
  21.     static BufferedReader br = new BufferedReader(isr);
  22.     static private User user = new User();
  23.     static private Manager manager = new Manager();
  24.     /**
  25.      * @param args the command line arguments
  26.      */
  27.     public static void main(String[] args) throws IOException {
  28.         // TODO code application logic here
  29.         while (flag) {
  30.             System.out.println("------------------------------------------------");
  31.             System.out.println("Please select the function:");
  32.             System.out.println("1.    Add user");
  33.             System.out.println("2.    Search user");
  34.             System.out.println("3.    Delete user");
  35.             System.out.println("4.    List user");
  36.             System.out.println("exit. exit app");
  37.             System.out.println("press the number of function: ");
  38.             str = br.readLine();
  39.             //System.out.println(str);
  40.             if (str.equals("exit")) {
  41.                 break;
  42.             } else if (str.equals("1")) {
  43.                 addUser();
  44.             } else if (str.equals("2")) {
  45.                 searchUser();
  46.             } else if (str.equals("3")) {
  47.                 deleteUser();
  48.             } else if (str.equals("4")) {
  49.                 listUser();
  50.             } else {
  51.                 System.out.println("The function code is invalid.");
  52.             }
  53.             continue;
  54.         }
  55.     }
  56.     private static void addUser() {
  57.         String[] strarr;
  58.         System.out.println("press user information by '<id> <name> <password>'.");
  59.         try {
  60.             str = br.readLine();
  61.         } catch (IOException ex) {
  62.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  63.         }
  64.         strarr = str.split(" ");
  65.         //user.setId(strarr[0]);
  66.         //user.setName(strarr[1]);
  67.         //user.setPassword(strarr[2]);
  68.         if (manager.add(new User(strarr[0],strarr[1],strarr[2]))) {
  69.             System.out.println("Add success.");
  70.         } else {
  71.             System.out.println("Add fail.");
  72.         }
  73.     }
  74.     private static void deleteUser() {
  75.         String[] sa;
  76.         System.out.println("usage: <id|name> <value>.");
  77.         try {
  78.             str = br.readLine();
  79.         } catch (IOException ex) {
  80.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  81.         }
  82.         sa = str.split(" ");
  83.         if (sa[0].equals("id")) {
  84.             user = manager.searchById(sa[1]);
  85.         } else if (sa[0].equals("name")) {
  86.             user = manager.searchByName(sa[1]);
  87.         }
  88.         if(user.isInit()){
  89.             System.out.println("The user is not found.");
  90.             return;
  91.         }
  92.         if(manager.remove(user))
  93.             System.out.println("the user " + user.getName() + " has deleted.");
  94.         else
  95.             System.out.println("can't delete the user " + user.getName() + ".");
  96.     }
  97.     private static void listUser() {
  98.         manager.list();
  99.     }
  100.     private static void searchUser() {
  101.         String[] sa;
  102.         System.out.println("usage: <id|name> <value>.");
  103.         try {
  104.             str = br.readLine();
  105.         } catch (IOException ex) {
  106.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  107.         }
  108.         sa = str.split(" ");
  109.         if (sa[0].equals("id")) {
  110.             user = manager.searchById(sa[1]);
  111.         } else if (sa[0].equals("name")) {
  112.             user = manager.searchByName(sa[1]);
  113.         }
  114.         if(user.isInit()){
  115.             System.out.println("The user is not found.");
  116.             return;
  117.         }
  118.         System.out.println(user);
  119.     }
  120. }