Question: I need help with the additional bullet point instructions above. I'll leave the original problem below. ----------------------------------------------------------------- public class Order { private int unitPrice; //price

 I need help with the additional bullet point instructions above. I'll

I need help with the additional bullet point instructions above. I'll leave the original problem below.

-----------------------------------------------------------------

public class Order { private int unitPrice; //price per share private int quantity; //quantity of order private char orderType; //type of order - B for buy, S for sell /** * Constructor * */ public Order(int unitPrice, int quantity, char orderType) { this.unitPrice = unitPrice; this.quantity = quantity; this.orderType = orderType; }

/** * return the unitPrice */ public int getUnitPrice() { return unitPrice; }

/** * unitPrice to set */ public void setUnitPrice(int unitPrice) { this.unitPrice = unitPrice; }

/** * return the quantity */ public int getQuantity() { return quantity; }

/** * the quantity to set */ public void setQuantity(int quantity) { this.quantity = quantity; }

/** * return the orderType */ public char getOrderType() { return orderType; }

/** * the orderType to set */ public void setOrderType(char orderType) { this.orderType = orderType; } }

-----------------------------------------------------------------

public class Stack { private int top; //top of stack private int size; //size of stack private Order[] orders; //array to hold orders /** * Constructor */ public Stack(int size) { this.top = -1; this.size = size; this.orders = new Order[size]; } /** * return true if stack empty */ boolean isEmpty() { return (top==-1); } /** * return true if stack full */ boolean isFull() { return (top==(size-1)); } /** * pushes an order onto stack */ void push(Order order) throws Exception { if(!isFull()) { top++; orders[top]=order; } else throw new Exception("Overflow"); } /** * return a popped order from stack * */ Order pop() throws Exception { Order order=null; if(!isEmpty()) { order=orders[top]; top--; } else throw new Exception("Underflow"); return order; } }

-----------------------------------------------------------------

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner;

/** * Capital Gain/Loss calculator * */ public class CapitalGainLoss { private ArrayList transactions; //list of transactions private Stack buyOrders; //stack for buy orders private Stack sellOrders; //stack for sell orders /** * Default constructor */ public CapitalGainLoss() { transactions=new ArrayList(); } /** * method to accept transactions * */ public void acceptTransactions() throws IOException { String transaction; InputStreamReader reader=new InputStreamReader(System.in); BufferedReader bufferedReader=new BufferedReader(reader); while(!(transaction=bufferedReader.readLine()).equals("done")) { transactions.add(transaction); } } /** * process transactions * */ public void processTransactions() throws Exception { buyOrders=new Stack(transactions.size()); sellOrders=new Stack(transactions.size()); Iterator iterator=transactions.iterator(); while(iterator.hasNext()) { String transaction=(String)iterator.next(); Scanner scanner=new Scanner(transaction); char type = 0; int price = 0, quantity = 0; while(scanner.hasNext()) { String token=scanner.next(); if(token.equals("buy")) type='B'; else if(token.equals("sell")) type='S'; else if(token.matches("\\d+")) { quantity=Integer.parseInt(token); } else if(token.startsWith("$")) { String val=token.substring(1, token.length()); if(val.matches("\\d+")) { price=Integer.parseInt(val); } } } Order order = new Order(price, quantity, type); if(type=='B') buyOrders.push(order); else if(type=='S') sellOrders.push(order); scanner.close(); } }

/** * return total capital gain/loss * */ int calculateCapitalGainLoss() throws Exception { int amount=0; int totalBuyValue=0; int totalSellValue=0; int totalBuyQuantity=0; int totalSellQuantity=0; int sharesToSell=0; int balanceSharesToSell=0; int buyOrderQuantity=0; int sellOrderQuantity=0; int buyOrderPrice=0; int sellOrderPrice=0; while(!buyOrders.isEmpty()&&!sellOrders.isEmpty()) { Order buyOrder=buyOrders.pop(); Order sellOrder=sellOrders.pop(); if(buyOrder!=null) { buyOrderPrice=buyOrder.getUnitPrice(); buyOrderQuantity=buyOrder.getQuantity(); totalBuyValue+=buyOrderPrice*buyOrderQuantity; totalBuyQuantity+=buyOrderQuantity; } if(sellOrder!=null) { sellOrderPrice=sellOrder.getUnitPrice(); sellOrderQuantity=sellOrder.getQuantity(); if(sellOrderQuantity > buyOrderQuantity) { sharesToSell=buyOrderQuantity+balanceSharesToSell; totalSellValue+=sellOrderPrice*sharesToSell; balanceSharesToSell=sellOrderQuantity-buyOrderQuantity; totalSellQuantity+=sharesToSell; } else { totalSellValue+=sellOrderPrice*sellOrderQuantity; totalSellQuantity+=sellOrderQuantity; } } } amount=totalSellValue-totalBuyValue; return amount; } /** * main function */ public static void main(String[] args) throws Exception { CapitalGainLoss calc = new CapitalGainLoss(); System.out.println("Enter your transactions one per line."); System.out.println("Example: buy 100 share(s) at $20 each " + "sell 150 share(s) at $30 each."); System.out.println("Be very careful, while entering your transactions."); System.out.println("They should be in format as shown in example."); System.out.println("If not so, they won't be processed."); System.out.println("To stop, enter 'done'."); System.out.println("Transactions:"); try { calc.acceptTransactions(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Now processing your transactions..."); calc.processTransactions(); System.out.println("Processing of transactions complete!"); System.out.println("Now calculating capital gain(loss)..."); int amount=calc.calculateCapitalGainLoss(); System.out.println("Your gain(loss) is:"+amount); } }

-----------------------------------------------------------------

These is the original instructions in case you need to know what the code is for. leave the original problem below. ----------------------------------------------------------------- public class Order { private int

Project # 2: Calculating the Capital Gain Ne before class starts. Instructions: Chapter 6: P-6.36 Additional Instructions: . Your program should prompt the user to input a single string that represents a sequence of transactions. Use semicolon (';') to separate the transactions in your input string. For example, your program should support the following input string: buy 100 share (s) at $20 each; buy 20 share (s) at $24 each; buy 20o share (s) at $36 each;sel1 150 share (s) at $30 each; buy 50 share (s) at $25 each; sell 200 share (s) at $35 each;" The output of your program should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol as explained in P-6.36. For instance, the output for the example input should be: $1070. Your program should be able to handle wrong inputs. Consider using built-in Java classes, e.g., classes that implement the interface java.util.Queue Project submission: Save your work as a NetBeans project and send the entire project folder (a single zip file). The file should be sent to the instructor by email. Project # 2: Calculating the Capital Gain Ne before class starts. Instructions: Chapter 6: P-6.36 Additional Instructions: . Your program should prompt the user to input a single string that represents a sequence of transactions. Use semicolon (';') to separate the transactions in your input string. For example, your program should support the following input string: buy 100 share (s) at $20 each; buy 20 share (s) at $24 each; buy 20o share (s) at $36 each;sel1 150 share (s) at $30 each; buy 50 share (s) at $25 each; sell 200 share (s) at $35 each;" The output of your program should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol as explained in P-6.36. For instance, the output for the example input should be: $1070. Your program should be able to handle wrong inputs. Consider using built-in Java classes, e.g., classes that implement the interface java.util.Queue Project submission: Save your work as a NetBeans project and send the entire project folder (a single zip file). The file should be sent to the instructor by email

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!