aboutsummaryrefslogtreecommitdiff
path: root/Week9 Webshop/src/com/camilstaps/shop/UserInteraction.java
blob: 1140089f19043f2a961f669401f75b7b5dc0c0b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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)];
    }

}