-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
188 lines (162 loc) · 7.23 KB
/
Main.java
File metadata and controls
188 lines (162 loc) · 7.23 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
// Main.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
public class Main extends MainFrame implements ActionListener {
// array to store all the stockprices based on the user's selections
protected ArrayList<StockPrice> stockprices;
protected ArrayList<Integer> pitches;
protected ReadFile reader;
protected SoundPlayer player;
// the MAIN function
public static void main(String[] args) {
Main main = new Main();
} // end main
public Main() {
super();
stockprices = new ArrayList<>();
reader = new ReadFile();
player = new SoundPlayer();
pitches = new ArrayList<>();
playButton.addActionListener(this);
} // end constructor
// method implemented from the ActionListener interface
@Override
public void actionPerformed(ActionEvent e) {
// check the time selected
String timeSelected;
timeSelected = (String) time.getSelectedItem();
// radio buttons with stock indexes
if (NYSE.isSelected()) {
reader.setTime(timeSelected);
reader.setFilename("./NYSE.csv");
reader.readDataTo(stockprices);
} else if (NASDAQ.isSelected()) {
reader.setTime(timeSelected);
reader.setFilename("./NASDAQ.csv");
reader.readDataTo(stockprices);
} else if (DJIA.isSelected()) {
reader.setTime(timeSelected);
reader.setFilename("./DJIA.csv");
reader.readDataTo(stockprices);
} else {
System.out.println("No Index Type Selected!");
} // end if
// change the instrument accordingly
String selected;
selected = (String) instruments.getSelectedItem();
if (selected.equals("Piano")) {
player.setInstrument(PIANO);
} else if (selected.equals("Xylophone")) {
player.setInstrument(XYLOPHONE);
} else if (selected.equals("Harmonica")) {
player.setInstrument(HARMONICA);
} else if (selected.equals("Guitar")) {
player.setInstrument(GUITAR);
} else if (selected.equals("Violin")) {
player.setInstrument(VIOLIN);
} else if (selected.equals("Flute")) {
player.setInstrument(FLUTE);
} // end if
// set the value to play for a stockprice equal to the selection made by the user
String priceType;
priceType = (String) priceToWatch.getSelectedItem();
for (int i = 0; i < stockprices.size(); i++) {
if (priceType.equals("Open Price")) {
stockprices.get(i).setValueToPlay(stockprices.get(i).getOpenPrice() * 100);
} else if (priceType.equals("High Price")) {
stockprices.get(i).setValueToPlay(stockprices.get(i).getHighPrice() * 100);
} else if (priceType.equals("Low Price")) {
stockprices.get(i).setValueToPlay(stockprices.get(i).getLowPrice() * 100);
} else if (priceType.equals("Close Price")) {
stockprices.get(i).setValueToPlay(stockprices.get(i).getClosePrice() * 100);
} // end if
} // end for
// find maximum and minimum prices of the existing data
float maxNum;
float minNum;
maxNum = getMaxNum();
minNum = getMinNum();
// find the range of the existing data
float rangeFrom = maxNum - minNum;
Graph myGraph = new Graph(minNum, maxNum, rangeFrom, stockprices);
/*
Algorithm to convert data from one range to another:
output = output_start + ((output_end - output_start) / (input_end - input_start)) * (input - input_start)
Or, think about it this way
slope = (output_end - output_start) / (input_end - input_start)
output = output_start + slope * (input - input_start)
*/
// make sure the pitches array is empty before adding any elements to it
if (!pitches.isEmpty()) {
pitches.clear();
} // end if
float rangeTo = 97; // range of the notes that can be played on the MIDI, first note that will be played is 10
int priceCasted; // number that will be added to the pitches dictionary as the price
float numConverted;
float slope;
slope = rangeTo/rangeFrom;
// convert each data value in the arrayList to correspond to new values in the new range
for (int i = 0; i < stockprices.size(); i++) {
// calculation to convert the number to the 30-127 range (audible)
numConverted = 30 + slope * (stockprices.get(i).getValueToPlay() - minNum);
priceCasted = (int) numConverted;
// make sure the pitches array is Empty before this data is played
pitches.add(priceCasted);
} // end for
try {
Thread.sleep(1000); // wait for 1 second
} catch (Exception ex) {
System.out.println(ex.getMessage());
} // end try-catch
// invoke this later so that it runs in the end after the graph is displayed
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// the pitches arrayList is now populated with the pitches to play
// now play the sound accordingly
System.out.println("Instrument: " + player.getInstrument());
for (int i = 0; i < pitches.size(); i++) {
player.setPitch(pitches.get(i));
player.playSound();
System.out.println("Date: " + stockprices.get(i).getDate());
System.out.println("Pitch: " + pitches.get(i) + " -----> Price: $" + stockprices.get(i).getValueToPlay()/100);
System.out.println("Number of Days: " + i + "\n\n");
} // end for
} // end run
});
} // end actionPerformed
// other methods
public float getMaxNum() {
float currentPrice;
float maxNum = stockprices.get(0).getValueToPlay(); // set zeroeth term as the initial max
for (int i = 0; i < stockprices.size(); i++) {
currentPrice = stockprices.get(i).getValueToPlay();
if (currentPrice > maxNum) {
maxNum = currentPrice;
} // end if
} // end for
return maxNum;
} // end getMaxNum
public float getMinNum() {
float currentPrice;
float minNum = stockprices.get(0).getValueToPlay(); // set zeroeth term as the initial min
for (int i = 1; i < stockprices.size(); i++) {
currentPrice = stockprices.get(i).getValueToPlay();
if (currentPrice < minNum) {
minNum = currentPrice;
} // end if
} // end for
return minNum;
} // end getMinNum
// constants representing different midi instruments
public static int
PIANO = 0,
XYLOPHONE = 13,
HARMONICA = 22,
GUITAR = 24,
VIOLIN = 40,
TRUMPET = 56,
FLUTE = 73;
} // end class def