-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPhase1.java
77 lines (62 loc) · 2.12 KB
/
TestPhase1.java
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
public class TestPhase1
{
public static void main(String[] args)
{
//Testing the Stock class:
Stock s1 = new Stock("GameStonk", "GMS", 400);
System.out.println(s1);
s1.changeCurrentPrice(350);
System.out.println(s1);
s1.changeCurrentPrice(360);
System.out.println(s1);
System.out.println();
//Testing the StockTransaction class:
OwnedStock st1 = new OwnedStock(10, s1, 360);
System.out.println(st1);
System.out.printf("Profit = $%.2f\n", st1.getProfit());
System.out.println();
s1.changeCurrentPrice(350);
System.out.println(s1);
System.out.printf("Profit = $%.2f\n", st1.getProfit());
System.out.println();
System.out.println("After buying 10 more shares:");
double cost = st1.addShares(10, s1.getCurrentPrice());
System.out.printf("The cost of the transaction: %.2f\n", cost);
System.out.println(st1);
System.out.printf("Profit = $%.2f\n", st1.getProfit());
System.out.println();
s1.changeCurrentPrice(400);
System.out.println(s1);
System.out.println(st1);
System.out.printf("Profit = $%.2f\n", st1.getProfit());
System.out.println();
System.out.println("After selling 15 shares:");
double moneyMade = st1.sellShares(15, s1.getCurrentPrice());
System.out.printf("Received this much from the sale: %.2f\n", moneyMade);
System.out.println(st1);
System.out.printf("Profit = $%.2f\n", st1.getProfit());
//Testing short selling:
System.out.println("\nTesting short selling:");
OwnedStock st2 = new OwnedStock(-100, s1, 400);
System.out.println(st2);
s1.changeCurrentPrice(100);
System.out.println(s1);
System.out.printf("Profit = $%.2f\n", st2.getProfit());
//Testing the two new types of exceptions:
System.out.println();
try{
throw new InvalidTransactionException("Problem with a transaction!");
}
catch(InvalidTransactionException ite)
{
System.out.println(ite.getMessage());
}
try{
throw new InsufficientFundsException("Not enough funds!");
}
catch(InvalidTransactionException ite)
{
System.out.println(ite.getMessage());
}
}
}