java吧 关注:1,222,446贴子:12,675,660
  • 0回复贴,共1

求问这道题有会的么!!!!

只看楼主收藏回复

Lab 11: November 27 or 28 (wish list)Attached Files:
wishListMaster.txt (278 B)
wishListMasterNew.txt (274 B)
11-wish-list.txt (6.449 KB)
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 arraydata[ 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 .7MusicGil Evans/Parabola (Vinyl)200.00Home3 Piece Sofa Set2499.99HomeDining Table with 4 Chairs1499.99MusicVince Mendoza First Album (CD)50.00LPHelen Merrill Swingin' With S. Suzuki (Vinyl)250.00HomeCappuccino Machine179.99HomeSilk Bed Bag (King Size)119.99The 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 indata = 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 indexint 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 useSystem.out.printf( "%03d:%s\t%s\t%s\t%s\n", ... );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.txtYou can add, remove, or viewWhich action should I perform? 1. Add2. Remove3. View the items4. QuitEnter you choice: 3total=7000:MusicGil Evans/Parabola (Vinyl)200.00001:Home3 Piece Sofa Set2499.99002:HomeDining Table with 4 Chairs1499.99003:MusicVince Mendoza First Album (CD)50.00004:LPHelen Merrill Swingin' With S. Suzuki (Vinyl)250.00005:HomeCappuccino Machine179.99006:HomeSilk Bed Bag (King Size)119.99You can add, remove, or viewWhich action should I perform? 1. Add2. Remove3. View the items4. QuitEnter you choice: 1Enter type, name, price with \t in between> HomeHome Theater Set1999.99You can add, remove, or viewWhich action should I perform? 1. Add2. Remove3. View the items4. QuitEnter you choice: 2Enter index 0You can add, remove, or viewWhich action should I perform? 1. Add2. Remove3. View the items4. QuitEnter you choice: 3total=7000:Home3 Piece Sofa Set2499.99001:HomeDining Table with 4 Chairs1499.99002:MusicVince Mendoza First Album (CD)50.00003:LPHelen Merrill Swingin' With S. Suzuki (Vinyl)250.00004:HomeCappuccino Machine179.99005:HomeSilk Bed Bag (King Size)119.99006:HomeHome Theater Set1999.99You can add, remove, or viewWhich action should I perform? 1. Add2. Remove3. View the items4. QuitEnter you choice: 4...Writing to file: wishListMasterNew.txtTo 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 ] ); } }


1楼2017-11-28 23:11回复