From acbea0f0f7a9aac2f20c9ba5875af6c09be8e2df Mon Sep 17 00:00:00 2001 From: HoolaBoola Date: Wed, 2 Dec 2020 23:31:55 +0200 Subject: [PATCH] Edited code to fulfill checkstyle requirements --- config/checkstyle/checkstyle.xml | 2 +- .../dao/InMemoryRecommendationDao.java | 60 +++++++------------ .../domain/BookRecommendation.java | 9 ++- .../domain/Recommendation.java | 15 +++-- .../domain/VideoRecommendation.java | 3 +- 5 files changed, 41 insertions(+), 48 deletions(-) diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 92a410e..63a2262 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -18,7 +18,7 @@ - + diff --git a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java index e6d5c8a..b2a5ff0 100644 --- a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java @@ -7,9 +7,8 @@ import recommendation_library.domain.BookRecommendation; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; +import java.util.function.Function; import recommendation_library.domain.TimeMemory; import recommendation_library.domain.Type; @@ -49,27 +48,22 @@ public void editBookRecommendation(String title, String fieldToBeEdited, String } } } - + private void editBookMatchingField(BookRecommendation b, String fieldToBeEdited, String newValue) { - switch (fieldToBeEdited.toLowerCase()){ - case "author": - b.setAuthor(newValue); - break; - case "title": - b.setTitle(newValue); - break; - case "description": - b.setDescription(newValue); - break; - case "isbn": - b.setIsbn(newValue); - break; - case "pagecount": - b.setPageCount(Integer.parseInt(newValue)); - break; - } + fieldToBeEdited = fieldToBeEdited.toLowerCase(); + + Map> map = new HashMap<>(); + + map.put("author", b::setAuthor); + map.put("title", b::setTitle); + map.put("description", b::setDescription); + map.put("isbn", b::setIsbn); + map.put("pagecount", s -> b.setPageCount(Integer.parseInt(s))); + + map.get(fieldToBeEdited).apply(newValue); } + @Override public void deleteBookByTitle(String title) { BookRecommendation toBeRemoved = null; @@ -103,25 +97,15 @@ public void editVideoRecommendation(String title, String fieldToBeEdited, String } } } - + private void editVideoMatchingField(VideoRecommendation v, String fieldToBeEdited, String newValue) { - System.err.println(v.getTitle()); - System.err.println(v.getUrl()); + fieldToBeEdited = fieldToBeEdited.toLowerCase(); + Map> map = new HashMap<>(); + map.put("title", v::setTitle); + map.put("url", v::setUrl); + map.put("description", v::setDescription); System.err.println(fieldToBeEdited); - System.err.println(newValue); - System.err.println(); - System.err.println(); - switch (fieldToBeEdited.toLowerCase()){ - case "title": - v.setTitle(newValue); - break; - case "url": - v.setUrl(newValue); - break; - case "description": - v.setDescription(newValue); - break; - } + map.get(fieldToBeEdited).apply(newValue); } @Override diff --git a/src/main/java/recommendation_library/domain/BookRecommendation.java b/src/main/java/recommendation_library/domain/BookRecommendation.java index 183e942..106829d 100644 --- a/src/main/java/recommendation_library/domain/BookRecommendation.java +++ b/src/main/java/recommendation_library/domain/BookRecommendation.java @@ -27,24 +27,27 @@ public int getPageCount() { return pageCount; } - public void setPageCount(int pageCount) { + public boolean setPageCount(int pageCount) { this.pageCount = pageCount; + return true; } public String getAuthor() { return author; } - public void setAuthor(String author) { + public boolean setAuthor(String author) { this.author = author; + return true; } public String getIsbn() { return isbn; } - public void setIsbn(String isbn) { + public boolean setIsbn(String isbn) { this.isbn = isbn; + return true; } } diff --git a/src/main/java/recommendation_library/domain/Recommendation.java b/src/main/java/recommendation_library/domain/Recommendation.java index 9613fac..8c26796 100644 --- a/src/main/java/recommendation_library/domain/Recommendation.java +++ b/src/main/java/recommendation_library/domain/Recommendation.java @@ -29,40 +29,45 @@ public int getId() { return id; } - public void setId(int id) { + public boolean setId(int id) { this.id = id; + return true; } public String getTitle() { return title; } - public void setTitle(String title) { + public boolean setTitle(String title) { this.title = title; + return true; } public Type getType() { return type; } - public void setType(Type type) { + public boolean setType(Type type) { this.type = type; + return true; } public String getDescription() { return description; } - public void setDescription(String description) { + public boolean setDescription(String description) { this.description = description; + return true; } public String getAddDate() { return addDate; } - public void setAddDate(String addDate) { + public boolean setAddDate(String addDate) { this.addDate = addDate; + return true; } public boolean equals(Recommendation other) { diff --git a/src/main/java/recommendation_library/domain/VideoRecommendation.java b/src/main/java/recommendation_library/domain/VideoRecommendation.java index acd08cb..9e9b1ff 100644 --- a/src/main/java/recommendation_library/domain/VideoRecommendation.java +++ b/src/main/java/recommendation_library/domain/VideoRecommendation.java @@ -23,8 +23,9 @@ public String getUrl() { return url; } - public void setUrl(String newUrl) { + public boolean setUrl(String newUrl) { url = newUrl; + return true; }