101 lines
5.3 KiB
HTML
101 lines
5.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>StockList.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
|
table {color: #888888; background-color: #313335; font-family: monospace}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST0 {color: #808080; font-family: monospace; font-weight: bold}
|
|
.literal {color: #cc7832}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab3_CalebFontenot/src/main/java/com/calebfontenot/lab3_calebfontenot/StockList.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
|
|
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.lab3_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST0">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> StockList {
|
|
|
|
<span class="literal">private</span> Stock[] list;
|
|
|
|
<span class="comment">// A constructor that creates a list of type stock</span>
|
|
<span class="literal">public</span> StockList(<span class="literal">int</span> listSize)
|
|
{
|
|
list = <span class="literal">new</span> Stock[listSize];
|
|
createListObjects();
|
|
}
|
|
|
|
<span class="comment">// Creates a list of Fan objects from User's input</span>
|
|
<span class="literal">private</span> <span class="literal">void</span> createListObjects()
|
|
{
|
|
Scanner scan = <span class="literal">new</span> Scanner(System.in);
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < list.length; ++i) {
|
|
Stock stock = <span class="literal">new</span> Stock();
|
|
System.out.println(<span class="string">"</span><span class="string">Enter Stock data for stock </span><span class="string">"</span> + (i + <span class="number">1</span>)
|
|
+ <span class="string">"</span><span class="string">: symbol, name, previous closing price, and current price</span><span class="string">"</span>);
|
|
String symbol = scan.next();
|
|
String name = scan.next();
|
|
<span class="literal">double</span> previousClosingPrice = scan.nextDouble();
|
|
<span class="literal">double</span> currentPrice = scan.nextDouble();
|
|
stock.setSymbol(symbol);
|
|
stock.setName(name);
|
|
stock.setPreviousClosingPrice(previousClosingPrice);
|
|
stock.setCurrentPrice(currentPrice);
|
|
list[i] = stock;
|
|
System.out.println(<span class="string">"</span><span class="string">----------------------------------------</span><span class="string">"</span>);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> String toString()
|
|
{
|
|
String s = <span class="string">"</span><span class="string">StockList{</span><span class="string">"</span> + <span class="string">"</span><span class="string">list=</span><span class="literal">\n</span><span class="string">"</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < list.length; ++i) {
|
|
s += list[i].toString() + <span class="string">"</span><span class="literal">\n</span><span class="string">-----------------------</span><span class="literal">\n</span><span class="string">"</span>;
|
|
}
|
|
s += <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
|
<span class="literal">return</span> s;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> sortByChangeInPercentage()
|
|
{
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < list.length - <span class="number">1</span>; ++i) {
|
|
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j < list.length; ++j) {
|
|
<span class="literal">if</span> (list[i].getChangePercent() > list[j].getChangePercent()) {
|
|
Stock temp = list[i];
|
|
list[i] = list[j];
|
|
list[j] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args)
|
|
{
|
|
StockList stockList = <span class="literal">new</span> StockList(<span class="number">3</span>);
|
|
System.out.println(stockList);
|
|
stockList.sortByChangeInPercentage();
|
|
System.out.println(stockList);
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|