mirror of
https://github.com/ulfgebhardt/java_stockticker.git
synced 2025-12-12 21:25:52 +00:00
init commit
This commit is contained in:
commit
4bbf784b5e
8
.classpath
Normal file
8
.classpath
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/acm"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>StockTicker</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
12
.settings/org.eclipse.jdt.core.prefs
Normal file
12
.settings/org.eclipse.jdt.core.prefs
Normal file
@ -0,0 +1,12 @@
|
||||
#Tue Jan 26 10:12:01 CET 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
1
Stock Ticker.ump
Normal file
1
Stock Ticker.ump
Normal file
File diff suppressed because one or more lines are too long
73
build.xml
Normal file
73
build.xml
Normal file
@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- You may freely edit this file. See commented blocks below for -->
|
||||
<!-- some examples of how to customize the build. -->
|
||||
<!-- (If you delete it and reopen the project it will be recreated.) -->
|
||||
<!-- By default, only the Clean and Build commands use this build script. -->
|
||||
<!-- Commands such as Run, Debug, and Test only use this build script if -->
|
||||
<!-- the Compile on Save feature is turned off for the project. -->
|
||||
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
|
||||
<!-- in the project's Project Properties dialog box.-->
|
||||
<project name="StockTicker" default="default" basedir=".">
|
||||
<description>Builds, tests, and runs the project StockTicker.</description>
|
||||
<import file="nbproject/build-impl.xml"/>
|
||||
<!--
|
||||
|
||||
There exist several targets which are by default empty and which can be
|
||||
used for execution of your tasks. These targets are usually executed
|
||||
before and after some main targets. They are:
|
||||
|
||||
-pre-init: called before initialization of project properties
|
||||
-post-init: called after initialization of project properties
|
||||
-pre-compile: called before javac compilation
|
||||
-post-compile: called after javac compilation
|
||||
-pre-compile-single: called before javac compilation of single file
|
||||
-post-compile-single: called after javac compilation of single file
|
||||
-pre-compile-test: called before javac compilation of JUnit tests
|
||||
-post-compile-test: called after javac compilation of JUnit tests
|
||||
-pre-compile-test-single: called before javac compilation of single JUnit test
|
||||
-post-compile-test-single: called after javac compilation of single JUunit test
|
||||
-pre-jar: called before JAR building
|
||||
-post-jar: called after JAR building
|
||||
-post-clean: called after cleaning build products
|
||||
|
||||
(Targets beginning with '-' are not intended to be called on their own.)
|
||||
|
||||
Example of inserting an obfuscator after compilation could look like this:
|
||||
|
||||
<target name="-post-compile">
|
||||
<obfuscate>
|
||||
<fileset dir="${build.classes.dir}"/>
|
||||
</obfuscate>
|
||||
</target>
|
||||
|
||||
For list of available properties check the imported
|
||||
nbproject/build-impl.xml file.
|
||||
|
||||
|
||||
Another way to customize the build is by overriding existing main targets.
|
||||
The targets of interest are:
|
||||
|
||||
-init-macrodef-javac: defines macro for javac compilation
|
||||
-init-macrodef-junit: defines macro for junit execution
|
||||
-init-macrodef-debug: defines macro for class debugging
|
||||
-init-macrodef-java: defines macro for class execution
|
||||
-do-jar: JAR building
|
||||
run: execution of project
|
||||
-javadoc-build: Javadoc generation
|
||||
test-report: JUnit report generation
|
||||
|
||||
An example of overriding the target for project execution could look like this:
|
||||
|
||||
<target name="run" depends="StockTicker-impl.jar">
|
||||
<exec dir="bin" executable="launcher.exe">
|
||||
<arg file="${dist.jar}"/>
|
||||
</exec>
|
||||
</target>
|
||||
|
||||
Notice that the overridden target depends on the jar target and not only on
|
||||
the compile target as the regular run target does. Again, for a list of available
|
||||
properties which you can use, check the target you are overriding in the
|
||||
nbproject/build-impl.xml file.
|
||||
|
||||
-->
|
||||
</project>
|
||||
4
build/built-jar.properties
Normal file
4
build/built-jar.properties
Normal file
@ -0,0 +1,4 @@
|
||||
#Sat, 27 Feb 2016 10:30:33 +0100
|
||||
|
||||
|
||||
D\:\\sources\\ulf\ gebhardt\\java\\StockTicker=
|
||||
0
build/classes/.netbeans_automatic_build
Normal file
0
build/classes/.netbeans_automatic_build
Normal file
0
build/classes/.netbeans_update_resources
Normal file
0
build/classes/.netbeans_update_resources
Normal file
BIN
build/classes/StockTicker$1.class
Normal file
BIN
build/classes/StockTicker$1.class
Normal file
Binary file not shown.
BIN
build/classes/StockTicker.class
Normal file
BIN
build/classes/StockTicker.class
Normal file
Binary file not shown.
BIN
build/classes/StockTicker_Solution$1.class
Normal file
BIN
build/classes/StockTicker_Solution$1.class
Normal file
Binary file not shown.
BIN
build/classes/StockTicker_Solution.class
Normal file
BIN
build/classes/StockTicker_Solution.class
Normal file
Binary file not shown.
32
dist/README.TXT
vendored
Normal file
32
dist/README.TXT
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
========================
|
||||
BUILD OUTPUT DESCRIPTION
|
||||
========================
|
||||
|
||||
When you build an Java application project that has a main class, the IDE
|
||||
automatically copies all of the JAR
|
||||
files on the projects classpath to your projects dist/lib folder. The IDE
|
||||
also adds each of the JAR files to the Class-Path element in the application
|
||||
JAR files manifest file (MANIFEST.MF).
|
||||
|
||||
To run the project from the command line, go to the dist folder and
|
||||
type the following:
|
||||
|
||||
java -jar "StockTicker.jar"
|
||||
|
||||
To distribute this project, zip up the dist folder (including the lib folder)
|
||||
and distribute the ZIP file.
|
||||
|
||||
Notes:
|
||||
|
||||
* If two JAR files on the project classpath have the same name, only the first
|
||||
JAR file is copied to the lib folder.
|
||||
* Only JAR files are copied to the lib folder.
|
||||
If the classpath contains other types of files or folders, these files (folders)
|
||||
are not copied.
|
||||
* If a library on the projects classpath also has a Class-Path element
|
||||
specified in the manifest,the content of the Class-Path element has to be on
|
||||
the projects runtime path.
|
||||
* To set a main class in a standard Java project, right-click the project node
|
||||
in the Projects window and choose Properties. Then click Run and enter the
|
||||
class name in the Main Class field. Alternatively, you can manually type the
|
||||
class name in the manifest Main-Class element.
|
||||
BIN
dist/StockTicker.jar
vendored
Normal file
BIN
dist/StockTicker.jar
vendored
Normal file
Binary file not shown.
BIN
dist/lib/acm.jar
vendored
Normal file
BIN
dist/lib/acm.jar
vendored
Normal file
Binary file not shown.
BIN
libs/acm.jar
Normal file
BIN
libs/acm.jar
Normal file
Binary file not shown.
3
manifest.mf
Normal file
3
manifest.mf
Normal file
@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
||||
1401
nbproject/build-impl.xml
Normal file
1401
nbproject/build-impl.xml
Normal file
File diff suppressed because it is too large
Load Diff
8
nbproject/genfiles.properties
Normal file
8
nbproject/genfiles.properties
Normal file
@ -0,0 +1,8 @@
|
||||
build.xml.data.CRC32=f1c9156f
|
||||
build.xml.script.CRC32=27859bb6
|
||||
build.xml.stylesheet.CRC32=8064a381@1.78.1.48
|
||||
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
|
||||
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
|
||||
nbproject/build-impl.xml.data.CRC32=f1c9156f
|
||||
nbproject/build-impl.xml.script.CRC32=5fc69be5
|
||||
nbproject/build-impl.xml.stylesheet.CRC32=2d327b5d@1.78.1.48
|
||||
1
nbproject/private/.gitignore
vendored
Normal file
1
nbproject/private/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/private.properties
|
||||
76
nbproject/project.properties
Normal file
76
nbproject/project.properties
Normal file
@ -0,0 +1,76 @@
|
||||
annotation.processing.enabled=true
|
||||
annotation.processing.enabled.in.editor=false
|
||||
annotation.processing.processor.options=
|
||||
annotation.processing.processors.list=
|
||||
annotation.processing.run.all.processors=true
|
||||
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
|
||||
build.classes.dir=${build.dir}/classes
|
||||
build.classes.excludes=**/*.java,**/*.form
|
||||
# This directory is removed when the project is cleaned:
|
||||
build.dir=build
|
||||
build.generated.dir=${build.dir}/generated
|
||||
build.generated.sources.dir=${build.dir}/generated-sources
|
||||
# Only compile against the classpath explicitly listed here:
|
||||
build.sysclasspath=ignore
|
||||
build.test.classes.dir=${build.dir}/test/classes
|
||||
build.test.results.dir=${build.dir}/test/results
|
||||
# Uncomment to specify the preferred debugger connection transport:
|
||||
#debug.transport=dt_socket
|
||||
debug.classpath=\
|
||||
${run.classpath}
|
||||
debug.test.classpath=\
|
||||
${run.test.classpath}
|
||||
# Files in build.classes.dir which should be excluded from distribution jar
|
||||
dist.archive.excludes=
|
||||
# This directory is removed when the project is cleaned:
|
||||
dist.dir=dist
|
||||
dist.jar=${dist.dir}/StockTicker.jar
|
||||
dist.javadoc.dir=${dist.dir}/javadoc
|
||||
excludes=
|
||||
file.reference.acm.jar=libs\\acm.jar
|
||||
file.reference.StockTicker-src=src
|
||||
includes=**
|
||||
jar.compress=false
|
||||
javac.classpath=\
|
||||
${file.reference.acm.jar}
|
||||
# Space-separated list of extra javac options
|
||||
javac.compilerargs=
|
||||
javac.deprecation=false
|
||||
javac.external.vm=true
|
||||
javac.processorpath=\
|
||||
${javac.classpath}
|
||||
javac.source=1.8
|
||||
javac.target=1.8
|
||||
javac.test.classpath=\
|
||||
${javac.classpath}:\
|
||||
${build.classes.dir}
|
||||
javac.test.processorpath=\
|
||||
${javac.test.classpath}
|
||||
javadoc.additionalparam=
|
||||
javadoc.author=false
|
||||
javadoc.encoding=${source.encoding}
|
||||
javadoc.noindex=false
|
||||
javadoc.nonavbar=false
|
||||
javadoc.notree=false
|
||||
javadoc.private=false
|
||||
javadoc.splitindex=true
|
||||
javadoc.use=true
|
||||
javadoc.version=false
|
||||
javadoc.windowtitle=
|
||||
main.class=StockTicker
|
||||
manifest.file=manifest.mf
|
||||
meta.inf.dir=${src.dir}/META-INF
|
||||
mkdist.disabled=false
|
||||
platform.active=default_platform
|
||||
run.classpath=\
|
||||
${javac.classpath}:\
|
||||
${build.classes.dir}
|
||||
# Space-separated list of JVM arguments used when running the project.
|
||||
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
|
||||
# To set system properties for unit tests define test-sys-prop.name=value:
|
||||
run.jvmargs=
|
||||
run.test.classpath=\
|
||||
${javac.test.classpath}:\
|
||||
${build.test.classes.dir}
|
||||
source.encoding=UTF-8
|
||||
src.dir=${file.reference.StockTicker-src}
|
||||
13
nbproject/project.xml
Normal file
13
nbproject/project.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||
<type>org.netbeans.modules.java.j2seproject</type>
|
||||
<configuration>
|
||||
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
|
||||
<name>StockTicker</name>
|
||||
<source-roots>
|
||||
<root id="src.dir"/>
|
||||
</source-roots>
|
||||
<test-roots/>
|
||||
</data>
|
||||
</configuration>
|
||||
</project>
|
||||
261
src/StockTicker.java
Normal file
261
src/StockTicker.java
Normal file
@ -0,0 +1,261 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
import acm.graphics.GLabel;
|
||||
import acm.graphics.GLine;
|
||||
import acm.graphics.GPoint;
|
||||
import acm.graphics.GRect;
|
||||
import acm.program.GraphicsProgram;
|
||||
|
||||
public class StockTicker extends GraphicsProgram {
|
||||
|
||||
/**
|
||||
* Solves a warning; not realy needed
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// maximum delay for timers to call updateTicker
|
||||
private static final int MAX_DURATION = 5000;
|
||||
|
||||
// the maximum value to expect
|
||||
private static final int MAX_VALUE = 1000;
|
||||
|
||||
// the number of calls to updateTicker
|
||||
private static final int NR_TIMERS = 3000;
|
||||
|
||||
// the maximum difference of old and new value
|
||||
private static final int MAX_FLUCTUATION = 15;
|
||||
|
||||
// the ticker symbols traded
|
||||
private static final String[] SYMBOLS = new String[] { "SNC", "EKDD",
|
||||
"ATD", "AGN", "HEDM" };
|
||||
|
||||
private Map<String, acm.graphics.GPoint> oldPos;
|
||||
|
||||
// we keep the timers in this collection to start them after init()
|
||||
private Vector<Timer> timers;
|
||||
|
||||
// the appr. time when the timers were startes
|
||||
private long startedAt;
|
||||
|
||||
// the mapping of ticker symbols to colors
|
||||
protected Map<String, Color> colors;
|
||||
|
||||
/**
|
||||
* Create a new StockTicker.
|
||||
*
|
||||
* This will instantiate all timers with their calls to updateTicker and put
|
||||
* them into the timers collection.
|
||||
*/
|
||||
public StockTicker()
|
||||
{
|
||||
timers = new Vector<Timer>(NR_TIMERS);
|
||||
|
||||
final Map<String, Double> oldWorth = new HashMap<String, Double>();
|
||||
for (String symbol : SYMBOLS)
|
||||
{
|
||||
// start somewhere near MAX_VALUE/2
|
||||
oldWorth.put(symbol, MAX_VALUE * Math.random() / 2 + (MAX_VALUE / 4));
|
||||
}
|
||||
for (int i = 0; i < NR_TIMERS; i++)
|
||||
{
|
||||
// instantiate a timer with a random delay within 0..MAX_DURATION
|
||||
Timer timer = new Timer((int) (Math.random() * MAX_DURATION),
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// get a random ticker symbol
|
||||
String symbol = SYMBOLS[(int) (Math.random() * SYMBOLS.length)];
|
||||
|
||||
// calculate the new worth
|
||||
double newWorth = oldWorth.get(symbol)
|
||||
+ MAX_FLUCTUATION * 2
|
||||
* (Math.random() - 0.5);
|
||||
|
||||
// do not increase beyond MAX_VALUE
|
||||
if (newWorth > MAX_VALUE) {
|
||||
newWorth -= MAX_FLUCTUATION;
|
||||
}
|
||||
// save the new value for next time
|
||||
oldWorth.put(symbol, newWorth);
|
||||
|
||||
updateTicker(symbol, newWorth);
|
||||
}
|
||||
});
|
||||
// only fire once
|
||||
timer.setRepeats(false);
|
||||
timers.add(timer);
|
||||
}
|
||||
|
||||
// pre-allocate the color mappings
|
||||
colors = new HashMap<String, Color>();
|
||||
colors.put("SNC", Color.BLUE);
|
||||
colors.put("EKDD", Color.ORANGE);
|
||||
colors.put("ATD", Color.MAGENTA);
|
||||
colors.put("AGN", Color.GRAY);
|
||||
colors.put("HEDM", Color.RED);
|
||||
|
||||
//initialize oldPos HashMap
|
||||
oldPos = new HashMap<String, acm.graphics.GPoint>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the timers and set the startTime.
|
||||
*/
|
||||
public void startTicking() {
|
||||
startedAt = System.currentTimeMillis();
|
||||
for (Timer timer : timers) {
|
||||
timer.start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Correct Height
|
||||
* Delete this when acm is fixed
|
||||
*/
|
||||
@Override
|
||||
public int getHeight()
|
||||
{
|
||||
return super.getHeight() - 30;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw Interface: - Y-Axis
|
||||
* - X-Lines every 50 pixel
|
||||
* - Value of every line
|
||||
* - White Rect
|
||||
* - Names of TickerSymbols in right Color
|
||||
*/
|
||||
@Override
|
||||
public void init()
|
||||
{
|
||||
{ //draw horizontal line
|
||||
GLine hline = new GLine(0,0,0,getHeight());
|
||||
add(hline); //draw it
|
||||
}
|
||||
|
||||
final int distance = 50; //50px distance between vlines
|
||||
|
||||
{ //draw vertical lines
|
||||
for(int i=0; i <= (getHeight()/distance);i++) //lines
|
||||
{
|
||||
for(int j=0; j < (getWidth()/10);j++) //small lines every 10px with leght 5
|
||||
{
|
||||
//many small lines per "line"
|
||||
GLine vline = new GLine( j*10+1, //+1 to not override the x-achsis
|
||||
getHeight()-i*distance,
|
||||
j*10+5+1,
|
||||
getHeight()-i*distance);
|
||||
vline.setColor(Color.LIGHT_GRAY); //light-gray!!!
|
||||
add(vline); //draw it
|
||||
}
|
||||
|
||||
//once per line
|
||||
GLabel label = new GLabel( String.valueOf((int)(((double)MAX_VALUE/(double)getHeight())*i*distance)),
|
||||
3,
|
||||
getHeight()-i*distance-2);
|
||||
add(label); //draw it
|
||||
}
|
||||
}
|
||||
|
||||
{ //Draw white Rect
|
||||
GRect rect = new GRect(getWidth()-120,0,120,120);
|
||||
rect.setColor(Color.WHITE); //color
|
||||
rect.setFilled(true); //do fill
|
||||
add(rect); //draw it
|
||||
}
|
||||
|
||||
{ //Draw Labels in Rect
|
||||
for(int i=0;i < SYMBOLS.length; i++)
|
||||
{
|
||||
//new label with symbol
|
||||
GLabel label = new GLabel(SYMBOLS[i],getWidth()-100,i*20+20);
|
||||
//set the right color
|
||||
label.setColor(colors.get(SYMBOLS[i])); //load color here
|
||||
//draw it
|
||||
add(label);
|
||||
}
|
||||
}
|
||||
|
||||
// uncomment this line when you have implemented 6.3 - i have
|
||||
startTicking();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a X-Value relative to startedAt to an Pixel-Value
|
||||
* relative to MAX_DURATION and Window-Width
|
||||
*
|
||||
* @param x X-Value relative to startedAT
|
||||
* @return Pixel-Value which represents given X-Value
|
||||
*/
|
||||
public double XVal2XPix(double x)
|
||||
{
|
||||
//pixel_per_timepassed
|
||||
return ((double)getWidth()/(double)MAX_DURATION)*x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Y-Value(worth) to an Pixel-Value
|
||||
* relative to MAX_VALUE and Window-Height
|
||||
*
|
||||
* @param y Y-Value(worth)
|
||||
* @return Pixel-Value which represents given Y-Value
|
||||
*/
|
||||
public double YVal2YPix(double y)
|
||||
{
|
||||
//pixel_per_valuepoint
|
||||
return ((double)getHeight()/(double)MAX_VALUE)*y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the graph of a single ticker symbol.
|
||||
*
|
||||
* @param symbol A textual representation of the ticker symbol.
|
||||
* @param worth The new worth of the company.
|
||||
*/
|
||||
public void updateTicker(String symbol, double worth)
|
||||
{
|
||||
//Calculate new Coordinates with time passed since start(x) and worth(y)
|
||||
double x_val = XVal2XPix(System.currentTimeMillis() - startedAt);
|
||||
double y_val = YVal2YPix(worth);
|
||||
|
||||
//Get last Value of given ticker-symbol
|
||||
GPoint oldgp = oldPos.get(symbol);
|
||||
|
||||
//No old value stored at first call for every ticker-symbol
|
||||
if(oldgp == null)
|
||||
{
|
||||
//create new Point with actual values instead
|
||||
oldgp = new GPoint(x_val/*1*/,y_val);//x_val,y_val);
|
||||
}
|
||||
|
||||
//line from oldpos to newpos
|
||||
GLine line = new GLine( oldgp.getX(), //oldpos
|
||||
oldgp.getY(),
|
||||
x_val, //newpos
|
||||
y_val);
|
||||
|
||||
//set the right color for every ticker-symbol
|
||||
line.setColor(colors.get(symbol));
|
||||
|
||||
//paint it
|
||||
add(line);
|
||||
|
||||
//store new pos as oldpos for next call.
|
||||
oldPos.put(symbol, new GPoint(x_val,y_val));
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate a new StockTicker and start it.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
new StockTicker().start(args);
|
||||
}
|
||||
|
||||
}
|
||||
210
src/StockTicker_Solution.java
Normal file
210
src/StockTicker_Solution.java
Normal file
@ -0,0 +1,210 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
import acm.graphics.GLabel;
|
||||
import acm.graphics.GLine;
|
||||
import acm.graphics.GPoint;
|
||||
import acm.graphics.GRect;
|
||||
import acm.program.GraphicsProgram;
|
||||
|
||||
public class StockTicker_Solution extends GraphicsProgram {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1866080068453559421L;
|
||||
|
||||
// maximum delay for timers to call updateTicker
|
||||
private static final int MAX_DURATION = 5000;
|
||||
|
||||
// the maximum value to expect
|
||||
private static final int MAX_VALUE = 1000;
|
||||
|
||||
// the number of calls to updateTicker
|
||||
private static final int NR_TIMERS = 3000;
|
||||
|
||||
// the maximum difference of old and new value
|
||||
private static final int MAX_FLUCTUATION = 15;
|
||||
|
||||
// the ticker symbols traded
|
||||
private static final String[] SYMBOLS = new String[] { "SNC", "EKDD",
|
||||
"ATD", "AGN", "HEDM" };
|
||||
|
||||
// we keep the timers in this collection to start them after init()
|
||||
private Vector<Timer> timers;
|
||||
|
||||
// the appr. time when the timers were startes
|
||||
private long startedAt;
|
||||
|
||||
// the mapping of ticker symbols to colors
|
||||
protected Map<String, Color> colors;
|
||||
|
||||
//
|
||||
protected Map<String, GPoint> oldPos;
|
||||
|
||||
/**
|
||||
* Create a new StockTicker.
|
||||
*
|
||||
* This will instantiate all timers with their calls to updateTicker and put
|
||||
* them into the timers collection.
|
||||
*/
|
||||
public StockTicker_Solution() {
|
||||
timers = new Vector<Timer>(NR_TIMERS);
|
||||
|
||||
final Map<String, Double> oldWorth = new HashMap<String, Double>();
|
||||
for (String symbol : SYMBOLS) {
|
||||
// start somewhere near MAX_VALUE/2
|
||||
oldWorth.put(symbol, MAX_VALUE * Math.random() / 2 + (MAX_VALUE / 4));
|
||||
}
|
||||
for (int i = 0; i < NR_TIMERS; i++) {
|
||||
// instantiate a timer with a random delay within 0..MAX_DURATION
|
||||
Timer timer = new Timer((int) (Math.random() * MAX_DURATION),
|
||||
new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// get a random ticker symbol
|
||||
String symbol = SYMBOLS[(int) (Math.random() * SYMBOLS.length)];
|
||||
|
||||
// calculate the new worth
|
||||
double newWorth = oldWorth.get(symbol)
|
||||
+ MAX_FLUCTUATION * 2
|
||||
* (Math.random() - 0.5);
|
||||
|
||||
// do not increase beyond MAX_VALUE
|
||||
if (newWorth > MAX_VALUE) {
|
||||
newWorth -= MAX_FLUCTUATION;
|
||||
}
|
||||
// save the new value for next time
|
||||
oldWorth.put(symbol, newWorth);
|
||||
|
||||
updateTicker(symbol, newWorth);
|
||||
}
|
||||
});
|
||||
// only fire once
|
||||
timer.setRepeats(false);
|
||||
timers.add(timer);
|
||||
}
|
||||
|
||||
// pre-allocate the color mappings
|
||||
colors = new HashMap<String, Color>();
|
||||
colors.put("SNC", Color.BLUE);
|
||||
colors.put("EKDD", Color.ORANGE);
|
||||
colors.put("ATD", Color.MAGENTA);
|
||||
colors.put("AGN", Color.GRAY);
|
||||
colors.put("HEDM", Color.RED);
|
||||
|
||||
oldPos = new HashMap<String, GPoint>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start the timers and set the startTime.
|
||||
*/
|
||||
public void startTicking() {
|
||||
startedAt = System.currentTimeMillis();
|
||||
for (Timer timer : timers) {
|
||||
timer.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
// add your call to the methods from 6.2 here
|
||||
GLine line_y = new GLine (0 , 0 , 0 , getHeight() );
|
||||
line_y.setColor(Color.black);
|
||||
add(line_y);
|
||||
|
||||
for (int i = 0; i < (getHeight() / 50) ; i++)
|
||||
{
|
||||
GLabel label = new GLabel (Integer.toString(MAX_VALUE - 100*(10-i)), 5 , getHeight() - (50*i + 5) ) ;
|
||||
add(label);
|
||||
for (int j = 0; j < (getWidth() / 8) ; j++)
|
||||
{
|
||||
GLine line_x = new GLine ( j * 8, getHeight() - (50 * i), j * 8 + 5, getHeight() - (50 * i));
|
||||
line_x.setColor(Color.gray);
|
||||
add(line_x);
|
||||
}
|
||||
}
|
||||
|
||||
GRect names = new GRect(getWidth()- 70,0,70,80);
|
||||
names.setFillColor(Color.white);
|
||||
names.setColor(Color.white);
|
||||
names.setFilled(true);
|
||||
add(names);
|
||||
|
||||
|
||||
for (int i = 0 ; i < SYMBOLS.length ; i++)
|
||||
{
|
||||
GLabel symbols = new GLabel (SYMBOLS[i], getWidth() - 50 , 5 + ((i+1) * 10));
|
||||
symbols.setColor(colors.get(SYMBOLS[i]));
|
||||
add(symbols);
|
||||
}
|
||||
|
||||
// uncomment this line when you have implemented 6.3
|
||||
startTicking();
|
||||
}
|
||||
|
||||
private String InttoString(int maxValue) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the graph of a single ticker symbol.
|
||||
*
|
||||
* @param symbol A textual representation of the ticker symbol.
|
||||
* @param worth The new worth of the company.
|
||||
*/
|
||||
public void updateTicker(String symbol, double worth) {
|
||||
|
||||
|
||||
|
||||
|
||||
if (oldPos.containsKey(symbol)) {
|
||||
|
||||
GLine stock = new GLine (
|
||||
oldPos.get(symbol).getX(), //old x
|
||||
oldPos.get(symbol).getY(), //old y
|
||||
(System.currentTimeMillis() - startedAt) * getWidth() / MAX_DURATION, //new x
|
||||
getHeight () - (worth / 2));//new y
|
||||
stock.setColor(colors.get(symbol));
|
||||
add(stock);
|
||||
|
||||
oldPos.put(symbol, new GPoint (
|
||||
(System.currentTimeMillis() - startedAt) * getWidth()/ MAX_DURATION ,
|
||||
getHeight() - (worth / 2)));
|
||||
} else
|
||||
|
||||
{
|
||||
GLine stock = new GLine(0,
|
||||
getHeight() - (worth / 2),
|
||||
(System.currentTimeMillis() - startedAt) * getWidth() / MAX_DURATION,
|
||||
getHeight() - (worth / 2));
|
||||
stock.setColor(colors.get(symbol));
|
||||
add(stock);
|
||||
|
||||
oldPos.put(symbol, new GPoint (
|
||||
(System.currentTimeMillis() - startedAt) * getWidth() / MAX_DURATION,
|
||||
getHeight()- (worth / 2)));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Instantiate a new StockTicker and start it.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
new StockTicker_Solution().start(args);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user