Java Stock program Stock prices for companies fluctuate multiple times during ea
ID: 3596438 • Letter: J
Question
Java Stock program
Stock prices for companies fluctuate multiple times during each day of trading. In this project we are going to write software for a financial company that keeps track of the stock prices. Following are the features provided by the company:
1. Register a client (business): Create unique ID and password, Name of Business, Current Stock price
2. Login a registered client: Upon successful login display the Name of the business, and the current stock price; login works only after registering
3. Provide option to log all the changes to the stock price, by adding the latest stock price and the time of the change
4. For the current day, display all the prices in reverse chronological order i.e., starting with the current price to the beginning price of the day
5. For the current day display the highest stock price that was attained, and the time
6. For the current day display the lowest stock price, and the time
7. Quit the session signaling the end of the day; the current stock price should be saved for the Business, so that when logging in the next day, that becomes the current stock price. Businesses open at 10:00 AM, so that should be the time of change for the first price of the day The program should be menu-driven; so until the user selects option 7 i.e., Quit, the program should keep executing. The program begins by registering a client if using the system for the first time; while registering only unique client ids are allowed and password must be at least 8 characters long. If the client cannot choose a unique id after 2 attempts, generate a random 8-character alphanumeric user id based on the name of the business. For clients previously registered, option 2 can be chosen directly.
Explanation / Answer
Client.java:
package pro;
public class Client
{
private String uniqueID;
private String password;
private String businessName;
private Stacks<Stock> userList;
public Client()
{
this.uniqueID = "";
this.password = "";
this.businessName = "";
userList = new Stacks<Stock>();
}
public Client(String uniqueID, String password, String businessName)
{
this.uniqueID = uniqueID;
this.password = password;
this.businessName = businessName;
userList = new Stacks<Stock>();
}
public String getUniqueID()
{
return uniqueID;
}
public void setUniqueID(String uniqueID)
{
this.uniqueID = uniqueID;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getBusinessName()
{
return businessName;
}
public void setBusinessName(String businessName)
{
this.businessName = businessName;
}
public void addStock(Stock stock)
{
userList.push(stock);
}
public Stacks<Stock> returnStockList()
{
return userList;
}
}
RegisterClient.java:
package pro;
import java.io.*;
import java.security.SecureRandom;
import java.util.Scanner;
public class RegisterClient
{
public static final void register(Client client) throws IOException
{
Scanner kb = new Scanner(System.in);
Scanner kb1 = new Scanner(System.in);
String scInput1, scInput2, scInput3;
File fout = new File("userdata.txt");
if(!fout.exists())
fout.createNewFile();
FileWriter file = new FileWriter(fout, true);
BufferedWriter writer = new BufferedWriter(file);
System.out.println("Enter a Unique ID: ");
scInput1 = kb.next();
client.setUniqueID(scInput1);
System.out.println("Enter a password: ");
scInput2 = kb.next();
client.setPassword(scInput2);
System.out.println("Enter business name: ");
scInput3 = kb.next();
client.setBusinessName(scInput3);
System.out.println("Enter the Stock price: ");
String stockPrice = kb1.next();
System.out.println("Enter the Stock clock: ");
String clock = kb1.next();
Stock stock = new Stock(stockPrice, clock);
client.addStock(stock);
writer.write(client.getUniqueID()+" "+ client.getPassword()+" "+ client.getBusinessName()+" ");
writer.close();
}
public static final String gerenrateUniqueID(String businessName, int rand){
SecureRandom randomID = new SecureRandom();
StringBuilder builder = new StringBuilder();
String charset = businessName;
for(int i = 0; i < 8; i++){
builder.append(charset.charAt(randomID.nextInt(rand)));
}
return builder.toString();
}
}
StackInterface.java:
package pro;
public interface StackInterface<E> {
E push(E obj);
E pop();
E peek();
boolean empty();
}
Stacks.java:
package pro;
public class Stacks<E> implements StackInterface<E> {
private E[] theData; //storage for the stack
private int topOfStack = -1; //Top of stack
private static final int INITIAL_CAPACITY = 10;
//Constructor
public Stacks(){
this.theData = ((E[]) new Object[INITIAL_CAPACITY]);
}
//Overload Constructor for user defined size
public Stacks(int size){
this.theData = ((E[]) new Object[size]);
}
//This method will add an element to the end of the stack
@Override
public E push(E obj){
//check if the stack is full
if(this.topOfStack == this.theData.length){
System.out.println("Stack Overflow!");
return null;
}
//else if there is space on the stack insert the data
else{
this.topOfStack++;
this.theData[topOfStack] = obj;
return obj;
}
}
public boolean empty(){
return (this.topOfStack == -1);
}
public E pop(){
//check if the stack is not empty
if(this.empty()){
System.out.println("Stack Underflow!");
return null;
}
else{
return this.theData[topOfStack--];
}
}
public E peek(){
if(this.empty()){
System.out.println("Stack Empty!");
return null;
}
return this.theData[topOfStack];
}
}
Stock.java:
package pro;
public class Stock {
private String stockPrice;
private String clock;
public Stock() {
this.stockPrice = "";
this.clock = "";
}
public Stock(String stockPrice, String clock) {
this.stockPrice = stockPrice;
this.clock = clock;
}
public String getStockPrice() {
return stockPrice;
}
public void setStockPrice(String stockPrice) {
this.stockPrice = stockPrice;
}
public String getClock() {
return clock;
}
public void setClock(String clock) {
this.clock = clock;
}
}
StockExchange.java:
import java.util.*;
public class StockExchange
{
static int cont()
{
int choice=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter "
+ "1.Registration "
+ "2.Login "
+ "3.change stock price "
+ "4.Prices list "
+ "5.highest stock price "
+ "6.lowest stock price "
+ "7.exit");
choice=sc.nextInt();
return choice;
}
public static void main(String[] args)
{
Stack info=new Stack();
String businessname;
double stockprice;
String id="";
String password="";
int count=0;
Map<String,String> logininfo=new HashMap();
Scanner sc=new Scanner(System.in);
switch(cont())
{
case 1:
System.out.println("Enter businessname & current stock price");
businessname= sc.next();
stockprice=sc.nextDouble();
System.out.println("Enter password for login");
password=sc.next();
System.out.println("Enter uniqueid for login");
for(int k=1;k<=2;k++)
{
id=sc.nextLine();
count++;
}
if(id.isEmpty() & count==2)
{
int b=(int)Math.floor(Math.random()*10000);
id=businessname.substring(0,4)+b;
logininfo.put(id,password);
}
else
logininfo.put(id,password);
info.add(businessname);
info.add(stockprice);
info.add(id);
info.add(password);
System.out.println("Successfully Register please note down ID and Password");
System.out.println("your details");
System.out.println(logininfo);
cont();
case 2:
System.out.println("enter id and password");
String id1=sc.next();
String pass=sc.next();
if(id1.equals(id) & pass.equals(password))
{
System.out.println("Login Successfully:");
System.out.println("Login Details Given Below:");
System.out.println(info);
}
cont();
case 7: System.exit(0);
}
}
}
file.txt:
83736 83737373628 Dell.com
3234 3434 ewr
44443434 3434 rwerr
434 3434 3434
32323 32424144 adada
23333 323313dad 22aaea
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.