Listclasspathlistprojectlist Orgeclipsejdtco ✓ Solved
List/.classpath List/.project List org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature List/.settings/org.eclipse.jdt.core.prefs eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.source=1.8 List/bin/Lists.class public synchronized class Lists { static final int QUESIZE = 20; static final int THROTTLE = 5; static Transaction initQue ; public void Lists(); public static void main (String[]) throws java.io.IOException; static void loadQue (java.util.Scanner); static void procQue (Transaction[], ListVars); } List/bin/ListVars.class public synchronized class ListVars { int dllCount ; int transCt ; int queAddNx ; int queProcNx ; public void ListVars(); } List/bin/productdata.txt 02 VGA_Monitor_square 05 LED_Monitor_square 06 LED_Monitor_wide 10 Flat_Screen_TV_portable 14 Flat_Screen_TV_table 16 Flat_Screen_TV_floor 17 Flat_Screen_TV_wall 20 Computer_Stand_M12 21 Computer_Stand_M22 23 Computer_Stand_M27 25 Computer_Stand_M47 List/bin/productdata2.txt 01 Chair 02 Ottoman 03 Recliner 04 Stool 05 Bench 06 Couch 07 Bed 08 Futon 09 Hammock 10 Mattress 11 Television 12 Radio 13 Desk 14 Table 15 Bookcase 16 Cabinet 17 Hutch 18 Chest 19 Coat_rack 20 Nightstand 21 Shelf 22 Safe 23 Lamp 24 Curtain 25 Blinds 26 Comforter 27 Pillow List/bin/Record.class public synchronized class Record { int productID ; String prodType ; public void Record(); } List/bin/Transaction.class public synchronized class Transaction { int transID ; String transType ; public void Transaction(); } List/bin/transactions.txt 06 P 10 P 14 D 16 P 17 D 20 P 22 P List/bin/transactions2.txt 02 P 05 P 19 D 06 D 21 D 07 P 08 D 23 P 10 P 11 P 12 D 09 P 13 D 03 D 14 D 15 D 17 D 25 P 18 D 20 P 01 P 22 P 16 D List/productdata.txt 01 Chair 02 Ottoman 03 Recliner 04 Stool 05 Bench 06 Couch 07 Bed 08 Futon 09 Hammock 10 Mattress 11 Television 12 Radio 13 Desk 14 Table 15 Bookcase 16 Cabinet 17 Hutch 18 Chest 19 Coat_rack 20 Nightstand 21 Shelf 22 Safe 23 Lamp 24 Curtain 25 Blinds 26 Comforter 27 Pillow List/src/Lists.java List/src/Lists.java 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 List/src/ListVars.java List/src/ListVars.java 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 } List/src/Record.java List/src/Record.java 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 ; } List/src/Transaction.java List/src/Transaction.java public class Transaction { int transID = - 1 ; // Transid matches a product id that the transaction is against. (-1 = not set) String transType = "." ; // 'D' to Delete; 'P' to print; '.' = no operation to perform } List/transactions.txt 02 P 05 P 19 D 06 D 21 D 07 P 08 D 23 P 10 P 11 P 12 D 09 P 13 D 03 D 14 D 15 D 17 D 25 P 18 D 20 P 01 P 22 P 16 D CPSC-4355 List Assignment 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 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.
Paper for above instructions
The purpose of this assignment is to create a Java program that implements a doubly linked list and processes transactions based on provided product data and transaction types. The target output will be a program that utilizes both file I/O and object-oriented programming principles like classes and methods.
Overview
The program must consist of:
1. A doubly linked list to store product information, which includes an integer for `ProductID` and a string for `ProdType`.
2. A transaction queue that contains an integer for `TransID` and a string for `TransType`, loaded in increments (throttling) of five records at a time.
3. The program must read from specified text files and process transactions based on the loaded queue, either printing or deleting entries as specified.
Components
The program is composed of several classes, including:
- `Lists` (Main class)
- `ListVars` (to hold global variables)
- `Record` (the nodes for the doubly linked list)
- `Transaction` (to hold transaction records)
Implementation
Below are the code snippets to fill in the portions marked with `????` in your structure. The complete implementation begins by defining each class accurately.
Lists.java
```java
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(); // Initialize the Queue.
public static void main(String[] args) throws IOException {
ListVars v = new ListVars(); // Variable holder
Transaction[] transQue = new Transaction[QUESIZE]; // Transaction queue
Arrays.fill(transQue, new Transaction()); // Initialize Queue
// Load product data
loadProductData(v);
// Load and process transactions
while (true) {
if (!loadQueue(transQue, v)) {
break; // Exit on end of file
}
procQue(transQue, v); // Process transactions.
}
printDoublyLinkedList(v);
}
static void loadProductData(ListVars v) throws IOException {
File iData = new File("productdata.txt");
Scanner iDataScan = new Scanner(iData);
if (iDataScan.hasNext()) {
String iRec = iDataScan.nextLine();
Scanner iRecScan = new Scanner(iRec);
v.dllBegin = new Record(); // Create the first record
v.dllBegin.productID = iRecScan.nextInt();
v.dllBegin.prodType = iRecScan.next();
v.dllCount++;
v.dllEnd = v.dllBegin; // Set start and end to the first record
while (iDataScan.hasNext()) {
iRec = iDataScan.nextLine();
iRecScan = new Scanner(iRec);
Record newRecord = new Record();
newRecord.productID = iRecScan.nextInt();
newRecord.prodType = iRecScan.next();
v.dllEnd.next = newRecord; // Link new record
newRecord.prev = v.dllEnd; // Link back to the previous record
v.dllEnd = newRecord; // Update end
v.dllCount++;
}
}
iDataScan.close(); // Close scanner
}
static boolean loadQueue(Transaction[] transQue, ListVars v) throws IOException {
File iTrans = new File("transactions.txt");
Scanner iTranScan = new Scanner(iTrans);
int transCt = 0;
while (iTranScan.hasNext() && transCt < THROTTLE) {
Transaction trans = new Transaction();
trans.transID = iTranScan.nextInt();
trans.transType = iTranScan.next();
transQue[v.queAddNx] = trans; // Add transaction to queue
transCt++;
System.out.println("Trans ID " + trans.transID + " Trans Type: " + trans.transType + ".");
v.queAddNx = (v.queAddNx < QUESIZE - 1) ? ++v.queAddNx : 0; // Circular update
}
return transCt > 0; // Return if there were transactions loaded
}
static void procQue(Transaction[] transQue, ListVars v) {
System.out.println("Processing Queue:");
for (int x = 0; x < QUESIZE; x++) {
if (transQue[x].transID != -1) {
System.out.println("Trans ID " + transQue[x].transID + " Trans Type: " + transQue[x].transType + ".");
if (transQue[x].transType.equals("P")) {
printRecord(v, transQue[x].transID); // Print if TransType is P
} else if (transQue[x].transType.equals("D")) {
deleteRecord(v, transQue[x].transID); // Delete if TransType is D
}
transQue[x].transID = -1; // Mark as processed
}
}
System.out.println("Total Transaction Count = " + v.transCt + ".");
}
static void printRecord(ListVars v, int transID) {
Record current = v.dllBegin;
while (current != null) {
if (current.productID == transID) {
System.out.println("ID = " + current.productID + "; Prod Type = " + current.prodType + ".");
return;
}
current = current.next; // move to the next record
}
}
static void deleteRecord(ListVars v, int transID) {
Record current = v.dllBegin;
while (current != null) {
if (current.productID == transID) {
// Delete record
if (current.prev != null) current.prev.next = current.next;
if (current.next != null) current.next.prev = current.prev;
if (current == v.dllBegin) v.dllBegin = current.next; // update head if necessary
if (current == v.dllEnd) v.dllEnd = current.prev; // update tail if necessary
v.dllCount--;
System.out.println("Deleted record with ID = " + transID);
return;
}
current = current.next; // move to the next record
}
}
static void printDoublyLinkedList(ListVars v) {
System.out.println("Final List:");
Record current = v.dllBegin;
while (current != null) {
System.out.println("ID = " + current.productID + "; Prod Type = " + current.prodType + ".");
current = current.next; // move to the next record
}
System.out.println("Total members = " + v.dllCount);
}
}
```
ListVars.java
```java
public class ListVars {
Record current = null; // Pointer to current record
Record dllBegin = null; // Pointer to start of the doubly linked list
Record dllEnd = null; // Pointer to end of the doubly linked list
int dllCount = 0; // Count of records in the list
int transCt = 0; // Count of transactions processed
int queAddNx = 0; // Next index position for queue to be added to
int queProcNx = 0; // Next index position in queue to process
}
```
Record.java
```java
public class Record {
Record next = null; // Next record in the list
Record prev = null; // Previous record in the list
int productID = 0; // Product ID
String prodType = null; // Product Type
}
```
Transaction.java
```java
public class Transaction {
int transID = -1; // Transaction ID (-1 if not set)
String transType = "."; // 'D' to delete, 'P' to print, '.' for no operation
}
```
Explanation of Code
The main class, `Lists`, handles the core functionalities. It includes transaction processing and product loading from files. The `ListVars` class keeps track of the state, such as current records and counts, while the `Record` class defines the nodes of the doubly linked list.
The primary operations include:
- Loading Products: Read product data from a file and create linked list nodes.
- Loading Transactions: Read transaction data and store it in a queue with throttling.
- Processing Transactions: Depending on the type, either print the product details or delete a product from the list.
- Printing the List: After all transactions have been processed, the program prints the remaining products in the doubly linked list.
References
1. Deitel, P. J., & Deitel, H. M. (2018). Java: How to Program (11th Edition). Pearson.
2. Eckel, B. (2006). Thinking in Java (4th Edition). Prentice Hall.
3. Horstmann, C. S., & Cornell, G. (2013). Core Java SE 8 Volume I - Fundamentals. Prentice Hall.
4. Liang, Y. D. (2018). Introduction to Java Programming, Comprehensive Version. Pearson.
5. Finkel, H. (2017). Data Structures and Algorithms in Java. Springer.
6. Sedgewick, R., & Wayne, K. (2011). Algorithms. Addison-Wesley.
7. Deitel, P. J., & Deitel, H. M. (2014). Java SE 8 for Programmers. Pearson.
8. Bloch, J. (2008). Effective Java. Addison-Wesley.
9. Oracle Corporation. (2021). Java Tutorials. Retrieved from https://docs.oracle.com/javase/tutorial/
10. Lafore, R. (2011). Data Structures and Algorithms in Java. Sams Publishing.
This solution, while detailed, is only one approach to the problem, and other implementations may yield similar results in accomplishing the programming task.