Answer:
Invoice.java
import java.util.*;
public class Invoice {
static final String endPrices = "END_PRICES";
static final String endInvoice = "END_INVOICE";
static final String quit = "QUIT";
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
<em> //HashMap to store fruit name as key and price as value
</em>
Map<String, Float> fruits = new HashMap<>();
<em> //Loop until input is not "END_PRICES"
</em>
while (true){
String input = sc.next();
<em> //Come out of loop if input is "END_PRICES"
</em>
if(input.equals(endPrices))
break;
float price = Float.parseFloat(sc.next());
<em> //add fruit to hash map
</em>
fruits.put(input, price);
}
<em> //ArrayList to store total cost of each invoice
</em>
List<Float> totalList = new ArrayList<>();
Float total = 0f;
<em> //loop until input becomes "QUIT"
</em>
while (true){
String input = sc.next();
<em> //Break out of the loop if input is "QUIT"
</em>
if(input.equals(quit)){
break;
}
<em> //Add total price of the invoice to array list and set total to "0f" to store total of next invoice
</em>
if(input.equals(endInvoice)){
totalList.add(total);
total = 0f;
continue;
}
int quantity = sc.nextInt();
total += (fruits.get(input)*quantity);
}
<em> //Iterate through all objects in the total Array List and print them
</em>
Iterator itr = totalList.iterator();
while (itr.hasNext()){
System.out.printf("Total: %.2f \n", itr.next());
}
}
}