Untitled

 avatar
fttk
plain_text
a year ago
3.1 kB
0
Indexable
Never
package main;

import java.util.ArrayList;
import java.util.HashMap;

import main.Solution.Result;



public class UserSolution {
	
	HashMap<String, ArrayList<Employee>> hm[];
	
    public void InitDB() {
    	hm = new HashMap[5];
        for(int i=0; i<5; i++) {
        	hm[i] = new HashMap<>();
        }        
    }

    public void Add(String name, String number, String birthday, String email, String memo) {
    	Employee e=new Employee(name,number,birthday,email,memo);
    	
    	for(int i=0; i<5; i++) {
            ArrayList<Employee> list = hm[i].get(e.value[i]);
             
            if(list == null) {
                list = new ArrayList<>();
                hm[i].put(e.value[i], list);
            }
            list.add(e);
        }
    }

    public int Delete(int field, String str) {
    	int count = 0;
        
        ArrayList<Employee> list = hm[field].get(str);
        if(list != null) {
            for(Employee record : list) {
                if(record.isDel == false) {
                    record.isDel = true;
                    count++;
                }
            }
        }
        return count;
    }

    public int Change(int field, String str, int changefield, String changestr) {
    	Employee newRecord = null;
        int count = 0;
        ArrayList<Employee> listRecord = hm[field].get(str);
        if(listRecord != null) {
            int size = listRecord.size();
            for(int i=0; i<size; i++) {
                Employee rc = listRecord.get(i);
                if(rc.isDel == false) {
                    newRecord = rc;
                    rc.isDel = true;
                    newRecord.value[changefield] = changestr;
                    count++;
                    Add(newRecord.value[0], newRecord.value[1], newRecord.value[2], newRecord.value[3], newRecord.value[4]);//Them ban ghi voi gia tri moi vao hashmap
                }
            }
        }
        return count;
    }

    public Result Search(int field, String str, int returnfield) {
    	Solution.Result s = new Solution.Result();
        s.count = 0;
        s.str = "";
         
        ArrayList<Employee> listRecord = hm[field].get(str);
        if(listRecord != null) {
            for(Employee record : listRecord) {
				if(record.isDel == false) {
                    s.count++;
                    s.str = record.value[returnfield];
                }
            }
        }
         
        if(s.count > 1)
            s.str = "";
        return s;
    }
    static class Employee {
    	public String[] value;
    	boolean isDel;
    	
    	public Employee(String name, String number, String birthday, String email, String memo) {
    		this.value = new String[5];
            this.value[0] = name;
            this.value[1] = number;
            this.value[2] = birthday;
            this.value[3] = email;
            this.value[4] = memo;
            this.isDel = false;
    	}
    	
    }
}