1

I am making a stock market simulator and I keep getting an error from each of my 3 classes the first class MYOSM the error is:

"Cannot find symbol- class stock"

The second class MarketTable outputs:

Cannot find MarketDataModel

And the third class MarketDataModel outputs the error:

Cannot find symbol- class stock

Have I gone wrong somewhere in my code I have checked it multiple times and I can't seem to locate it.

Here is my code:

MYOSM class:

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class MYOSM extends JFrame implements Runnable {

  Stock[] market = {
    new Stock("JTree", 14.57),
    new Stock("JTable", 17.44),
    new Stock("JList", 16.44),
    new Stock("JButton", 7.21),
    new Stock("JComponent", 27.40)
  };
  boolean monitor;
  Random rg = new Random();
  Thread runner;

  public MYOSM() {
    // Not meant to be shown as a real frame
    super("Thread only version . . .");
    runner = new Thread(this);
    runner.start();
  }


  public MYOSM(boolean monitorOn) {
    super("Stock Market Monitor");
    setSize(400, 100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    monitor = monitorOn;

    getContentPane().add(new JLabel("Trading is active.  " +
          "Close this window to close the market."),
          BorderLayout.CENTER);
    runner = new Thread(this);
    runner.start();
  }

  public void run() {
    while(true) {
      int whichStock = Math.abs(rg.nextInt()) % market.length;
      double delta = rg.nextDouble() - 0.4;
      market[whichStock].update(delta);
      if (monitor) {
        market[whichStock].print();
      }
      try {
        Thread.sleep(1000);
      }
      catch(InterruptedException ie) {
      }
    }
  }

  public Stock getQuote(int index) {
    return market[index];
  }

  // This method returns the list of all the symbols in the market table
  public String[] getSymbols() {
    String[] symbols = new String[market.length];
    for (int i = 0; i < market.length; i++) {
      symbols[i] = market[i].symbol;
    }
    return symbols;
  }

  public static void main(String args[]) {
    MYOSM myMarket = new MYOSM(args.length > 0);
    myMarket.setVisible(true);
  }
}

MarketTable class:

import java.awt.*;
import javax.swing.*;

public class MarketTable extends JFrame {

  public MarketTable() {
    super("Dynamic Data Test");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    MarketDataModel mdm = new MarketDataModel(5);

    mdm.setStocks(new int[] { 0, 1, 2 });

    JTable jt = new JTable(mdm);
    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
  }

  public static void main(String args[]) {
    MarketTable mt = new MarketTable();
    mt.setVisible(true);
  }
}

MarketDataModel class:

import javax.swing.table.*;
import javax.swing.*;

public class MarketDataModel extends AbstractTableModel
implements Runnable {

  Thread runner;
  MYOSM market;
  int delay;

  public MarketDataModel(int initialDelay) {
    market = new MYOSM();
    delay = initialDelay * 1000;
    Thread runner = new Thread(this);
    runner.start();
  }

  Stock[] stocks = new Stock[0];
  int[] stockIndices = new int[0];
  String[] headers = {"Symbol", "Price", "Change", "Last updated"};

  public int getRowCount() { return stocks.length; }
  public int getColumnCount() { return headers.length; }

  public String getColumnName(int c) { return headers[c]; }

  public Object getValueAt(int r, int c) {
    switch(c) {
    case 0:
      return stocks[r].symbol;
    case 1:
      return new Double(stocks[r].price);
    case 2:
      return new Double(stocks[r].delta);
    case 3:
      return stocks[r].lastUpdate;
    }
    throw new IllegalArgumentException("Bad cell (" + r + ", " + c +")");
  }

  public void setDelay(int seconds) { delay = seconds * 1000; }
  public void setStocks(int[] indices) {
    stockIndices = indices;
    updateStocks();
    fireTableDataChanged();
  }

  public void updateStocks() {
    stocks = new Stock[stockIndices.length];
    for (int i = 0; i < stocks.length; i++) {
      stocks[i] = market.getQuote(stockIndices[i]);
    }
  }

  public void run() {
    while(true) {

      updateStocks();


      fireTableRowsUpdated(0, stocks.length - 1);
      try { Thread.sleep(delay); }
      catch(InterruptedException ie) {}
    }
  }
}
Omor
  • 54
  • 1
  • 8
  • It's hard to say without seeing the stock class, so I'm guessing. return getValueAt() is a strange method because it's returning an object rather than a double. do you have a lastUpdate Object variable in Stock? Maybe there is a lastUpdate method and you just forgot the ()? Post your Stock class too. – Nick Ziebert Mar 26 '17 at 19:38
  • Possible duplicate of [What does a "Cannot find symbol" compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Joe C Mar 26 '17 at 19:56

1 Answers1

0

You're missing a Stock class there. Should be something like this I suppose:

public class Stock {

public Stock(String string, double d) {
        this.symbol = string;
        this.price = d;
    }

    public String symbol;
    public double price;
    public double delta;
    public String lastUpdate;

    public void print() {
        System.out.println(this);
    }

    public void update(double delta2) {
        this.delta = delta2;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Stock [symbol=").append(symbol).append(", price=").append(price).append(", delta=").append(delta).append(", lastUpdate=")
                .append(lastUpdate).append("]");
        return builder.toString();
    }
}

Or is it just in a different package than those classes?

Also it's weird your error message has "stock" in lowercase.

With the Stock class added as above, I have managed to start your code, but I'm not sure what it was supposed to be doing. I must note it is poorly written in general, with some basic mistakes like usage of default package.

mvmn
  • 3,717
  • 27
  • 30