diff options
author | Camil Staps | 2015-04-18 13:44:44 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-18 13:44:44 +0200 |
commit | 6a44b074f0169a1b0f9e92347af929c5e471746e (patch) | |
tree | ae5663fe7c69881bf4ecfedbef99c2505f8ec964 /Week9/src/com/camilstaps/shop/UserInteraction.java | |
parent | Added copyright to docs (diff) |
Reorganised projects
Diffstat (limited to 'Week9/src/com/camilstaps/shop/UserInteraction.java')
-rw-r--r-- | Week9/src/com/camilstaps/shop/UserInteraction.java | 198 |
1 files changed, 0 insertions, 198 deletions
diff --git a/Week9/src/com/camilstaps/shop/UserInteraction.java b/Week9/src/com/camilstaps/shop/UserInteraction.java deleted file mode 100644 index 1140089..0000000 --- a/Week9/src/com/camilstaps/shop/UserInteraction.java +++ /dev/null @@ -1,198 +0,0 @@ -/** - * 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 |