idk ive done too much

This commit is contained in:
2022-12-11 21:40:09 -06:00
parent 9a1bc9cfb5
commit 4ee4e7551a
59 changed files with 3553 additions and 13 deletions

15
LAB-8/.classpath Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

17
LAB-8/.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LAB-8</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>

1
LAB-8/input1.txt Normal file
View File

@@ -0,0 +1 @@
0.25 0.20 0.18 0.05 29999999.95

View File

@@ -0,0 +1,7 @@
/**
* @author Brett Bender
* @version 2022.12.05
*/
module uwstout.courses.cs144 {
requires junit;
}

View File

@@ -0,0 +1,162 @@
package uwstout.courses.cs144.labs.lab8;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Stores the prices of an item over time, and provides access to the current,
* maximum, and average price.
*
* @author Brett Bender
* @version 2022.12.05
*/
public class PriceHistory {
private static final int INITIAL_SIZE = 5;
private String name;
private double[] prices;
private int count;
/**
* Create a new PriceHistory that is tracking the item named nName.
* Initializes the internal array to {@link #INITIAL_SIZE}, and the count to
* 0.
*
* @param nName The name of the item.
*/
public PriceHistory(String nName) {
name = nName;
prices = new double[INITIAL_SIZE];
count = 0;
}
/**
* Get the name of the item
*
* @return The item name.
*/
public String getName() {
return name;
}
/**
* Get the current (last) price. Returns 0 if no price history stored.
*
* @return The current price
*/
public double getCurrentPrice() {
if (count > 0) {
return prices[count - 1];
}
return 0;
}
/**
* Get the number of prices in the history.
*
* @return The number of prices
*/
public int getNumberOfPrices() {
return count;
}
/**
* Get the price at a certain point. Returns 0 if pos out of bounds.
*
* @param pos The position of the price you'd like to retrieve.
* @return The price at that position
*/
public double getPrice(int pos) {
if (0 <= pos && pos < count) {
return prices[pos];
}
return 0;
}
/**
* Add a price to the history. Will only add if positive, and will
* automatically expand the array if needed.
*
* @param price The price to add
*/
public void addPrice(double price) {
if (price > 0) {
if (count == prices.length) {
double[] temp = new double[prices.length + 3];
for (int i = 0; i < prices.length; i++) {
temp[i] = prices[i];
}
prices = temp;
}
prices[count++] = price;
}
}
/**
* Add prices from an input scanner
*
* @param sc The input scanner
*/
public void addPrices(Scanner sc) {
while (sc.hasNextDouble()) {
addPrice(sc.nextDouble()); // Since addPrice already checks if the
// value is positive/negative, we can
// directly pass in the input
}
}
/**
* Add prices from an input file
*
* @param fileName The input file name
*/
public void addPrices(String fileName) {
try {
Scanner sc = new Scanner(new File(fileName));
addPrices(sc);
sc.close();
} catch (FileNotFoundException e) {
// Do nothing
}
}
/**
* Get the maximum price of an item
*
* @return The maximum price
*/
public double getMaximumPrice() {
double max = 0;
if (count > 0) {
max = prices[0];
for (int i = 1; i < count; i++) {
double x = prices[i];
if (x > max) {
max = x;
}
}
}
return max;
}
/**
* Get the average price of the item
*
* @return The average price
*/
public double getAveragePrice() {
double avg = 0;
if (count > 0) {
for (int i = 0; i < count; i++) {
avg += prices[i];
}
avg = avg / count;
}
return avg;
}
}

View File

@@ -0,0 +1,235 @@
package uwstout.courses.cs144.labs.lab8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.PrintWriter;
import java.util.Scanner;
import org.junit.Test;
/**
* Tests the PriceHistory class
*
* @author Brett Bender
* @version 2022.12.05
*/
public class PriceHistoryTest {
private static final double DELTA = 0.001;
/**
* Tests the constructor
*/
@Test
public void testPriceHistory() {
PriceHistory ph1 = new PriceHistory("Test Item");
assertEquals("Test Item", ph1.getName());
assertEquals(0, ph1.getNumberOfPrices());
PriceHistory ph2 = new PriceHistory("Wooot");
assertEquals("Wooot", ph2.getName());
assertEquals(0, ph2.getNumberOfPrices());
}
/**
* Tests the getCurrentPrice method
*/
@Test
public void testGetCurrentPrice() {
PriceHistory ph1 = new PriceHistory("Pizza");
assertEquals(0, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(7.99);
assertEquals(7.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(28.99);
assertEquals(28.99, ph1.getCurrentPrice(), DELTA);
}
/**
* Tests the getNumberOfPrices method
*/
@Test
public void testGetNumberOfPrices() {
PriceHistory ph1 = new PriceHistory("Pizza");
assertEquals(0, ph1.getNumberOfPrices());
ph1.addPrice(7.99);
assertEquals(1, ph1.getNumberOfPrices());
ph1.addPrice(21.99);
assertEquals(2, ph1.getNumberOfPrices());
ph1.addPrice(21.99);
assertEquals(3, ph1.getNumberOfPrices());
ph1.addPrice(16.99);
assertEquals(4, ph1.getNumberOfPrices());
ph1.addPrice(28.99);
assertEquals(5, ph1.getNumberOfPrices());
ph1.addPrice(27.99);
assertEquals(6, ph1.getNumberOfPrices());
}
/**
* Tests the getPrice method
*/
@Test
public void testGetPrice() {
PriceHistory ph1 = new PriceHistory("Pizza");
assertEquals(0, ph1.getPrice(1), DELTA);
assertEquals(0, ph1.getPrice(0), DELTA);
assertEquals(0, ph1.getPrice(-1), DELTA);
ph1.addPrice(7.99); // 0
ph1.addPrice(21.99); // 1
ph1.addPrice(21.99); // 2
ph1.addPrice(16.99); // 3
ph1.addPrice(28.99); // 4
ph1.addPrice(27.99); // 5
assertEquals(7.99, ph1.getPrice(0), DELTA);
assertEquals(21.99, ph1.getPrice(1), DELTA);
assertEquals(21.99, ph1.getPrice(2), DELTA);
assertEquals(16.99, ph1.getPrice(3), DELTA);
assertEquals(0, ph1.getPrice(6), DELTA);
}
/**
* Tests the addPrice method
*/
@Test
public void testAddPrice() {
PriceHistory ph1 = new PriceHistory("Pizza");
assertEquals(0, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(-5.99);
assertEquals(0, ph1.getNumberOfPrices());
assertEquals(0, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(5.99);
assertEquals(1, ph1.getNumberOfPrices());
assertEquals(5.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(8.99);
assertEquals(2, ph1.getNumberOfPrices());
assertEquals(8.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(10.99);
assertEquals(3, ph1.getNumberOfPrices());
assertEquals(10.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(0);
assertEquals(3, ph1.getNumberOfPrices());
assertEquals(10.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(-5.99);
assertEquals(3, ph1.getNumberOfPrices());
assertEquals(10.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(11.99); // 4
ph1.addPrice(15.99); // 5
ph1.addPrice(14.99); // 6
assertEquals(6, ph1.getNumberOfPrices());
assertEquals(14.99, ph1.getCurrentPrice(), DELTA);
ph1.addPrice(11.99); // 7
ph1.addPrice(15.99); // 8
ph1.addPrice(14.99); // 9
assertEquals(9, ph1.getNumberOfPrices());
assertEquals(14.99, ph1.getCurrentPrice(), DELTA);
}
/**
* Tests the addPrices(Scanner) method
*/
@Test
public void testAddPricesScanner() {
String input = "0.25 0.20 0.18 0.05 29999999.95";
PriceHistory ph = new PriceHistory("GME");
ph.addPrices(new Scanner(input));
assertEquals(5, ph.getNumberOfPrices());
assertEquals(0.25, ph.getPrice(0), DELTA);
assertEquals(0.2, ph.getPrice(1), DELTA);
assertEquals(0.18, ph.getPrice(2), DELTA);
assertEquals(0.05, ph.getPrice(3), DELTA);
assertEquals(29999999.95, ph.getPrice(4), DELTA);
}
/**
* Tests the addPrices(String) method
*/
@Test
public void testAddPricesString() {
String filename = "input1.txt";
try {
PrintWriter out = new PrintWriter(filename);
out.println("0.25 0.20 0.18 0.05 29999999.95");
out.close();
} catch (Exception e) {
fail("Couldn't write file!");
}
PriceHistory ph = new PriceHistory("Apple Stock");
ph.addPrices(filename);
assertEquals(5, ph.getNumberOfPrices());
assertEquals(0.25, ph.getPrice(0), DELTA);
assertEquals(0.2, ph.getPrice(1), DELTA);
assertEquals(0.18, ph.getPrice(2), DELTA);
assertEquals(0.05, ph.getPrice(3), DELTA);
assertEquals(29999999.95, ph.getPrice(4), DELTA);
PriceHistory ph2 = new PriceHistory("GME Stock");
ph.addPrices("invalid");
assertEquals(0, ph2.getNumberOfPrices());
}
/**
* Tests the getMaximumPrice method
*/
@Test
public void testGetMaximumPrice() {
PriceHistory ph1 = new PriceHistory("Woo");
assertEquals(0, ph1.getMaximumPrice(), DELTA);
String input2 = "0.25 0.20 0.18 0.05 29999999.95";
PriceHistory ph2 = new PriceHistory("GME");
ph2.addPrices(new Scanner(input2));
assertEquals(29999999.95, ph2.getMaximumPrice(), DELTA);
String input3 = "0.25 100 0.20 0.18 0.05";
PriceHistory ph3 = new PriceHistory("APPL");
ph3.addPrices(new Scanner(input3));
assertEquals(100, ph3.getMaximumPrice(), DELTA);
}
/**
* Tests the getAveragePrice method
*/
@Test
public void testGetAveragePrice() {
PriceHistory ph1 = new PriceHistory("Woo");
assertEquals(0, ph1.getAveragePrice(), DELTA);
String input2 = "0.25 0.20 0.18 0.05 29999999.95";
PriceHistory ph2 = new PriceHistory("GME");
ph2.addPrices(new Scanner(input2));
assertEquals(6000000.126, ph2.getAveragePrice(), DELTA);
String input3 = "0.25 100 0.20 0.18 0.05";
PriceHistory ph3 = new PriceHistory("APPL");
ph3.addPrices(new Scanner(input3));
assertEquals(20.136, ph3.getAveragePrice(), DELTA);
}
}