/** * Copyright (c) 2015 Camil Staps * 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
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 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)]; } }