-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockOptimization.java
More file actions
145 lines (124 loc) · 5.38 KB
/
Copy pathStockOptimization.java
File metadata and controls
145 lines (124 loc) · 5.38 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Stock {
String companyName;
double ebitda;
double revenueGrowth;
double marketCap;
double price;
double cost;
double weightedScore;
public Stock(String companyName, double ebitda, double revenueGrowth, double marketCap, double price, double cost) {
this.companyName = companyName;
this.ebitda = ebitda;
this.revenueGrowth = revenueGrowth;
this.marketCap = marketCap;
this.price = price;
this.cost = cost;
this.weightedScore = 0;
}
public void calculateWeightedScore(double w1, double w2, double w3, double w4) {
this.weightedScore = w1 * this.ebitda + w2 * this.revenueGrowth + w3 * this.marketCap - w4 * this.price;
}
}
public class StockOptimization {
private static double parseDouble(String str) {
try {
return Double.parseDouble(str);
} catch (NumberFormatException e) {
return 0.0;
}
}
private static void generateCSVOutput(List<Stock> selectedStocks, double totalCost) {
String csvFile = "selected_stocks.csv";
try (PrintWriter writer = new PrintWriter(new FileWriter(csvFile))) {
// Write CSV header
writer.println("Stock #,Company Name,Cost ($)");
// Write each selected stock to CSV
for (int i = 0; i < selectedStocks.size(); i++) {
Stock stock = selectedStocks.get(i);
if (stock.cost == 0.00)
continue;
writer.printf("%d,%s,%.2f%n", i + 1, stock.companyName, stock.cost);
}
// Write total cost at the end
writer.println();
writer.printf("Total Cost,,%.2f%n", totalCost);
System.out.println("CSV file generated successfully: " + csvFile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the weight for EBITDA (0-1):");
double w1 = scanner.nextDouble();
System.out.println("Enter the weight for Revenue Growth (0-1):");
double w2 = scanner.nextDouble();
System.out.println("Enter the weight for Market Cap (0-1):");
double w3 = scanner.nextDouble();
System.out.println("Enter the weight for minimizing Price (0-1):");
double w4 = scanner.nextDouble();
List<Stock> stocks = new ArrayList<>();
String csvFile = "stocks_cleaned.csv"; // Update with your file path
// Read data from CSV file
try (BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {
String line;
reader.readLine();
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length >= 6) {
String companyName = fields[2];
double ebitda = parseDouble(fields[1]);
double revenueGrowth = parseDouble(fields[2]);
double marketCap = parseDouble(fields[3]);
double price = parseDouble(fields[4]);
double cost = parseDouble(fields[5]);
Stock stock = new Stock(companyName, ebitda, revenueGrowth, marketCap, price, cost);
stock.calculateWeightedScore(w1, w2, w3, w4);
stocks.add(stock);
} else {
System.out.println("Skipping incomplete row: " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
// Set budget constraint
System.out.println("Enter Your Budget\n");
double budget = scanner.nextDouble();
double totalCost = 0;
List<Stock> selectedStocks = new ArrayList<>();
// Sort stocks by weighted score in descending order
Collections.sort(stocks, (a, b) -> Double.compare(b.weightedScore, a.weightedScore));
// Select stocks based on the highest weighted score, respecting the budget
for (Stock stock : stocks) {
if (totalCost + stock.cost <= budget) {
selectedStocks.add(stock);
totalCost += stock.cost;
}
}
Collections.sort(selectedStocks,(a,b)->Double.compare(a.cost,b.cost));
// Output the results
System.out.println("Optimal Stock Selection:");
System.out.println("+---------+----------------------+------------+");
System.out.println("| Stock # | Company Name | Cost ($) |");
System.out.println("+---------+----------------------+------------+");
for (int i = 0; i < selectedStocks.size(); i++) {
Stock stock = selectedStocks.get(i);
if (stock.cost == 0.00)
continue;
System.out.printf("| %7d | %-20s | %10.2f |\n", i + 1, stock.companyName, stock.cost);
}
System.out.println("+---------+----------------------+------------+");
System.out.println("Total Cost: $" + String.format("%.2f", totalCost));
generateCSVOutput(selectedStocks, totalCost);
}
}