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

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!
 
 
 
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; }
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;
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 6 images

Blurred answer
Knowledge Booster
Processes of 3D Graphics
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