forked from srico/StockPriceJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest6.java
More file actions
289 lines (252 loc) · 7.05 KB
/
test6.java
File metadata and controls
289 lines (252 loc) · 7.05 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import java.util.Scanner;
import java.util.Stack;
import javax.swing.JOptionPane;
public class test6 {
static Stack myStack = new Stack ();
static Scanner in = new Scanner(System.in) ;
public static void main(String[] args) {
String myStockName = null ;
int numNeeded = 0 ;
int menuSelection = 0 ;
int myStockNum = 0 ;
double myStockPrice = 0 ;
Stock myStock = null ;
populateStack();
while(menuSelection != 4)
{
System.out.println("Welcome to the Stock-Trading Menu!");
System.out.println("Press 1 to enter a new stock");
System.out.println("Press 2 to find the FIFO and LIFO price for a stock");
System.out.println("Press 3 to display all stocks");
System.out.println("Press 4 to quit");
menuSelection = inputGuard(1,4);
if (menuSelection ==1)
{
System.out.println("You have chosen to add new stock");
myStockName = chooseStock();
System.out.println("Stock selected was " + myStockName);
System.out.println("Enter the number of stocks you want to add");
myStockNum = inputInteger();
System.out.println("Enter the individual price of the stocks you want to add");
myStockPrice = inputDouble();
myStack.push(new Stock(myStockName,myStockNum,myStockPrice) );
System.out.println();
}
else if (menuSelection == 2)
{
System.out.println("You have chosen to find the FIFO and LIFO price for a stock");
myStockName = chooseStock();
System.out.println("Stock selected was " + myStockName);
System.out.println("Enter number of shares");
numNeeded = inputInteger();
doFIFOCost(numNeeded,myStockName);
doLIFOCost(numNeeded,myStockName);
System.out.println();
}
else if (menuSelection ==3)
{
System.out.println("You have chosen to display all stocks");
int size = myStack.size();
System.out.println();
System.out.printf("%-20s %-20s %-20s \n", "Stock Name" , "Number of Shares", "Purchase Price");
System.out.println("--------------------------------------------------------");
for (int i=0 ; i < size ; i++)
{
myStock = (Stock) myStack.get(i);
System.out.printf("%-20s %-20d %-18.2f \n", myStock.getName(), myStock.getNumberShares(), myStock.getPrice());
}
System.out.println();
}
else
{
System.out.println("Bye!");
break;
}
}
}
public static void doFIFOCost(int numStocksNeeded, String StockName)
{
int i = 0 ;
int numNeeded = numStocksNeeded ;
String myStockName = StockName ;
int numStocks = myStack.size();
Stock myStock = null ;
double myPrice ;
int stocksAvail = 0 ;
double stockValueSum = 0 ;
int numStocksAdded = 0 ;
double FIFOCost = 0 ;
while (numNeeded !=0 & (i < numStocks))
{
myStock = (Stock) myStack.get(i) ;
if (myStock.getName().equalsIgnoreCase(myStockName))//if1
{
stocksAvail = myStock.getNumberShares();
myPrice = myStock.getPrice();
if (stocksAvail >= numNeeded)//if2
{
stockValueSum += numNeeded*myPrice ;
numStocksAdded += numNeeded ;
numNeeded = 0 ;
}//end if2
else
{
stockValueSum += stocksAvail*myPrice ;
numNeeded -= stocksAvail ;
numStocksAdded += stocksAvail ;
}
}//end if1
i++ ;
}//end of while
FIFOCost = stockValueSum/(double) numStocksAdded ;
if (numStocksAdded < numNeeded)
System.out.printf("There were only %10d %12s stocks available. The FIFO cost is %10f",numStocksAdded,myStockName,FIFOCost);
else
System.out.printf("The FIFO cost of %10d %12s stocks is %10.2f ", numStocksAdded,myStockName,FIFOCost);
System.out.println();
}
public static void doLIFOCost(int numStocksNeeded, String StockName)
{
int numStocks = myStack.size();
int i = numStocks - 1 ;
int numNeeded = numStocksNeeded ;
String myStockName = StockName ;
Stock myStock = null ;
double myPrice ;
int stocksAvail = 0 ;
double stockValueSum = 0 ;
int numStocksAdded = 0 ;
double LIFOCost = 0 ;
while (numNeeded !=0 & (i >= 0))
{
myStock = (Stock) myStack.get(i) ;
if (myStock.getName().equalsIgnoreCase(myStockName))//if1
{
stocksAvail = myStock.getNumberShares();
myPrice = myStock.getPrice();
if (stocksAvail >= numNeeded)//if2
{
stockValueSum += numNeeded*myPrice ;
numStocksAdded += numNeeded ;
numNeeded = 0 ;
}//end if2
else
{
stockValueSum += stocksAvail*myPrice ;
numNeeded -= stocksAvail ;
numStocksAdded += stocksAvail ;
}
}//end if1
i-- ;
}//end of while
LIFOCost = stockValueSum/(double) numStocksAdded ;
if (numStocksAdded < numNeeded)
System.out.printf("There were only %10d %12s stocks available. The LIFO cost is %10f",numStocksAdded,myStockName,LIFOCost);
else
System.out.printf("The LIFO cost of %10d %12s stocks is %10.2f ", numStocksAdded,myStockName,LIFOCost);
System.out.println();
}
public static void populateStack()
{
Stock Google1 = new Stock("Google", 20, 25);
Stock Google2 = new Stock("Google", 10, 40);
Stock Google3 = new Stock("Google", 15, 50);
Stock Apple1 = new Stock("Apple", 40, 10);
Stock Apple2 = new Stock("Apple", 30, 50);
Stock IBM1 = new Stock("IBM", 36, 20);
Stock IBM2 = new Stock("IBM", 30, 40);
Stock Intel1 = new Stock("Intel", 26, 70);
Stock Intel2 = new Stock("Intel", 38, 58);
Stock Google4 = new Stock("Google", 30, 80);
//Load myStack
myStack.push(Google1);
myStack.push(Google2);
myStack.push(Google3);
myStack.push(Apple1);
myStack.push(Apple2);
myStack.push(IBM1);
myStack.push(IBM2);
myStack.push(Intel1);
myStack.push(Intel2);
myStack.push(Google4);
}
public static String chooseStock()
{
int selection = 0 ;
String stockName = null ;
System.out.println("Press 1 to select Google stock");
System.out.println("Press 2 to select Apple stock");
System.out.println("Press 3 to select Facebook stock");
System.out.println("Press 4 to select Intel stock");
selection = inputGuard(1,4);
switch (selection){
case 1 : {
stockName = "Google";
break ;
}
case 2 : {
stockName = "Apple";
break;
}
case 3 : {
stockName = "Facebook";
break;
}
case 4 : {
stockName = "Intel";
break ;
}
}//end of switch
return stockName ;
}
public static int inputGuard ( int lower, int upper)
{
int selection = 0 ;
boolean notHappy = true ;
int myLower = lower ;
int myUpper = upper ;
while (notHappy){
if (!(in.hasNextInt())){
JOptionPane.showMessageDialog(null, "You did not enter an integer");
in.nextLine();
}// end first if
else {
selection = in.nextInt();
if (!(((selection<=myUpper)&(selection>=myLower)))) {
JOptionPane.showMessageDialog(null, "The integer you entered is not from the menu");
in.nextLine();
}//end second if
else {
notHappy = false ;
in.nextLine();
}//end second else
}//end first else
}//end while
notHappy = true ;
return selection ;
}
public static int inputInteger()
{
int inputInt = 0 ;
while (!(in.hasNextInt()))
{
JOptionPane.showMessageDialog(null, "You did not enter an integer");
in.nextLine();
}
inputInt = in.nextInt();
in.nextLine();
return inputInt ;
}
public static double inputDouble()
{
double inputDouble = 0 ;
while (!(in.hasNextDouble()))
{
JOptionPane.showMessageDialog(null, "You did not enter a double");
in.nextLine();
}
inputDouble = in.nextInt();
in.nextLine();
return inputDouble ;
}
}