Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Java program named List that contains the following: o a doubly linked list that contains data for an integer named ProductID and a

Create a Java program named List that contains the following: o a doubly linked list that contains data for an integer named ProductID and a string named ProdType o allow the list to be filled by reading data from a file (productdata.txt or prompt for filename) that contains records consisting of the two fields o create a 20 entry queue that can contains data for an integer named Transid and a character named Transtype o load the queue from a file (transactions.txt or prompt for filename) five records at a time or until end of file (i.e. throttle the input to process); o After all of the transactions are processed, print the entries from the linked list. o process the queue after each partial load: ? first print the entire queue (choose some symbol to print for empty members), ? then, for each transid, find the matching productid in the doubly linked list (transids will be in random order) ? print the entry for transtype 'P' ? delete the entry for transtype 'D' ? clear (null ) the transid after processing an entry o after all of the transactions are processed, print the entire linked list.

Note: ? Most of the code has been written for you. However, you have to fill in the code where there are question marks (????) in List.java (the main class), ListVars.java (which contains global variables), and Record.java (defines the linked-list members). In addition, you must create code to process the transaction type where noted in the List.java module. Extra print statements have been added just for display purposes. If they become confusing, comment them out.

===============================================================================================================================================

A) List.java (the main class) { you have to fill in the code where there are question marks (????) }

import java.io.*;

import java.util.*;

public class Lists { static final int QUESIZE = 20; // size of the processing queue static final int THROTTLE = 5; // number of transactions to load into the queue at a time static Transaction initQue = new Transaction(); // used to initialize the Queue. public static void main(String[] args) throws IOException { ListVars v = new ListVars(); // a list of variables used throughout the program Transaction[] transQue = new Transaction[QUESIZE]; // array used for transaction queue /* Arrays.fill(transQue, initQue); /* since the contents of the queue are objects, each member points to an instantiation of the Transaction class, so an instance must be associated with each queue Member. This can be done with a loop, or using the fill static method of the Arrays class. */ ????? // Use a loop to initialize the transaction queue { ????? } String iRec; // used to hold the input record from the productdata file Scanner iRecScan; // scanner to scan for data in iRec File iData = new File("productdata.txt"); Scanner iDataScan = new Scanner(iData); // scanner to scan for data from iData File iTrans = new File("transactions.txt"); Scanner iTranScan = new Scanner(iTrans); // scanner to scan for data in iTrans if (iDataScan.hasNext()) // While there are records to read from iData { // read the 1st record, thus starting the doubly linked list iRecScan = new Scanner(iDataScan.nextLine()); // and set up parsing of the record using a Scanner v.????? = new Record(); // create the first list member (the next and prev pointers remain null for 1st record v.????? .productID = iRecScan.nextInt(); v.????? .prodType = iRecScan.next(); v.dllBegin = v.????? ; // the beginning of the list and v.dllEnd = v.????? ; // the end of the list point to the 1st member v.dllCount++; while (iDataScan.hasNext()) // While there are more records to load { iRec = iDataScan.nextLine(); // save the record in the work String iRec iRecScan = new Scanner(iRec); // set up parsing of the record v.current = new ?????; // create a new member to add to the list v.dllEnd.next = v.?????; // point the previous end of the list to this new member v.current.prev = v.?????; // point the new member to the previous end of the list v.current.productID = iRecScan.nextInt(); v.current.prodType = iRecScan.next(); v.dllCount++; v.dllEnd = v.?????; // save this member's address as the new end of the list // (recall that the identifier of an object simply holds the address of the object) } } while (iTranScan.hasNext()) // While there are more transactions to load { loadQue(iTranScan, transQue, v); // load the trans queue with "THROTTLE" trans at a time procQue(transQue, v); // process any unprocessed trans in the trans queue } // PRINT Doubly Linked List v.current = ?????; // point to the beginning of the list while (v.current != null) // if you're pointing to a member, print it { System.out.println("ID = " + v.current.productID + "; Prod Type = " + v.current.prodType + "."); v.current = ??????; // point to the next list member; a null will indicate you're at the end of the list } System.out.println("Total members = " + v.dllCount); iDataScan.close(); iTranScan.close(); } // E n d M A I N static void loadQue(Scanner iTranScan, ????? transQue, ListVars v) throws IOException { int transCt; // trans count processed in the current run of this method transCt = 0; do { transQue[v.?????] = new Transaction(); // the queue is an array of Transaction objects, so must instantiate a new object transQue[v.?????].transID = iTranScan.nextInt(); transQue[v.?????].transType = iTranScan.next(); transCt++; System.out.println("Trans ID "+ transQue[v.queAddNx].transID + " Trans Type: "+transQue[v.queAddNx].transType + "."); v.????? = (v.queAddNx < QUESIZE - 1) ? ++v.queAddNx : 0; /* point to next entry to add into; If already pointing to the last array element (QUESIZE - 1), then point back to index 0. */ System.out.println("v.queAddNx = " + v.queAddNx); } while (iTranScan.hasNext() && (transCt < THROTTLE )); // exit the loop if you hit end of file or the throttle limit v.????? += transCt; // accumulate all trans loaded in all runs of this method } // E n d l o a d Q u e static void procQue(Transaction[] transQue, ListVars v) { int x; for (x=0; x < QUESIZE; x++) System.out.println("Trans ID "+ transQue[x].transID + " Trans Type: "+ transQue[x].transType + "."); System.out.println("Total Transaction Count = "+ v.transCt + "."); while (transQue[v.?????].transID != -1) // While there is another transaction to process { // // Process the transaction type HERE // // THEN do the following transQue[v.?????].transID = -1; // use -1 to mark the queue member as processed v.queProcNx = (v.queProcNx < QUESIZE - 1) ? ++v.queProcNx : 0; /* If the pointer to the next entry to process increments to the last element (QUESIZE - 1), then back to 0. */

} } // E n d p r o c Q u e

} // E n d C L A S S

===========================================================================================================================================

B) ListVars.java (which contains global variables), { you have to fill in the code where there are question marks (????) }

public class ListVars { ????? current = null; // Pointer to current record to process in Linked List ????? dllBegin = null; // Pointter to begining record of Linked List ????? dllEnd = null; // Pointter to last record of Linked List int dllCount = 0; // Number of records in the Linked List int transCt = 0; // Number of transactions in queue to be processed int queAddNx = 0; // Next index position in queue to insert into next int queProcNx = 0; // Next index position in queue to process

}

=========================================================================================================================================

c) Record.java (defines the linked-list members). { you have to fill in the code where there are question marks (????) }

public class Record { ???? next = null; //Each record points to the NEXT record ???? prev = null; //Each record points to the Previous record int productID = 0; //Each record has an integer id String prodType= null; }

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

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Why is investment spending unstable? LO10.4

Answered: 1 week ago