From ad3cc8f08d0b629dfc68c97a81ee247a1795b518 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Wed, 27 Jul 2022 17:36:37 -0500 Subject: Add getters for stock attributes --- picker.java | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/picker.java b/picker.java index f25cf8f..d9de0f1 100644 --- a/picker.java +++ b/picker.java @@ -27,7 +27,7 @@ class Backpointer { public Backpointer(int previousValue, Stock stock) { this.stock = stock; - this.totalValue = previousValue + stock.value; + this.totalValue = previousValue + stock.getValue(); } public static Backpointer chooseBackpointer(Stock[] stocks, @@ -38,7 +38,7 @@ class Backpointer { new Backpointer(backpointers.get(currentPrice - 1).totalValue, NOTHING); for (Stock stock : stocks) { - int previousPrice = currentPrice - stock.price; + int previousPrice = currentPrice - stock.getPrice(); if (previousPrice >= 0) { Backpointer candidate = @@ -65,7 +65,7 @@ class Backpointer { // postprocess backpointers ArrayList result = new ArrayList(); for (int price = priceLimit; price > 0; - price -= backpointers.get(price).stock.price) { + price -= backpointers.get(price).stock.getPrice()) { Stock stock = backpointers.get(price).stock; if (stock != NOTHING) @@ -132,22 +132,27 @@ class Backpointer { break; } } - while(!run); + while (!run); scanner.close(); // Stock stocks[] = {new Stock(3, 10, "A"), new Stock(4, 14, "B")}; ArrayList result = chooseStocks(choices.toArray( new Stock[choices.size()]), priceLimit); + int totalValue = 0; for (Stock stock : result) { System.out.println(stock); + totalValue += stock.getValue(); } + + System.out.println("\nBuying power: " + priceLimit); + System.out.println("Total value: " + totalValue); } } class Stock { - int price, value; - String ticker; + private int price, value; + private String ticker; public Stock() { price = 0; @@ -161,6 +166,14 @@ class Stock { this.ticker = ticker; } + public int getPrice() { + return price; + } + + public int getValue() { + return value; + } + @Override public String toString() { return "Ticker: " + ticker + " price: " + price + " value: " + value; -- cgit v1.2.3