-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests refactored, exception messages added
- Loading branch information
Showing
4 changed files
with
47 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 15 additions & 13 deletions
28
src/main/java/ru/faang/school/hashmap/task_1/HouseBook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,47 @@ | ||
package ru.faang.school.hashmap.task_1; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
@Getter | ||
|
||
@RequiredArgsConstructor | ||
public class HouseBook { | ||
|
||
private final Map<String, House> houses = new HashMap<>(); | ||
private final Map<String, HouseAttributes> houses = new HashMap<>(); | ||
|
||
public String addNewHouse(String houseName, String houseMotto, String houseSigil){ | ||
if (houses.containsKey(houseName)){ | ||
throw new HouseBookException("The house " + houseName + " already exists in the book."); | ||
throw new HouseBookException(String.format(Message.HOUSE_ALREADY_EXISTS, houseName)); | ||
} | ||
houses.put(houseName, new House(houseMotto, houseSigil)); | ||
return "A house " + houseName + " added. There are " + houses.size() + " in the list so far."; | ||
houses.put(houseName, new HouseAttributes(houseMotto, houseSigil)); | ||
return String.format(Message.HOUSE_ADDED, houseName, houses.size()); | ||
} | ||
|
||
public String deleteHouse(String houseName){ | ||
if (houses.containsKey(houseName)){ | ||
houses.remove(houseName); | ||
return "A house " + houseName + " deleted. There are " + houses.size() + " in the list so far."; | ||
return String.format(Message.HOUSE_DELETED, houseName, houses.size()); | ||
} | ||
throw new HouseBookException("The house " + houseName + " doesn't exist in the book."); | ||
throw new HouseBookException(String.format(Message.HOUSE_DOESNT_EXIST, houseName)); | ||
} | ||
|
||
public String getHouseSigil(String houseName){ | ||
if (houses.containsKey(houseName)){ | ||
return houses.get(houseName).sigil; | ||
} | ||
throw new HouseBookException("The house " + houseName + " doesn't exist in the book."); | ||
throw new HouseBookException(String.format(Message.HOUSE_DOESNT_EXIST, houseName)); | ||
} | ||
|
||
public void getAllHouses(){ | ||
if (houses.isEmpty()){ | ||
throw new HouseBookException("The housebook is empty for now :("); | ||
throw new HouseBookException(Message.HOUSEBOOK_IS_EMPTY); | ||
} | ||
for (Map.Entry<String, House> entry : houses.entrySet()){ | ||
System.out.println("House name is " + entry.getKey() + ", " + entry.getValue()); | ||
for (Map.Entry<String, HouseAttributes> entry : houses.entrySet()){ | ||
System.out.printf((Message.HOUSE_INFO) + "%n", entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
public int getHouseBookSize(){ | ||
return houses.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ru.faang.school.hashmap.task_1; | ||
|
||
public class Message { | ||
|
||
public static final String HOUSE_DOESNT_EXIST = "The house %s doesn't exist in the book."; | ||
public static final String HOUSE_ALREADY_EXISTS = "The house %s already exists in the book."; | ||
public static final String HOUSE_DELETED = "A house %s deleted from the book. Houses in the book: %d."; | ||
public static final String HOUSEBOOK_IS_EMPTY = "The housebook is empty for now :("; | ||
public static final String HOUSE_INFO = "House name is %s, %s."; | ||
public static final String HOUSE_ADDED = "A house %s added to the book. Houses in the book: %d."; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Я бы здесь тоже использовал форматирование сообщений из класса Message
Потому что иначе мы снова эти строчки дублируем) String.format поможет собрать реальную строку для проверки