Hey professor,
I was working on this code and I'm stuck between a rock and a hard place. For the life of me, I cannot figure out how to do this. I have a method labeled "buyRandomStock(Agent a)". I'll link the file of the entire class. Any advice will be helpful, thank you.
What I want to accomplish:
-have a method with randomly generated purchase amounts used to create a certain amount of stocks to purchase
-then use that purchase amount method with another method to create an allocated amount for each stock by dividing the budget equally
-then use the allocated amount and divide that by the stock price of each selected stock to come up with amount of that stock bought
-I then want to be able to use that data and put it in the portfolio and use it as like receipts for different agents (for example, when i print it, i want it to be something like "System.out.println("Agent : ABCD purchased Stocks:..."). You get the idea. Thank you for your time.
Best,
Justin Trujillo
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Portfolio {
ArrayList portfolio = new ArrayList();
ArrayList index = new ArrayList();
ArrayList agentIndex = new ArrayList();
/**
*
* RECORD OF PURCHASES OF STOCK
*/
public Portfolio()
{
}
private void buyRandomStock(Agent a)
{
Random r = new Random();
double x = (a.createRandomPurchaseAmount());
for(int i = 1; i <=x; i++)
{
Stock l = index.get(r.nextInt(6000 - 1) + 1);
double stockPrice = l.getPrice();
double NumberOfStocksBought = (a.createAllocationAmount() / stockPrice);
portfolio.addAll(NumberOfStocksBought); //Stuck here
}
}
public void addStockToPortfolio(Transaction t)
{
portfolio.add(t);
}
/**
*
* print the portfolio
*/
public void printPortfolio()
{
// System.out.println(Arrays.toString(portfolio));
for (int i = 0; i < portfolio.size(); i++)
System.out.println("\nThis is the ID of your transaction: " + portfolio.get(i));
}
//public void transactionInfo(String l, ArrayList<Stock> index)
//{
// Transaction t = new Transaction();
// for(Stock s : index)
// System.out.println("\nUser has purchased stock: " + s.getSymbol() +
// "\n Quantity: " + t.getNumberBought() +
// "\nTotal Price: " + "\n\nThank you for your purchase.");
//}
public List<Transaction> getAll()
{
return new ArrayList<Transaction>(portfolio);
}
}
Hey professor,
I was working on this code and I'm stuck between a rock and a hard place. For the life of me, I cannot figure out how to do this. I have a method labeled "buyRandomStock(Agent a)". I'll link the file of the entire class. Any advice will be helpful, thank you.
What I want to accomplish:
-have a method with randomly generated purchase amounts used to create a certain amount of stocks to purchase
-then use that purchase amount method with another method to create an allocated amount for each stock by dividing the budget equally
-then use the allocated amount and divide that by the stock price of each selected stock to come up with amount of that stock bought
-I then want to be able to use that data and put it in the portfolio and use it as like receipts for different agents (for example, when i print it, i want it to be something like "System.out.println("Agent : ABCD purchased Stocks:..."). You get the idea. Thank you for your time.
Best,
Justin Trujillo
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Portfolio {
ArrayList portfolio = new ArrayList();
ArrayList index = new ArrayList();
ArrayList agentIndex = new ArrayList();
}