Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*JAVA* Why is the following program giving a runtime error. Show modified code import java.io.File; import java.util.Scanner; import java.util.Stack; public class Order { String vendor,

*JAVA*

Why is the following program giving a runtime error. Show modified code

import java.io.File; import java.util.Scanner; import java.util.Stack; public class Order { String vendor, type; int quantity, quantityServed; double costPerWidget, costPaid; Order(String s) { String[] list = s.split (" "); if (list[0].equals ("I")) { type = list[0]; quantity = Integer.parseInt (list[1]); costPerWidget = Double.parseDouble (list[2]); vendor = ""; for (int i = 3; i < list.length; i++) { vendor += list[i] + " "; } } else { type = list[0]; quantity = Integer.parseInt (list[1]); vendor = ""; for (int i = 2; i < list.length; i++) { vendor += list[i] + " "; } } } double getCostByQuantity(int q) { } public String toString() { if (type.equals ("I")) { return vendor + " $" + ((quantity + quantityServed) * costPerWidget) + " "; } return "Shipment to " + vendor + "" + quantityServed + " units for $" + costPaid + " "; } } class Main { public static void main(String[] args) { try { String fileName = "src/Warehouse.txt"; // provide complete file path Scanner sc = new Scanner (new File (fileName)); // to read inputs Stack st = new Stack<> (); // stack of type Order double profit = 0; // current profit is 0 String outputOrder = "Orders: ", outputSupplier = "Summary: Suppliers "; // string for orders and suppliers while (sc.hasNextLine ()) { String line = sc.nextLine (); Order order = new Order (line); if (order.type.equals ("I")) { // it is shipment IN order st.push (order); outputSupplier += order.toString (); } else { while (!st.empty ()) { Order topOrder = st.pop (); // top order on stack if (topOrder.quantity <= order.quantity) { int unitsServed = topOrder.quantity; // we can serve this many quantity for current order order.costPaid += topOrder.getCostByQuantity (unitsServed); // cost paid by order // add total profit by subtracting cost it has paid profit += (topOrder.getCostByQuantity (unitsServed) - unitsServed * topOrder.costPerWidget); topOrder.quantityServed += unitsServed; topOrder.quantity = 0; order.quantityServed += unitsServed; order.quantity -= unitsServed; if (st.empty ()) { outputOrder += order.toString (); } if (order.quantity == 0) { break; } } else { int unitsServed = order.quantity; order.costPaid += topOrder.getCostByQuantity (unitsServed); profit += (topOrder.getCostByQuantity (unitsServed) - unitsServed * topOrder.costPerWidget); topOrder.quantityServed += unitsServed; topOrder.quantity -= unitsServed; order.quantityServed += unitsServed; order.quantity = 0; outputOrder += order.toString (); st.push (topOrder); break; } } } } int remaining = 0; while (!st.empty ()) { // count remaining quantities remaining += st.pop ().quantity; } // printing details to console outputSupplier += "There are " + remaining + " widgets in the warehouse. "; outputSupplier += "Profit is $" + profit; System.out.println (outputOrder); System.out.println (outputSupplier); } catch(Exception e){ System.out.printf ("Runtime Error"); } } }

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions

Question

=+ Are they breakable for any reason?

Answered: 1 week ago

Question

=+When and under what circumstances are contracts renegotiated?

Answered: 1 week ago