aboutsummaryrefslogtreecommitdiff
path: root/picker.java
blob: 9710348d8c46b8fdfbed47cf2446267fa10c9bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * Stock picking algorithm
 * Copyright (C) 2022 Michael Hunteman
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import java.lang.*;
import java.util.*;
import stock.*;

class Backpointer {
    static Stock NOTHING = new Stock(1, 0, "");

    int totalValue;
    Stock stock;

    public Backpointer(int previousValue, Stock stock) {
        this.stock = stock;
        this.totalValue = previousValue + stock.getValue();
    }

    public static Backpointer chooseBackpointer(Stock[] stocks,
        ArrayList<Backpointer> backpointers) {

        int currentPrice = backpointers.size();
        Backpointer best =
        new Backpointer(backpointers.get(currentPrice - 1).totalValue, NOTHING);

        for (Stock stock : stocks) {
            int previousPrice = currentPrice - stock.getPrice();

            if (previousPrice >= 0) {
                Backpointer candidate =
                    new Backpointer(backpointers.get(previousPrice).totalValue,
                    stock);

                if (candidate.totalValue > best.totalValue)
                    best = candidate;
            }
        }
        return best;
    }

    public static ArrayList<Stock> chooseStocks(Stock[] stocks, int priceLimit) {
        // TODO: assert stock values are integers

        // create backpointers
        ArrayList<Backpointer> backpointers = new ArrayList<Backpointer>();
        backpointers.add(new Backpointer(0, NOTHING));
        while (backpointers.size() <= priceLimit) {
            backpointers.add(chooseBackpointer(stocks, backpointers));
        }

        // postprocess backpointers
        ArrayList<Stock> result = new ArrayList<Stock>();
        for (int price = priceLimit; price > 0;
        price -= backpointers.get(price).stock.getPrice()) {
            Stock stock = backpointers.get(price).stock;

            if (stock != NOTHING)
                result.add(stock);
        }
        Collections.reverse(result);

        return result;
    }

    public static void main(String[] args) {
        int input;
        Scanner scanner = new Scanner(System.in);
        ArrayList<Stock> choices = new ArrayList<Stock>();
        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();
            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?");
                    try {
                        priceLimit = Integer.parseInt(scanner.nextLine());
                    } catch (NumberFormatException e){
                        System.out.println("Invalid input - must enter: #");
                    }
                    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);
        scanner.close();

        // Stock stocks[] = {new Stock(3, 10, "A"), new Stock(4, 14, "B")};
        ArrayList<Stock> 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);
    }
}