Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could someone help me add on to my current code please with the criteria below? Java only, no SQL or anything like that. Criteria -

Could someone help me add on to my current code please with the criteria below? Java only, no SQL or anything like that.

Criteria -

"Extend the above implementation to include a Customer class that has Customer name, Current balance, and StocksInhand. The class should include a constructor that takes in name and balance. Note the stocksInhand is initialized to 0. The customer should have a buy and sell operation, in addition to a toString method to display the current state of the customer.

In the driver modify option 1 to create a stock or a customer. The operation to display displays the status of stocks and that of the customer. The other operations of buy and sell are on behalf of the customer. Complete the transaction based on the information provided. A buy by customer increases the stocksInhand and reduces the balance and reduces the volume in the stock and sets its new price.

At this point make sure that transactions should not be carried out if any of the criteria for buy or sell. Also, no transaction should be carried out if the new price is different by more than 5% of the current price."

Current Code -

Broker Class-

package assignment; import java.util.Scanner; import java.util.StringTokenizer; import javax.swing.JOptionPane;

public class broker {

public static void main(String[] args) { String menu = "Please choose an option: "; menu = menu + "Type 1 to create a stock "; menu = menu + "Type 2 to display the stock "; String option = JOptionPane.showInputDialog(menu); // creates Java menu int choice = Integer.parseInt(option); System.out.println("Please input an option."); while (choice != 9) // choice 9 for exit { if (choice == 1) { // need to create a stock System.out.println("You want to create a stock"); // request and get the information from the user String stockinfo = JOptionPane.showInputDialog("Please type in the symbol, price, and volume separated by space"); StringTokenizer stk = new StringTokenizer(stockinfo); String symbol = stk.nextToken(); // gets symbol token double price = Double.parseDouble(stk.nextToken()); // gets price token int volume = Integer.parseInt(stk.nextToken()); // gets volume token System.out.println(volume); // scanner object to take input @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); System.out.println("Enter symbol: "); // Stock object stock stock = new stock(symbol, volume, price); stock.currentState(); int buyVolume = 0; do { // options for users System.out.println(" 1. Buy"); System.out.println("2. Sell"); System.out.println("3. End"); System.out.print("Enter choice: "); choice = scanner.nextInt(); // gets user input switch(choice) { case 1: { System.out.print("Enter volume to buy: "); int number = scanner.nextInt(); // gets user input System.out.print("Enter the new price: "); price = scanner.nextDouble(); // gets user input stock.buy(number, price); // calls method from stock buyVolume += number; } break; case 2: { System.out.print("Enter volume to sell: "); int number = scanner.nextInt(); // gets user input while(number > buyVolume) { // checks to make sure the user doesn't enter a number larger than the value System.out.println("Available to sell is: " + buyVolume); System.out.println("Please re-enter sell amount."); // if so, it will loop back number = scanner.nextInt(); // gets user input } System.out.print("Enter the new price: "); // asks for the new price price = scanner.nextDouble(); // gets user input stock.sell(number, price); // calls method from stock.java buyVolume -= number; } break; case 3: // ends the program and displays current state of the stock { System.out.println("Ending the program."); } } } while(choice != 3); stock.currentState();

} } } }

Stock Class-

package assignment; public class stock { private String symbol; // symbol private int volume; // initial volume private double price; // initial price private double value; // initial value (volume * price) // Simple Constructor // Sets symbol, and initial values of the stock public stock(String symbol, int volume, double price) { this.symbol = symbol; this.volume = volume; this.price = price; } // this method allows the user to buy stock, and update the price // uses number(volume amount) and price of stock. public void buy(int number, double price) { System.out.println("User selects Buy \t" + number + String.format("\t$ %.2f", price)); this.volume = this.volume - number; this.price = price; } // this method allows the user to sell stock and update the price // used number(volume amount) and price of stock public void sell(int number, double price) { System.out.println("User selects Sell \t" + number + "\t$ " + String.format("%.2f", price)); this.volume = this.volume + number; this.price = price; } // this method will print the current state of the stock using previous variables in a neatly formatted manner public void currentState() { System.out.println("Output of Current State:"); System.out.println("\t\t\tSymbol\t" + this.symbol); System.out.println("\t\t\tVolume\t" + this.volume); System.out.println("\t\t\tPrice\t$ " +String.format("%.2f", this.price)); this.value = this.price * this.volume; System.out.println("\t\t\tValue\t" + String.format("$ %.2f", this.value)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Make A Database In Historical Studies

Authors: Tiago Luis Gil

1st Edition

3030782409, 978-3030782405

Students also viewed these Databases questions