-1

I want to store made up stock information in an ArrayList. The stock information being stored is the stock symbol, number of shares, and the price of each share. If the user inputs 1, they are prompted to enter new stock information. If they enter 2, they can search for a stock symbol and if it is in the ArrayList, it will print the LIFO average cost of the last 250 shares entered into the ArrayList (sum of the cost of the last 250 shares / 250).

What I have so far just stores the information entered into an ArrayList. I have a few questions:

  1. How can I have the user input multiple data types into one element of an ArrayList? Storing the input as sym, amt, and cost and then having

ArrayList<stock> StockList = new ArrayList<stock>(); StockList.add(stock(sym,amt,cost));

doesn't work and I don't know why.

  1. Once the user inputs the information into the ArrayList, I want to prompt the user to input a stock symbol and it will display the average of the last 250 shares entered. How do you search an ArrayList and then print the average of the last 250 shares entered even if they were entered for example as: AAPL 125 536.86 and GOOG 300 1120.15

This is what I have so far:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class stock {

    String name;
    int shares;
    double price;

        public void Stock(String name, int shares, double price){
            this.name = name;
            this.shares = shares;
            this.price = price;
        }

        public String getSym(){
            return name;
        }

        public int getShares(){
            return shares;
        }

        public double getPrice(){
            return price;
        }

        public void setSym(){
            this.name = name;
        }

        public void setShares(){
            this.shares = shares;
        }

        public void setPrice(){
            this.price = price;
        }


        public void add(String name, int shares, double price){


        }





    public static void main(String[] args) {

        int choice = 0;
        ArrayList<Stocks> stock = new ArrayList<Stocks>();

        while (choice == 0){
        System.out.println("Enter 1 to input a new stock, or 2 to query a stock's price, 3 to quit: ");
        Scanner sc1 = new Scanner (System.in);
        choice = sc1.nextInt();



        if (choice == 1){
            System.out.println("Please enter the stock symbol: ");
            String sym = sc1.next();
            System.out.println("Please enter the number of shares: ");
            int amt = sc1.nextInt();
            System.out.println("Please enter the price per share: ");
            double cost = sc1.nextDouble();

            for(int i = 0; i < amt; i++){
            ArrayList<stock> StockList = new ArrayList<stock>();
            StockList.add(stock(sym,amt,cost));
            }


            choice = 0;
            for (int i = 0; i < StockList.size(); i++) {
                System.out.println(myArray[i])
            }


        if (choice == 3){
            System.exit(0);
        }
        }
    }}
  • 2
    I presume that, where you put `public void Stock`, you meant to make it a constructor instead? (`public stock`). Further, you don't have a static method `stock` to just populate and instantiate a `stock` object; you probably meant `new stock(sym, amt, cost)`. – Makoto Mar 30 '14 at 21:12
  • What does "doesn't work" mean? Doesn't compile, results in an error? Doesn't output as you expect? – aliteralmind Mar 30 '14 at 21:13

1 Answers1

0

To search stocks by stock symbol, I'd create a Map<String,Stock>, where the key is the stock symbol. Then retrieve the requested stock with stockMap.get(userInputSymbol).

To get the average of the most-recently entered stocks, you could use an ArrayList<Stock>, where the most recent is inserted at element zero, the oldest (size() - 1) is removed if it happens to be the 251-st. Getting the average could be done by iterating through them and accumulating the values.

I thought there'd be a queue that'd do this adding the newest, deleting the oldest for you, but there isn't.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • @user2921899: if this answer has helped you, please consider up-voting and accepting it as your answer by clicking on the big green checkmark. Best of luck and welcome to stackoverflow! – aliteralmind Apr 02 '14 at 22:39