Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN JAVA The goal of this lab is to write a program WishList to maintaining a list of some wish list. The program reads from
IN JAVA The goal of this lab is to write a program WishList to maintaining a list of some wish list. The program reads from a data file that the user specifies as args[ 0 ] and writes to a file that the user specifies as args[ 1 ]. Reading occurs at the beginning of the program execution and writing occurs at the end, before termination. Each item of the list has three pieces of information: the type, the name, and the price. We will treat all of these, including the price as a String data. We naturally use an array element of length 3 to represent an item. Thus, given a list of n items, we represent them as a two-dimensional jagged String array of length n, whose rows happen to be length-3 arrays. Thus, our instantiation is like: String[][] data = new String[ n ][]; To store an item at slot i of this array is by assigning a one-dimensional array of Strings to a slot i: String[] item = ... // somehow this is instantiated as // a three-element array data[ i ] = item; In addition to the reading and writing, the program uses a while-loop to receive input from the user about an action to perform. The actions are: adding an item, removing an item, and viewing the list. The first line of the data file is the number of items. After that the items appear one line per item. Here is a sample file called wishListMaster.txt . 7 Music Gil Evans/Parabola (Vinyl) 200.00 Home 3 Piece Sofa Set 2499.99 Home Dining Table with 4 Chairs 1499.99 Music Vince Mendoza First Album (CD) 50.00 LP Helen Merrill Swingin' With S. Suzuki (Vinyl) 250.00 Home Cappuccino Machine 179.99 Home Silk Bed Bag (King Size) 119.99 The three components are separated by a tab-stop. When writing to the file, the same format should be maintained. In addition to main, the program should consist of five methods. public static String[][] read( File f ) throws FileNotFoundException public static String[][] add( String[][] data, String[] item ) public static String[][] remove( String[][] data, int index ) public static void view( String[][] data ) public static void write( File f, String[][] data ) There methods are for reading, viewing, adding, removing, and writing. The first three methods return an array that represents the data. The second parameter of the method add is the item to add, which is an array of String. The third parameter of the method remove is the index at which the removal should occur. The File parameter appearing in the read and write is the file from which to read the data and the file to which to write the information. The String[][] data appearing in add, remove, view, and write is the present data. Here is how you can design your program: Step 1. The program uses args[ 0 ] to instantiate a File object and call read. It stores the returned array in a String[][] variable data. Step 2. The program executes a while-loop whose condition is ( selection != 4 ). Here selection is an int variable initialized with the value of 1. In the while-loop, the program does the following: (a) The program prints an instruction. It receives the input using nextLine of a Scanner, converts it to an integer using Integer.parseInt, and stores it in selection; that is, selection = Integer.parseInt( keyboard.nextLine() ); Here keyboard is the Scanner instantiate with System.in (b) If ( selection == 1 ), the program asks the user to enter the type, the name, and the price in a single line with a tab-stop as the separator. The program receives an input line from the user (using keyboard.nextLine()) and splits it into an array using split( "\t" ) and stores in a String array, as in: String[] item = keyboard.nextLine().split( "\t" ); It then calls the add method, as in data = add( data, item ); (c) Else if ( selection == 2 ), the program asks the user to enter the index of removal. The program receives an input line from the user and converts it to an integer using Integer.parseInt. It stores the value in a variable index int index = Integer.parseInt( keyboard.nextLine() ); and calls remove( data, index). It stores the return value in data. (d) Else if ( selection == 3 ), the program calls view( data ). The method prints the elements of the array along with the index value. If the index is i, then it can use System.out.printf( "%03d:%s\t%s\t%s\t%s ", ... ); where the %03d is for the index and the three occurrences of %s are for the three elements of data[ i ]. Step 3. Use args[ 1 ] to instantiate a File object and call write with the file and the data as parameters. The code similar to view can be used for this purpose, but we do not include the index value in the output. Here is a sample execution: % java WishList wishListMaster.txt wishListMasterNew.txt ...Reading from file: wishListMaster.txt You can add, remove, or view Which action should I perform? 1. Add 2. Remove 3. View the items 4. Quit Enter you choice: 3 total=7 000:Music Gil Evans/Parabola (Vinyl) 200.00 001:Home 3 Piece Sofa Set 2499.99 002:Home Dining Table with 4 Chairs 1499.99 003:Music Vince Mendoza First Album (CD) 50.00 004:LP Helen Merrill Swingin' With S. Suzuki (Vinyl) 250.00 005:Home Cappuccino Machine 179.99 006:Home Silk Bed Bag (King Size) 119.99 You can add, remove, or view Which action should I perform? 1. Add 2. Remove 3. View the items 4. Quit Enter you choice: 1 Enter type, name, price with \t in between > Home Home Theater Set 1999.99 You can add, remove, or view Which action should I perform? 1. Add 2. Remove 3. View the items 4. Quit Enter you choice: 2 Enter index 0 You can add, remove, or view Which action should I perform? 1. Add 2. Remove 3. View the items 4. Quit Enter you choice: 3 total=7 000:Home 3 Piece Sofa Set 2499.99 001:Home Dining Table with 4 Chairs 1499.99 002:Music Vince Mendoza First Album (CD) 50.00 003:LP Helen Merrill Swingin' With S. Suzuki (Vinyl) 250.00 004:Home Cappuccino Machine 179.99 005:Home Silk Bed Bag (King Size) 119.99 006:Home Home Theater Set 1999.99 You can add, remove, or view Which action should I perform? 1. Add 2. Remove 3. View the items 4. Quit Enter you choice: 4 ...Writing to file: wishListMasterNew.txt To save your time, I am sharing with you a code for view below. You can modify this to write the code for the method write (make sure that you use the heaader provided in the above). public static void view( String[][] data ) { System.out.println( "total=" + data.length ); for ( int i = 0; i < data.length; i ++ ) { System.out.printf( "%03d:%s\t%s\t%s%n", i, data[ i ][ 0 ], data[ i ][ 1 ], data[ i ][ 2 ] ); }
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