Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package repository;
- import java.util.HashMap;
- import java.util.Map;
- public class Repository {
- private Map<Integer, Person> data;
- private int id =0;
- public Repository() {
- this.data = new HashMap<>();
- }
- public void add(Person person){
- this.data.put(this.id, person);
- id++;
- }
- public Person get(int id){
- Person person = new Person();
- for (Map.Entry<Integer, Person> entry : data.entrySet()) {
- if (entry.getKey()==id){
- person=entry.getValue();
- }
- }
- return person;
- }
- public boolean update(int id, Person newPerson){
- boolean idExist = true;
- try {
- Person person = get(id);
- person = new Person();
- data.put(id, newPerson);
- }catch (Exception e){
- idExist=false;
- }
- return idExist;
- }
- public boolean delete(int id){
- boolean idExist = true;
- try {
- data.remove(id);
- }catch (Exception e){
- idExist=false;
- }
- return idExist;
- }
- public int getCount(){
- return data.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement