Results 1 to 4 of 4
  1. #1
    Avey is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Question What to do... Simple Stock-Broker program

    Hi guys, I have an assignment which requires defining my own classes and using them to manipulate the stock symbol (like AAPL or GOOG), a stock price, and a stock volume.

    I've written the majority of my "Stock" Class... here it is:

    public class Stock {

    String stockSymbol;
    int stockVolume;
    double stockPrice;

    public Stock()
    {
    stockSymbol ="unknown";
    stockVolume = 0;
    stockPrice = 0;
    }

    public Stock(String s, int v, double p)
    {
    stockSymbol = s;
    stockVolume = v;
    stockPrice = p;
    }

    public void setSymbol(String s)
    {
    stockSymbol = s;
    }
    public void setVolume(int v)
    {
    stockVolume = v;
    }
    public void setPrice(double p)
    {
    stockPrice = p;
    }
    public String getSymbol()
    {
    return stockSymbol;
    }
    public int getVolume()
    {
    return stockVolume;
    }
    public double getPrice()
    {
    return stockPrice;
    }
    public void buyStock(int amtOfStock, double stockPrice)
    {
    stockVolume = amtOfStock + stockVolume;

    this.stockPrice = stockPrice;
    }
    public void sellStock(int amtOfStock, double stockPrice)
    {
    stockVolume = stockVolume - amtOfStock;

    this.stockPrice = stockPrice;
    }
    public String toString()
    {
    return(???);
    }
    }

    I have not run this yet because I haven't created a driver for the class, but I am assuming my getters and setter are fine, and I think my buy and sell methods will work too.

    Basically what I'm having trouble with is using my toString method and returning a nice looking string with the three given data members. Do I need to pass an argument through the toString method or can I somehow return these values within this class? Thanks.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: What to do... Simple Stock-Broker program

    A toString method should never take any arguments, really. And I'm not sure what you mean by "can I somehow return these values within this class".

  3. #3
    Avey is offline Member
    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Default Re: What to do... Simple Stock-Broker program

    Ok I got my toString method working... But now my driver class will not show that my buyStock and sellStock methods are working... Here is the edited Stock class:

    public class Stock {

    String stockSymbol;
    int stockVolume;
    double stockPrice;

    public Stock()
    {
    stockSymbol ="unknown";
    stockVolume = 0;
    stockPrice = 0;
    }

    public Stock(String s, int v, double p)
    {
    stockSymbol = s;
    stockVolume = v;
    stockPrice = p;
    }

    public void setSymbol(String s)
    {
    stockSymbol = s;
    }
    public void setVolume(int v)
    {
    stockVolume = v;
    }
    public void setPrice(double p)
    {
    stockPrice = p;
    }
    public String getSymbol()
    {
    return stockSymbol;
    }
    public int getVolume()
    {
    return stockVolume;
    }
    public double getPrice()
    {
    return stockPrice;
    }
    public void buyStock(int amtOfStock, double stockPrice)
    {
    if (amtOfStock > stockVolume)
    {
    stockVolume = stockVolume - amtOfStock;

    this.stockPrice = stockPrice;
    }
    else
    System.out.println("You cannot purchase more stock than what is the total volume of stock.");
    }
    public void sellStock(int amtOfStock, double stockPrice)
    {
    stockVolume = stockVolume + amtOfStock;

    this.stockPrice = stockPrice;
    }
    public String toString()
    {
    return("The volume of " + stockSymbol + " is: " + stockVolume + " and is priced at $" + stockPrice + " per share.");
    }
    }


    And this is the driver class, the goal is for the user to input the stock symbol(String) the volume of the stock(int) and it's price(double)... It also must give the user the option to buy or sell an amount of stock for a price they must input as well. Any reasons you guys can see why it won't print the new values of the stock price and its volume after choosing to buy or sell.

    import javax.swing.JOptionPane;


    public class Broker {


    public static void main(String[] args) {

    String stock1Symbol = JOptionPane.showInputDialog("What is the stock's symbol?");
    int stock1Volume = Integer.parseInt(JOptionPane.showInputDialog("What is the stock's volume? (an integer)"));
    double stock1Price = Double.parseDouble(JOptionPane.showInputDialog("Wh at is the stock's starting price?"));

    Stock stock1 = new Stock(stock1Symbol, stock1Volume, stock1Price);

    System.out.println(stock1.toString()); //initial stock info

    String userInput = JOptionPane.showInputDialog("Would you like to buy or sell your stock? (Please enter either 'buy' or 'sell'");

    //buy or sell or both...

    if (userInput == "buy")
    {
    int stockBought = Integer.parseInt(JOptionPane.showInputDialog("How many shares of stock would you like to purchase?"));

    double newStockPrice = Double.parseDouble(JOptionPane.showInputDialog("At what price are you selling these shares?"));

    stock1.buyStock(stockBought, newStockPrice);

    System.out.println(stock1.toString());

    String secondInput = JOptionPane.showInputDialog("Would you like to sell stock as well? (Enter 'yes' or 'no')");

    if (secondInput == "yes")
    {
    int stockSold = Integer.parseInt(JOptionPane.showInputDialog("How many shares of stock would you like to sell?"));

    double newerStockPrice = Double.parseDouble(JOptionPane.showInputDialog("At what price are you selling these shares?"));

    stock1.sellStock(stockSold, newerStockPrice);

    System.out.println(stock1.toString());
    }
    }

    if (userInput == "sell")
    {
    int stockSold = Integer.parseInt(JOptionPane.showInputDialog("How many shares of stock would you like to sell?"));

    double newStockPrice = Double.parseDouble(JOptionPane.showInputDialog("At what price are you selling these shares?"));

    stock1.sellStock(stockSold, newStockPrice);

    System.out.println(stock1.toString());

    String secondInput = JOptionPane.showInputDialog("Would you like to purchase stock as well? (Enter 'yes' or 'no')");

    if (secondInput == "yes")
    {
    int stockBought = Integer.parseInt(JOptionPane.showInputDialog("How many shares of stock would you like to purchase?"));

    double newerStockPrice = Double.parseDouble(JOptionPane.showInputDialog("At what price are you selling these shares?"));

    stock1.buyStock(stockBought, newerStockPrice);

    System.out.println(stock1.toString());

    }

    }

    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,199
    Rep Power
    19

    Default Re: What to do... Simple Stock-Broker program

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your posts accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  2. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 12:44 AM
  3. Need a Main for a Stock Managing program !!
    By Sary in forum New To Java
    Replies: 3
    Last Post: 05-30-2010, 07:57 PM
  4. Stock exchange program
    By askinne2 in forum New To Java
    Replies: 2
    Last Post: 05-02-2010, 11:25 PM
  5. Replies: 2
    Last Post: 04-17-2008, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •