Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

In NetBeans, open the DrinkMenuFile project. Take a few minutes to study the existing code in the Beverage.java and the DrinkMenuFile.java classes. The Beverage class

In NetBeans, open the DrinkMenuFile project. Take a few minutes to study the existing code in the Beverage.java and the DrinkMenuFile.java classes. The Beverage class is complete and ready to use for creating and working with Beverage objects (if you wish to use it). The DrinkMenuFile.java file contains the application's driver class with the main() method. There are also 2 incomplete methods in this class, getMenuData() and writeMenuText() for which you will need to complete the code, as directed below.
The menu.data.txt file in the project's top-level directory contains the text data that the program needs to read. Do not modify the contents or format of this file. The text input data in the file looks like this:
coffee 81.95
coffee 122.25
coffee 162.60
coffee 202.95
tea 81.75
tea 122.00
tea 162.25
tea 202.50
water 8.99
Be sure to document the added source code with comments and format it appropriately.
1. Complete the code for the getMenuData() method so that it opens the file passed in via the fileName parameter. This method should read and parse the data in the text file using a Scanner. The code must include try and catch blocks to handle exceptions that may be thrown while opening or reading data from the file. The code for the getMenuData() method already includes a StringBuilder to assemble the multiline string with formatted data like this:
coffee 8 oz. $1.95
coffee 12 oz. $2.25
coffee 16 oz. $2.60
coffee 20 oz. $2.95
tea 8 oz. $1.75
tea 12 oz. $2.00
tea 16 oz. $2.25
tea 20 oz. $2.50
water 8 oz. $0.99
The method returns the contents of the StringBuilder as a string. Be sure to close the Scanner that reads from the file.
2. Complete the code for the writeMenuText() so that it uses a PrintWriter to write to the File object passed in as the first parameter. The multiline String value to write to the file is passed in at the second parameter. If the file does not already exist, the code for the writeMenuText() method needs to create it. Since an exception could be thrown when creating the file, include appropriate try and catch blocks. The catch block should print out a readable message for the user in the console that indicates the problem (rather than displaying a stack trace). Close the PrintWriter before returning from the method.
The contents of the output file after running the program should look like this:
Beverage Size Price
_________________________
coffee 8 oz. $1.95
coffee 12 oz. $2.25
coffee 16 oz. $2.60
coffee 20 oz. $2.95
tea 8 oz. $1.75
tea 12 oz. $2.00
tea 16 oz. $2.25
tea 20 oz. $2.50
water 8 oz. $0.99
3. After the code for the getMenuData() and writeMenuText() is complete, write the code for the main() method so that it produces output like this in the NetBeans console by calling the getMenuData() and writeMenuText() methods.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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