Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help! Java. I do not know what to do with the CookieProgram.java. I finished MasterOrder.java and CookieOrder.java. Download CookieProgram.java and Orders.txt.The text file contains

Please help! Java. I do not know what to do with the CookieProgram.java. I finished MasterOrder.java and CookieOrder.java.

Download CookieProgram.java and Orders.txt.The text file contains the information for the cookie orders to be stored in the master order. Create a MasterOrder object and then read in the data from the text file to fill it with CookieOrder objects. The Scanner object fileInput has already been created for you to use. Your program should then output the initial master order and the total number of boxes it contains using the getTotalBoxes method. Then ask the user which variety they would like to remove from the order and use the removeVariety method to update the master order. Finally, display the updated master order.

Here is the MasterOrder.java

import java.util.*; public class MasterOrder{ private ArrayList orders; public MasterOrder(){ orders = new ArrayList(); } public ArrayList getOrders(){ return orders; } public void addOrder(CookieOrder c){ orders.add(c); } //TO BE COMPLETED: getTotalBoxes //Computes and returns the sum of the number of //boxes of all cookie orders. //If there are no cookie orders in the master order, //the method returns 0. public int getTotalBoxes(){ int sum=0; for( CookieOrder c: orders){ sum = sum + c.getnumBoxes(); } return sum; } //TO BE COMPLETED:removeVariety //Updates the master order by removing all of //the cookie orders in which the variety of cookie //matches the parameter cookieVar. //The master order may contain zero or more //cookie orders with the same variety as cookieVar. //The method returns the total number of boxes removed //from the master order. public int removeVariety(String cookieVar){ int boxesRemoved=0; for(int i = orders.size()-1; i>=0; i--){ if(cookieVar.equals(orders.get(i).getVariety())){ boxesRemoved = boxesRemoved + orders.remove(i).getnumBoxes(); } } return boxesRemoved;

}

}

Here is the CookieOrder.java

public class CookieOrder{ private String variety; private int numBoxes; public CookieOrder(String v, int n){ variety=v; numBoxes=n; } public String getVariety(){ return variety; } public int getnumBoxes(){ return numBoxes; } public String toString(){ return numBoxes+"of"+variety; } }

Here is the CookieProgram.java

import java.util.*; import java.io.*; public class CookieProgram{ public static void main(String[] args) throws IOException{ Scanner fileInput = new Scanner(new FileReader("orders.txt")); ArrayListck= new ArrayList();

}

}

Here is the order.txt

4 Tagalongs 2 Tagalongs 2 Samoas 3 Trefoils 3 Do-si-dos 2 Caramel Chocolate Chip 5 Thin Mints 2 Thin Mints 7 Samoas 2 Trefoils 2 Do-si-dos 3 Thin Mints 3 Tagalongs 5 Do-si-dos 4 Trefoils 2 Thin Mints

Example output;

Initial Master Order: [4 boxes of Tagalongs, 2 boxes of Tagalongs, 2 boxes of Samoas, 3 boxes of Trefoils, 3 boxes of Do-si-dos, 2 boxes of Caramel Chocolate Chip, 5 boxes of Thin Mints, 2 boxes of Thin Mints, 7 boxes of Samoas, 2 boxes of Trefoils, 2 boxes of Do-si-dos, 3 boxes of Thin Mints, 3 boxes of Tagalongs, 5 boxes of Do-si-dos, 4 boxes of Trefoils, 2 boxes of Thin Mints] Total Boxes in the Order: 51 What variety would you like to remove? Trefoils 9 boxes of Trefoils were removed Here is the updated Master Order: [4 boxes of Tagalongs, 2 boxes of Tagalongs, 2 boxes of Samoas, 3 boxes of Do-si-dos, 2 boxes of Caramel Chocolate Chip, 5 boxes of Thin Mints, 2 boxes of Thin Mints, 7 boxes of Samoas, 2 boxes of Do-si-dos, 3 boxes of Thin Mints, 3 boxes of Tagalongs, 5 boxes of Do-si-dos, 2 boxes of Thin Mints]

Can you please give me some tips about how to understand the assignment faster? Like when I look at the assignment, I know what code should I use to write the program.

Thank you!

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

Students also viewed these Databases questions