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.