Stock broker

Report a typo

Suppose you are creating a stockbroker application. It performs two commands which are buy and sell. Use the command pattern to implement this application. Use the following guidelines:

  • Don't change the provided code.
  • The sell command must print Stock was sold.
  • The buy command must print Stock was bought.
  • First, it must perform a buy command, then the sell command.

Sample Input 1:

Sample Output 1:

Stock was bought
Stock was sold
Write a program in Java 17
public class Main {

public static void main(String[] args) {

/* write your code here */

broker.setCommand(buyCommand);
broker.executeCommand();

broker.setCommand(sellCommand);
broker.executeCommand();
}
}


class Stock {

public void buy() {
System.out.println("Stock was bought");
}

public void sell() {
System.out.println("Stock was sold");
}
}

interface Command {
/* write your code here */
}

class BuyCommand implements Command {
private Stock stock;

public BuyCommand(Stock stock) {
this.stock = stock;
}
___

Create a free account to access the full topic