Answered step by step
Verified Expert Solution
Question
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 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 toplevel 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
coffee
coffee
coffee
tea
tea
tea
tea
water
Be sure to document the added source code with comments and format it appropriately.
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 oz $
coffee oz $
coffee oz $
coffee oz $
tea oz $
tea oz $
tea oz $
tea oz $
water oz $
The method returns the contents of the StringBuilder as a string. Be sure to close the Scanner that reads from the file.
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 oz $
coffee oz $
coffee oz $
coffee oz $
tea oz $
tea oz $
tea oz $
tea oz $
water oz $
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started