Advertisement
ivanyordanov67

Untitled

Feb 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. package repository;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. public class Repository {
  7.  
  8.  
  9. private Map<Integer, Person> data;
  10.  
  11.  
  12. private int id =0;
  13.  
  14. public Repository() {
  15. this.data = new HashMap<>();
  16. }
  17. public void add(Person person){
  18.  
  19. this.data.put(this.id, person);
  20. id++;
  21.  
  22. }
  23.  
  24. public Person get(int id){
  25.  
  26. Person person = new Person();
  27.  
  28. for (Map.Entry<Integer, Person> entry : data.entrySet()) {
  29. if (entry.getKey()==id){
  30. person=entry.getValue();
  31. }
  32. }
  33. return person;
  34. }
  35. public boolean update(int id, Person newPerson){
  36. boolean idExist = true;
  37. try {
  38. Person person = get(id);
  39. person = new Person();
  40. data.put(id, newPerson);
  41.  
  42. }catch (Exception e){
  43. idExist=false;
  44. }
  45. return idExist;
  46.  
  47. }
  48. public boolean delete(int id){
  49. boolean idExist = true;
  50. try {
  51. data.remove(id);
  52.  
  53. }catch (Exception e){
  54. idExist=false;
  55. }
  56. return idExist;
  57. }
  58.  
  59. public int getCount(){
  60.  
  61. return data.size();
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement