diff options
author | Camil Staps | 2015-04-18 00:26:48 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-18 00:26:48 +0200 |
commit | 6dd7405206b2babfe491b59250f4d1d7f78e654b (patch) | |
tree | a5e53ec980a4cbdce038dd0beb3c8fe71fa0e80c /Week9/src/com/camilstaps/shop/UserInteraction.java | |
parent | Added tarball w8 (diff) |
Week9
Diffstat (limited to 'Week9/src/com/camilstaps/shop/UserInteraction.java')
-rw-r--r-- | Week9/src/com/camilstaps/shop/UserInteraction.java | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/Week9/src/com/camilstaps/shop/UserInteraction.java b/Week9/src/com/camilstaps/shop/UserInteraction.java new file mode 100644 index 0000000..a857f85 --- /dev/null +++ b/Week9/src/com/camilstaps/shop/UserInteraction.java @@ -0,0 +1,198 @@ +/** + * Copyright (c) 2015 Camil Staps <info@camilstaps.nl> + * See the LICENSE file for copying permission. + */ + +package com.camilstaps.shop; + +import java.util.Set; + +/** + * Interact with the user: provide and request information. + * @author Camil Staps, s4498062 + */ +public abstract class UserInteraction { + + /** + * Show a String + * @param string + */ + abstract void putString(String string); + + /** + * Show a String with a linefeed + * @param string + */ + void putStringLn(String string) { + putString(string + System.lineSeparator()); + } + + /** + * Get a String as input + * @return + */ + abstract String getString(); + + /** + * Get a String as input, after outputting a question + * @param question + * @return + */ + String getString(String question) { + putString(question); + return getString(); + } + + /** + * Get a String as input, and throw an Exception when it's empty + * @return + * @throws InputRequiredException + */ + String getRequiredString() throws InputRequiredException { + String result = getString(); + if (result.isEmpty()) throw new InputRequiredException(); + return result; + } + + /** + * Get a String as input, after outputting a question, and throw an + * Exception when the input is empty + * @param question + * @return + * @throws InputRequiredException + */ + String getRequiredString(String question) throws InputRequiredException { + putString(question); + return getRequiredString(); + } + + /** + * Get a float as input + * @return + */ + abstract float getFloat(); + + /** + * Get a float as input, after outputting a question + * @param question + * @return + */ + float getFloat(String question) { + putString(question); + return getFloat(); + } + + /** + * Get a boolean as input + * @return + */ + abstract boolean getBoolean(); + + /** + * Get a boolean as input, after outputting a question + * @param question + * @return + */ + boolean getBoolean(String question) { + putString(question); + return getBoolean(); + } + + /** + * Get a Command as input + * @return + */ + abstract Command getCommand(); + + /** + * Let the user choose from an array of options, after outputting a question + * @param question + * @param options + * @return the index of the choice in the options array + */ + abstract int getChoice(String question, String[] options); + + /** + * Let the user choose from a Set of Articles + * @param set + * @return + */ + Article getArticle(Set<Article> set) { + String[] articleNames = new String[set.size()]; + Article[] articles = new Article[set.size()]; + int i = 0; + for (Article a : set) { + articleNames[i] = a.toString(); + articles[i++] = a; + } + return articles[getChoice("Article: ", articleNames)]; + } + + /** + * Let the user choose an Article + * @return + * @throws InputRequiredException + * @throws ItemNotFoundException + */ + Article getArticle() throws InputRequiredException, ItemNotFoundException { + String name = getRequiredString("Name article: "); + return Database.getInstance().getArticle(name); + } + + /** + * Let the user choose an Article from the Articles of a specific User + * @param user + * @return + */ + Article getArticle(User user) { + return getArticle(user.getArticles()); + } + + /** + * Let the user choose a category + * @return + * @throws ItemNotFoundException + */ + Category getCategory() throws ItemNotFoundException { + String[] categories = Database.getInstance().getCategoryNames(); + return Database.getInstance().getCategory(categories[getChoice("Category: ", categories)]); + } + + /** + * Let the user choose a User + * @return + * @throws InputRequiredException + * @throws ItemNotFoundException + */ + User getUser() throws InputRequiredException, ItemNotFoundException { + return Database.getInstance().getUser(getRequiredString("Number user: ")); + } + + /** + * Let the user choose an order + * @return + * @throws InputRequiredException + * @throws ItemNotFoundException + */ + Order getOrder() throws InputRequiredException, ItemNotFoundException { + return getOrder(getUser()); + } + + /** + * Let the user choose an order, from the orders of a specific User + * @param user + * @return + */ + Order getOrder(User user) { + Set<Order> orders = user.getOrders(); + String[] orderStrings = new String[orders.size()]; + Order[] orderObjects = new Order[orders.size()]; + int i = 0; + for (Order o : orders) { + orderStrings[i] = o.toString(); + orderObjects[i++] = o; + } + return orderObjects[getChoice("Order: ", orderStrings)]; + } + +}
\ No newline at end of file |