-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment03.java
More file actions
83 lines (67 loc) · 2.75 KB
/
Assignment03.java
File metadata and controls
83 lines (67 loc) · 2.75 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
// CSE 110 : <Spring 2021>
// Assignment : <assignment #3>
// Author : <Erik Christian Gotta> & <1222628953>
// Description : <Code that computes whether you should buy, sell or a hold stock and how much>
package Assignment03;
import java.util.Scanner;
public class Assignment03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//input collection
System.out.print("Current Shares : ");
int currentShares = sc.nextInt();
System.out.print("Purchase Price : ");
int purchasePrice = sc.nextInt();
System.out.print("Market Price : ");
int marketPrice = sc.nextInt();
System.out.print("Available Funds : ");
int availableFunds = sc.nextInt();
//Math calculations
int transactionFee = 10;
//used in deciding how many shares to buy with explicit conversion
double numberOfSharesToBuy = Math.floor((availableFunds - transactionFee) / marketPrice);
int castNumberOfSharesToBuy = (int) numberOfSharesToBuy;
//Currently an un-used variable
double totalBuyCost = transactionFee + marketPrice * numberOfSharesToBuy;
//cost of each share
double perShareBuyValue = purchasePrice - marketPrice;
//we should buy if total value of shares is greater than transaction fee
double totalBuyValue = perShareBuyValue * numberOfSharesToBuy;
//sell value must be hired then value paid plus cover transaction fee
double perShareSellValue = marketPrice - purchasePrice;
double totalSellValue = perShareSellValue * currentShares;
//to calculate shares being sold with explicit type cast
double numberOfSharesToSell = Math.floor((perShareSellValue * currentShares) - transactionFee);
int castNumberOfSharesToSell = (int) numberOfSharesToSell;
//Selection Control Statements based off of math above
//if for buying
if ((availableFunds > transactionFee) && (perShareBuyValue >= marketPrice))
{
if (totalBuyValue > transactionFee)
{
System.out.println("Buy " + castNumberOfSharesToBuy + " shares");
}
else
{
System.out.println("Hold shares");
}
}
//else if for selling
else if (marketPrice > perShareSellValue)
{
if (totalSellValue > transactionFee)
{
System.out.println("Sell " + castNumberOfSharesToSell + " shares");
}
else
{
System.out.println("Hold shares");
}
}
else
{
System.out.println("Hold shares");
}
sc.close();
}
}