From 6d8c7f5add72e0921bb00053c4910538810c3f16 Mon Sep 17 00:00:00 2001 From: mhunteman Date: Sun, 10 Jul 2022 15:34:10 -0500 Subject: Add user input with error handling --- picker.java | 133 ++++++++++++++++++++++++++++++++++++++++++++++-------------- stock.java | 18 -------- 2 files changed, 102 insertions(+), 49 deletions(-) delete mode 100644 stock.java diff --git a/picker.java b/picker.java index 708273c..2a31355 100644 --- a/picker.java +++ b/picker.java @@ -1,24 +1,8 @@ -import java.util.*; import java.io.*; +import java.lang.*; +import java.util.*; -public class Stock { - int price, value; - String ticker; - - public Stock() { - price = 0; - value = 0; - ticker = ""; - } - - public Stock(int price, int value, String ticker) { - this.price = price; - this.value = value; - this.ticker = ticker; - } -} - -public class Backpointer { +class Backpointer { static Stock NOTHING = new Stock(1, 0, ""); int previousValue, totalValue; @@ -33,17 +17,20 @@ public class Backpointer { ArrayList backpointers) { int currentPrice = backpointers.size(); - Backpointer best = + Backpointer best = new Backpointer(backpointers.get(currentPrice - 1).totalValue, NOTHING); + for (int i = 0; i < stocks.length; i++) { - Stock stock = stocks[i]; + Stock stock = stocks[i]; int previousPrice = currentPrice - stock.price; + if (previousPrice >= 0) { - Backpointer candidate = - new Backpointer(backpointers.get(previousPrice).totalValue, stock); - if (candidate.totalValue > best.totalValue) { + Backpointer candidate = + new Backpointer(backpointers.get(previousPrice).totalValue, + stock); + + if (candidate.totalValue > best.totalValue) best = candidate; - } } } return best; @@ -51,7 +38,7 @@ public class Backpointer { public static ArrayList chooseStocks(Stock stocks[], int priceLimit) { // assert stock values are integers - + // create backpointers ArrayList backpointers = new ArrayList(); backpointers.add(new Backpointer(0, NOTHING)); @@ -61,12 +48,14 @@ public class Backpointer { // postprocess backpointers ArrayList result = new ArrayList(); - for (int price = priceLimit; price > 0; + for (int price = priceLimit; price > 0; price -= backpointers.get(price).stock.price) { + // for debugging + // System.out.println(price); Stock stock = backpointers.get(price).stock; + if (stock != NOTHING) result.add(stock); - result.add(backpointers.get(price).stock); } Collections.reverse(result); @@ -74,8 +63,90 @@ public class Backpointer { } public static void main(String[] args) { - Stock stocks[] = {new Stock(3, 10, "A"), new Stock(4, 14, "B")}; - int priceLimit = 10; - ArrayList result = chooseStocks(stocks, priceLimit); + int input; + Scanner scanner = new Scanner(System.in); + ArrayList choices = new ArrayList(); + int priceLimit = 0; + boolean run = false; + + do { + System.out.println("1. Add a stock"); + System.out.println("2. Buying power"); + System.out.println("3. Display stocks and buying power"); + System.out.println("4. Run"); + System.out.println("5. Exit"); + + input = scanner.nextInt(); + String newLine = scanner.nextLine(); + + switch (input) { + case 1: + System.out.println("Type 'Stock Ticker, Price (USD), " + + "Value'"); + String stockInput = scanner.nextLine(); + String[] stockTraits = stockInput.trim() + .split("\\s*,\\s*"); + try { + choices.add(new Stock(Integer.parseInt(stockTraits[1]), + Integer.parseInt(stockTraits[2]), stockTraits[0])); + } catch (NumberFormatException e) { + System.out.println("Invalid input - must enter: " + + "ticker, #, #"); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("Wrong number of inputs - must " + + "enter: ticker, #, #"); + } + break; + case 2: + System.out.println("How much money do you have to " + + "allocate?"); + priceLimit = Integer.parseInt(scanner.nextLine()); + break; + case 3: + System.out.println(choices); + System.out.println(priceLimit); + break; + case 4: + run = true; + break; + case 5: + System.exit(0); + break; + default: + System.out.println("Invalid input - must enter a number " + + "between 1 and 5"); + break; + } + } + while(run == false); + // Stock stocks[] = {new Stock(3, 10, "A"), new Stock(4, 14, "B")}; + ArrayList result = chooseStocks(choices.toArray( + new Stock[choices.size()]), priceLimit); + + for (int i = 0; i < result.size(); i++) { + System.out.println(result.get(i)); + } + } +} + +class Stock { + int price, value; + String ticker; + + public Stock() { + price = 0; + value = 0; + ticker = ""; + } + + public Stock(int price, int value, String ticker) { + this.price = price; + this.value = value; + this.ticker = ticker; + } + + @Override + public String toString() { + return "Ticker: " + ticker + " price: " + price + " value: " + value; } } diff --git a/stock.java b/stock.java deleted file mode 100644 index 9a15628..0000000 --- a/stock.java +++ /dev/null @@ -1,18 +0,0 @@ -package stocks; - -public class Stock { - int price, value; - String ticker; - - public Stock() { - price = 0; - value = 0; - ticker = ""; - } - - public Stock(int price, int value, String ticker) { - this.price = price; - this.value = value; - this.ticker = ticker; - } -} -- cgit v1.2.3