Implement the following:  1. Add the missing functionality in “controller.java” to finish the “sellLIFO” and “sellFIFO” methods. 2. Provide error checking so users can’t sell a stock they don’t own or try to sell more stock than they own. Eliminate that runtime exception. 3. We think its sort of limited to only work with just two stocks. You need to change the user input, so the user enters the stock name they wish to buy or sell instead of just choosing between Google and Amazon. Keep the command line interface since we are only in the testing phase.  4. We like the layout and design of the application so don’t change any of the other methods or objects. Only edit the Controller.java file!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given the following code in Java and attached images( containing related code):

//Controller (ONLY EDIT THE Controller.java file)
import java.util.LinkedList;
import java.util.Scanner;

public class Controller {
   
    public Controller() {
        LinkedList<Stock> googList = new LinkedList<Stock>();
        LinkedList<Stock> amazList = new LinkedList<Stock>();
        Scanner input = new Scanner(System.in);
       
        do {
            System.out.print("Enter 1 for Google stock or 2 for Amazon, 3 to quit: ");
            int stockSelect = input.nextInt();
            if(stockSelect == 3)
                break;
            System.out.print("Input 1 to buy, 2 to sell: ");
            int controlNum = input.nextInt();
            System.out.print("How many stocks: ");
            int quantity = input.nextInt();
           
            if(controlNum == 1) {
                System.out.print("At what price: ");
                double price = input.nextDouble();
                if(stockSelect == 1) {
                    Controller.buyStock(googList, "Google", quantity, price);
                }
                else
                    Controller.buyStock(amazList, "Amazon", quantity, price);
            }
            else {
                System.out.print("Press 1 for LIFO accounting, 2 for FIFO accounting: ");
                controlNum = input.nextInt();
                if(controlNum == 1) {
                    if(stockSelect == 1)
                        Controller.sellLIFO(googList, quantity);
                    else
                        Controller.sellLIFO(amazList, quantity);
                }
                else {
                    if(stockSelect == 1)
                        Controller.sellFIFO(googList, quantity);
                    else
                        Controller.sellFIFO(amazList, quantity);
                }
            }
           
        } while(true);
        input.close();
    }
           
    public static void buyStock(LinkedList<Stock> list, String name, int quantity, double price) {
        Stock temp = new Stock(name,quantity,price);
        list.push(temp);
        System.out.printf("You bought %d shares of %s stock at $%.2f per share %n", quantity, name, price);
    }
   
    public static void sellLIFO(LinkedList<Stock> list, int numToSell) {
        // You need to write the code to sell the stock using the LIFO method (Stack)
        // You also need to calculate the profit/loss on the sale
        double total = 0; // this variable will store the total after the sale
        double profit = 0; // the price paid minus the sale price, negative # means a loss
        System.out.printf("You sold %d shares of %s stock at %.2f per share %n", numToSell, list.element().getName(), total/numToSell);
        System.out.printf("You made $%.2f on the sale %n", profit);
    }
   
    public static void sellFIFO(LinkedList<Stock> list, int numToSell) {
        // You need to write the code to sell the stock using the FIFO method (Queue)
        // You also need to calculate the profit/loss on the sale
        double total = 0; // this variable will store the total after the sale
        double profit = 0; // the price paid minus the sale price, negative # means a loss
        System.out.printf("You sold %d shares of %s stock at %.2f per share %n", numToSell, list.element().getName(), total/numToSell);
        System.out.printf("You made $%.2f on the sale %n", profit);
    }
   
   
}
 
//Driver
public class Driver {

    public static void main(String[] args) {
        System.out.println("Java Stock Exchange");
        new Controller();
       
    }

}
 
//Images attached are in order with following code
//Stock
public Stock(int quantity, double price) {
        this.name = "undefined";
        this.quantity = quantity;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
   
    public String toString() {
        return "Stock: " + this.getName() + "  Quantity: " + this.getQuantity() + "  Price: " + this.getPrice();
    }
}
Implement the following: 
1. Add the missing functionality in “controller.java” to finish the
“sellLIFO” and “sellFIFO” methods.

2. Provide error checking so users can’t sell a stock they don’t own or
try to sell more stock than they own. Eliminate that runtime exception.

3. We think its sort of limited to only work with just two stocks. You
need to change the user input, so the user enters the stock name they
wish to buy or sell instead of just choosing between Google and
Amazon. Keep the command line interface since we are only in the
testing phase. 

4. We like the layout and design of the application so don’t change any
of the other methods or objects. Only edit the Controller.java file!
 
 
 
29
30
31
32
33
34
35
36
37
38
39
` 48 4 2 3 4 5 46 47 48
40
41
42
43
44
49
50
51
52
public Stock(int quantity) {
this.name = "undefined";
this.quantity = quantity;
this.price = 0.0;
}
public Stock (double price) {
this.name = "undefined";
}
public Stock(String name, int quantity) {
this.name = name;
this.quantity = quantity;
this.price = 0.0;
}
this.quantity = 0;
this.price = price;
public Stock(String name, double price) {
}
this.name = name;
this.quantity = 0;
this.price = price;
Transcribed Image Text:29 30 31 32 33 34 35 36 37 38 39 ` 48 4 2 3 4 5 46 47 48 40 41 42 43 44 49 50 51 52 public Stock(int quantity) { this.name = "undefined"; this.quantity = quantity; this.price = 0.0; } public Stock (double price) { this.name = "undefined"; } public Stock(String name, int quantity) { this.name = name; this.quantity = quantity; this.price = 0.0; } this.quantity = 0; this.price = price; public Stock(String name, double price) { } this.name = name; this.quantity = 0; this.price = price;
5
600 0 HN 3 4 5 6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
12345678
22
public class Stock {
private String name;
private int quantity;
private double price;
public Stock() {
}
public Stock (String name, int quantity, double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
this.name = "undefined";
this.quantity = 0;
this.price = 0.0;
public Stock(String name) {
this.name = name;
this.quantity = 0;
this.price = 0.0;
}
Transcribed Image Text:5 600 0 HN 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 12345678 22 public class Stock { private String name; private int quantity; private double price; public Stock() { } public Stock (String name, int quantity, double price) { this.name = name; this.quantity = quantity; this.price = price; } this.name = "undefined"; this.quantity = 0; this.price = 0.0; public Stock(String name) { this.name = name; this.quantity = 0; this.price = 0.0; }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 6 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Given the code: 

import java.util.HashMap;

import java.util.LinkedList;

import java.util.Scanner;

 

public class Controller {

privateHashMap<String,LinkedList<Stock>>stockMap;

 

publicController(){

stockMap=newHashMap<>();

Scannerinput=newScanner(System.in);

 

do{

// Prompt for stock name or option to quit

System.out.print("Enter stock name or 3 to quit: ");

StringstockName=input.next();

if(stockName.equals("3")){

break;// Exit if user inputs '3'

}

 

// Get or create a list for the specified stock

LinkedList<Stock>stockList=stockMap.computeIfAbsent(stockName,k->newLinkedList<>());

 

// Prompt to buy or sell

System.out.print("Input 1 to buy, 2 to sell: ");

intcontrolNum=input.nextInt();

System.out.print("How many stocks: ");

intquantity=input.nextInt();

 

if(controlNum==1){

// Buying stocks

System.out.print("At what price: ");

doubleprice=input.nextDouble();

buyStock(stockList,stockName,quantity,price);

}else{

// Selling stocks

System.out.print("Press 1 for LIFO accounting, 2 for FIFO accounting: ");

controlNum=input.nextInt();

if(controlNum==1){

// Sell using LIFO method

sellLIFO(stockList,quantity);

}else{

// Sell using FIFO method

sellFIFO(stockList,quantity);

}

}

 

}while(true);

 

input.close();// Close the scanner

}

 

publicstaticvoidbuyStock(LinkedList<Stock>list,Stringname,intquantity,doubleprice){

// Create a new stock object and add it to the list

Stocktemp=newStock(name,quantity,price);

list.push(temp);

// Display purchase details

System.out.printf("You bought %d shares of %s stock at $%.2f per share %n",quantity,name,price);

}

 

publicstaticvoidsellLIFO(LinkedList<Stock>list,intnumToSell){

// Check if there are enough stocks to sell

inttotalQuantity=list.stream().mapToInt(Stock::getQuantity).sum();

if(totalQuantity<numToSell){

System.out.println("Not enough stocks to sell.");

return;

}

 

doubletotal=0;

doubleprofit=0;

intremainingToSell=numToSell;

 

while(remainingToSell>0){

// Retrieve and sell stocks using LIFO

StocklastStock=list.peek();

if(lastStock.getQuantity()<=remainingToSell){

remainingToSell-=lastStock.getQuantity();

total+=lastStock.getPrice()*lastStock.getQuantity();

profit+=lastStock.getPrice()*lastStock.getQuantity();

list.pop();// Remove stock from the list

}else{

lastStock.setQuantity(lastStock.getQuantity()-remainingToSell);

total+=lastStock.getPrice()*remainingToSell;

profit+=lastStock.getPrice()*remainingToSell;

remainingToSell=0;

}

}

 

// Display sale details

System.out.printf("You sold %d shares of %s stock at %.2f per share %n",numToSell,list.peek().getName(),total/numToSell);

System.out.printf("You made $%.2f on the sale %n",profit);

}

 

publicstaticvoidsellFIFO(LinkedList<Stock>list,intnumToSell){

// Check if there are enough stocks to sell

inttotalQuantity=list.stream().mapToInt(Stock::getQuantity).sum();

if(totalQuantity<numToSell){

System.out.println("Not enough stocks to sell.");

return;

}

 

doubletotal=0;

doubleprofit=0;

intremainingToSell=numToSell;

 

while(remainingToSell>0){

// Retrieve and sell stocks using FIFO

StockfirstStock=list.peekLast();// Get the first element (FIFO)

if(firstStock.getQuantity()<=remainingToSell){

remainingToSell-=firstStock.getQuantity();

total+=firstStock.getPrice()*firstStock.getQuantity();

profit+=firstStock.getPrice()*firstStock.getQuantity();

list.removeLast();// Remove stock from the list (FIFO)

}else{

firstStock.setQuantity(firstStock.getQuantity()-remainingToSell);

total+=firstStock.getPrice()*remainingToSell;

profit+=firstStock.getPrice()*remainingToSell;

remainingToSell=0;

}

}

 

// Display sale details

System.out.printf("You sold %d shares of %s stock at %.2f per share %n",numToSell,list.peekLast().getName(),total/numToSell);

System.out.printf("You made $%.2f on the sale %n",profit);

}

}

 

Rewrite without using Hashmap, but just stick with the LinkedList instead! The output should display the profit made from sales!

If I bought 30 stocks of Google for 120 then I decide to sell 5 Google stocks at 140, then the output should display the gained profit!

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

It didn't solve my question, the console should ask how many stocks I want to sell and at what price. Then it should print out the amount made from selling the shares. When it gives me the option to select 1 and 2 for sellLIFO and sellFIFO it gives "Not enough stocks to sell" which should not be the case if I have 20 stocks bought and I want to sell 3!

Please fix!

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

The code using only the LinkedList doesn't produce the right output now, it gives this again. Refer to image

The output should produce the amount made from the shares using only the LinkedList instead of the Hashmap.

Java Stock Exchange
Enter stock name or 3 to quit: Google
Input 1 to buy, 2 to sell: 1
How many stocks: 10
At what price: 120
You bought 10 shares of Google stock at $120.00 per share
Enter stock name or 3 to quit: Google
Input 1 to buy, 2 to sell: 2
How many stocks: 5
Press 1 for LIFO accounting, 2 for FIFO accounting: 2
Not enough stocks to sell.
Enter stock name or 3 to quit:
Transcribed Image Text:Java Stock Exchange Enter stock name or 3 to quit: Google Input 1 to buy, 2 to sell: 1 How many stocks: 10 At what price: 120 You bought 10 shares of Google stock at $120.00 per share Enter stock name or 3 to quit: Google Input 1 to buy, 2 to sell: 2 How many stocks: 5 Press 1 for LIFO accounting, 2 for FIFO accounting: 2 Not enough stocks to sell. Enter stock name or 3 to quit:
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Thank you, it has the correct output now! 

Last thing, can this be done without using a Hashmap, but instead with the original method of using just the LinkedList in the original question? If so, please provide another solution without using hashmap to see the difference in structure of the code! 

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

The code is not working the way it should be. If I buy 20 stocks, I should be able to sell 3. The output should be the profit loss/gain not "Not enough stocks to sell".

Refer to the image to see the problem

Java Stock Exchange
Enter stock name or 3 to quit: Google
Input 1 to buy, 2 to sell: 1
How many stocks: 20
At what price: 120
You bought 20 shares of Google stock at $120.00 per share
Enter stock name or 3 to quit: Google
Input 1 to buy, 2 to sell: 2
How many stocks: 3
Press 1 for LIFO accounting, 2 for FIFO accounting: 1
Not enough stocks to sell.
Transcribed Image Text:Java Stock Exchange Enter stock name or 3 to quit: Google Input 1 to buy, 2 to sell: 1 How many stocks: 20 At what price: 120 You bought 20 shares of Google stock at $120.00 per share Enter stock name or 3 to quit: Google Input 1 to buy, 2 to sell: 2 How many stocks: 3 Press 1 for LIFO accounting, 2 for FIFO accounting: 1 Not enough stocks to sell.
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Fundamentals of system reliability
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education