Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with a beginning level stock program

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with a beginning level stock program

    Hi there so I have been working on a programming assignment dealing with stocks. Here is what we have to do for the program:

    You will need to use files, arrays, and classes stock, customer, and broker from assignment 3(b).

    Modify the broker class read input files called stock.dat and customer.dat

    The first line in stock.dat has an integer that indicates how many stocks are described in the file.

    Subsequently, each stock is described as follows, one stock description per line.

    Stock label (String) initial volume (int) price (double)

    Modify the stock class and create stock objects (in Broker) from the descriptions read from the stock.dat file.

    The first line in customer.dat has an integer that indicates how many customers are described in the file.

    Subsequently, each customer is described as follows, one customer at a time

    Customer Name Account Balance on the top line:

    Modify the customer class and create customer objects (in Broker) from the descriptions read from the customer.dat file. Note that all customers
    start with empty portfolio and can have a portfolio that contains many different stocks – as many as described in the stock.dat class.

    Then,

    First – displays the Current state of all the stocks. Then displays the all the customers and their portfolio.

    Then, provide a menu for the user to:

    Select a customer, view their portfolio and or trade stocks as in 3(b).

    Until the session is terminated when exit option.

    Finally – display the current state of all the stocks. Then display all the customers and their current portfolio.

    This is what I have so far:
    I have three seperate classes.

    The Broker class looks like this:

    import java.util.Scanner;

    public class broker3c {

    public static void main(String[] args) {

    Customer[] mycustomers;

    stock[] mystocks;

    File inpfile= new File("Stock.dat");

    Scanner stkfile = new Scanner (finp1);

    int numberofstock = stkfile.nextInt();

    mystocks = new stock(numberofstock);

    for (int i= 0; i < numberofstock; i++)
    {
    String stkname = stkfile.next();
    int volume = stkfile.nextInt();
    double price = stkfile.nextDouble();

    stock s = new stock(stkname, price, volume);

    mystocks(i) = s;

    }


    // read the stocks from the stock.dat file and have them ready

    File inpfile2 = new File("customer.dat");

    FileInputStream finp2 = new FileInputStream(inpfile2);

    Scanner custfile = new Scanner(finp2);


    int numberofcustomers = custfile.nextInt();

    mycustomers = new Customer[numberofcustomers];

    for (int i = 0; i < numberofcustomers; i++)
    {
    String custname = custfile.next();

    double amount = custfile.next();

    Customer c = new Customer(custname, amount);

    mycustomers[i] = c;
    }

    int trade;

    trade = JOptionPane.showConfirmDialog(null, "Do you want to trade?");

    while (trade == JOptionPane.YES_OPTION)
    {

    int cust = Integer.parseInt(JOptionPane.showInputDialog("Type in the customer number: "));

    int stknum = Integer.parseInt(JOptionPane.showInputDialog("Type in the stock number: "));

    int buysell = Integer.parseInt(JOptionPane.showInputDialog("type 1 for buy and 2 for sell"));

    int numofstks = Integer.parseInt(JOptionPane.showInputDialog("How many stocks to buy/sell"));

    double price = Double.parseDouble(JOptionPane.showInputDialog(" at what price to trade));

    if (numofstk >= price)
    {
    // check if the price of the stock is within the limits
    for

    // check if the cust wants to sell - if he has that many to sell
    int sell = Integer.parseInt(JOptionPane.showInputDialog("Woul d you like to sell?"));

    while (sell == JOptionPane.YES_OPTION)
    {

    }

    while (sell == JOptionPane.NO_OPTION)
    {

    }
    // check if the cust wants to buy - if he has that much money to buy
    int buy = Integer.parseInt(JOptionPane.showInputDialog("Woul d you like to buy? "));

    while (buy == JOptionPane.YES_OPTION)
    {

    }

    while (buy == JOptionPane.NO_OPTION)
    {

    }

    // if all kapish then go and do the trade : make changes in the cust balance, portfolio, and in mystock
    }
    trade = JOptionPane.showConfirmDialog(null, "Do you want to trade?");
    }

    }

    }


    The customer class looks like this:

    public class Customer {

    String cname;
    double balance;

    stock[] portfolio;

    public Customer(String name, double bal, stock[] port)
    {
    cname = name;
    balance = bal;

    portfolio = port;


    }

    public String toString()
    {
    String outstring;

    outstring = " The customer name is " + cname;

    outstring = outstring + " \n" + " with balance " + balance;


    return outstring;
    }


    }


    The Stock class looks like this:

    import javax.swing.*;

    import java.util.*;
    import java.text.*;

    public class Stock{

    public static void main(String []args) {



    int volume;
    double intial_stock_price;

    double price;
    double number;


    String userinput;

    String stock_symbol;

    {

    stock_symbol = JOptionPane.showInputDialog("Please input a symbol: ");

    volume = Integer.parseInt(JOptionPane.showInputDialog("Plea se input the initial volume (numerical only): "));

    intial_stock_price = Double.parseDouble(JOptionPane.showInputDialog("Pl ease input a start-off price (numberical only): "));

    number = Double.parseDouble(JOptionPane.showInputDialog("Pl ease input the number(numerical only): "));

    price = intial_stock_price*volume;

    JOptionPane.showMessageDialog(null,"Symbol: " + stock_symbol + "\n Volume: " + volume + "\n Price: " + price + "\n Value: " + price*volume);

    userinput = JOptionPane.showInputDialog("Please input 1 for selling stock or 2 for buying stock.");


    }

    JOptionPane.showMessageDialog(null, "The final stock information is: " + "\n Symbol: " + stock_symbol + "\n Volume: " + volume + "\n Price: " + price + "\n Value: " + price*volume);

    }
    }


    I also have two .dat files, stock and customer.

    The stock.dat is:

    String Stock_Label
    int Intial_Volume
    double Price

    2
    msft 10000 20.5
    fast 29990 45.32

    The customer.dat is:

    4
    iyengar 595959
    ryan 54545
    chris 74747
    kennedy 7747474


    If someone could tell me if I'm heading in the right direction, any corrections, and what needs to be done next that would be great. Thank you.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need help with a beginning level stock program

    Waaaay to long, got bored, have no idea what your problem is.
    Improving the world one idiot at a time!

Similar Threads

  1. stock charts...
    By theChameleon in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 05:11 AM
  2. stock screener api java
    By thosecars82 in forum Java SE APIs
    Replies: 0
    Last Post: January 31st, 2010, 09:50 AM
  3. Error in printing method as always it prints the last value
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 1st, 2009, 10:37 AM
  4. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM
  5. Java stock project help
    By lotus in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 12th, 2009, 04:16 AM

Tags for this Thread