Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions merge_sort/java-implementation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# COBOL Merge Sort Migration to Java

This Java implementation migrates the merge sort functionality from the COBOL program `merge_sort_test.cbl` to Java while maintaining the exact same file-based processing behavior.

## Implementation Details

### Data Model
- `CustomerRecord.java`: Represents the customer record structure with fields matching the COBOL implementation:
- Customer ID (5 digits)
- Last Name (50 characters)
- First Name (50 characters)
- Contract ID (5 digits)
- Comment (25 characters)

### File Processing
- Maintains the file-based approach from COBOL
- Input files: `test-file-1.txt` and `test-file-2.txt`
- Output files: `merge-output.txt` and `sorted-contract-id.txt`
- Uses `BufferedReader` and `BufferedWriter` for file operations

### Processing Flow
1. **Create Test Data**: Generates the same test data as the COBOL program
2. **Merge Files**: Combines two input files sorted by ascending customer ID
3. **Sort File**: Sorts the merged file by descending contract ID
4. **Display Results**: Shows the processed records for validation

## Running the Implementation

```bash
cd java-implementation/src/main/java
javac com/example/mergesort/*.java
java com.example.mergesort.MergeSortProcessor
```

## Test Data

### test-file-1.txt (East region)
- Customer ID 1: last-1, first-1, contract 5423, comment-1
- Customer ID 5: last-5, first-5, contract 12323, comment-5
- Customer ID 10: last-10, first-10, contract 653, comment-10
- Customer ID 50: last-50, first-50, contract 5050, comment-50
- Customer ID 25: last-25, first-25, contract 7725, comment-25
- Customer ID 75: last-75, first-75, contract 1175, comment-75

### test-file-2.txt (West region)
- Customer ID 999: last-999, first-999, contract 1610, comment-99
- Customer ID 3: last-03, first-03, contract 3331, comment-03
- Customer ID 30: last-30, first-30, contract 8765, comment-30
- Customer ID 85: last-85, first-85, contract 4567, comment-85
- Customer ID 24: last-24, first-24, contract 247, comment-24

## Expected Output

The program will display the merged records sorted by customer ID, then the same records sorted by contract ID in descending order, matching the behavior of the original COBOL program.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.example.mergesort;

public class CustomerRecord {
private int customerId;
private String lastName;
private String firstName;
private int contractId;
private String comment;

public CustomerRecord() {}

public CustomerRecord(int customerId, String lastName, String firstName, int contractId, String comment) {
this.customerId = customerId;
this.lastName = lastName;
this.firstName = firstName;
this.contractId = contractId;
this.comment = comment;
}

public int getCustomerId() {
return customerId;
}

public void setCustomerId(int customerId) {
this.customerId = customerId;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public int getContractId() {
return contractId;
}

public void setContractId(int contractId) {
this.contractId = contractId;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public static CustomerRecord fromString(String line) {
if (line == null || line.length() < 135) {
throw new IllegalArgumentException("Invalid record format");
}

int customerId = Integer.parseInt(line.substring(0, 5).trim());
String lastName = line.substring(5, 55).trim();
String firstName = line.substring(55, 105).trim();
int contractId = Integer.parseInt(line.substring(105, 110).trim());
String comment = line.substring(110, 135).trim();

return new CustomerRecord(customerId, lastName, firstName, contractId, comment);
}

@Override
public String toString() {
return String.format("%05d%-50s%-50s%05d%-25s",
customerId,
padRight(lastName, 50),
padRight(firstName, 50),
contractId,
padRight(comment, 25));
}

private String padRight(String str, int length) {
if (str == null) str = "";
if (str.length() >= length) {
return str.substring(0, length);
}
StringBuilder sb = new StringBuilder(str);
while (sb.length() < length) {
sb.append(' ');
}
return sb.toString();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.example.mergesort;

import java.io.*;
import java.util.*;

public class MergeSortProcessor {

public void createTestData() {
System.out.println("Creating test data files...");

createTestFile1();
createTestFile2();
}

private void createTestFile1() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("test-file-1.txt"))) {
CustomerRecord[] records = {
new CustomerRecord(1, "last-1", "first-1", 5423, "comment-1"),
new CustomerRecord(5, "last-5", "first-5", 12323, "comment-5"),
new CustomerRecord(10, "last-10", "first-10", 653, "comment-10"),
new CustomerRecord(50, "last-50", "first-50", 5050, "comment-50"),
new CustomerRecord(25, "last-25", "first-25", 7725, "comment-25"),
new CustomerRecord(75, "last-75", "first-75", 1175, "comment-75")
};

for (CustomerRecord record : records) {
writer.write(record.toString());
writer.newLine();
}
} catch (IOException e) {
System.err.println("Failed to create test-file-1.txt: " + e.getMessage());
System.exit(1);
}
}

private void createTestFile2() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("test-file-2.txt"))) {
CustomerRecord[] records = {
new CustomerRecord(999, "last-999", "first-999", 1610, "comment-99"),
new CustomerRecord(3, "last-03", "first-03", 3331, "comment-03"),
new CustomerRecord(30, "last-30", "first-30", 8765, "comment-30"),
new CustomerRecord(85, "last-85", "first-85", 4567, "comment-85"),
new CustomerRecord(24, "last-24", "first-24", 247, "comment-24")
};

for (CustomerRecord record : records) {
writer.write(record.toString());
writer.newLine();
}
} catch (IOException e) {
System.err.println("Failed to create test-file-2.txt: " + e.getMessage());
System.exit(1);
}
}

public void mergeFiles(String file1, String file2, String outputFile) {
System.out.println("Merging and sorting files...");

List<CustomerRecord> allRecords = new ArrayList<>();

try (BufferedReader reader1 = new BufferedReader(new FileReader(file1))) {
String line;
while ((line = reader1.readLine()) != null) {
if (!line.trim().isEmpty()) {
allRecords.add(CustomerRecord.fromString(line));
}
}
} catch (IOException e) {
System.err.println("Error reading " + file1 + ": " + e.getMessage());
System.exit(1);
}

try (BufferedReader reader2 = new BufferedReader(new FileReader(file2))) {
String line;
while ((line = reader2.readLine()) != null) {
if (!line.trim().isEmpty()) {
allRecords.add(CustomerRecord.fromString(line));
}
}
} catch (IOException e) {
System.err.println("Error reading " + file2 + ": " + e.getMessage());
System.exit(1);
}

allRecords.sort(Comparator.comparingInt(CustomerRecord::getCustomerId));

try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
for (CustomerRecord record : allRecords) {
writer.write(record.toString());
writer.newLine();
System.out.println(record.toString());
}
} catch (IOException e) {
System.err.println("Error writing to " + outputFile + ": " + e.getMessage());
System.exit(1);
}
}

public void sortFile(String inputFile, String outputFile) {
System.out.println("Sorting merged file on descending contract id....");

List<CustomerRecord> records = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty()) {
records.add(CustomerRecord.fromString(line));
}
}
} catch (IOException e) {
System.err.println("Error reading " + inputFile + ": " + e.getMessage());
System.exit(1);
}

records.sort(Comparator.comparingInt(CustomerRecord::getContractId).reversed());

try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
for (CustomerRecord record : records) {
writer.write(record.toString());
writer.newLine();
System.out.println(record.toString());
}
} catch (IOException e) {
System.err.println("Error writing to " + outputFile + ": " + e.getMessage());
System.exit(1);
}
}

public static void main(String[] args) {
MergeSortProcessor processor = new MergeSortProcessor();

processor.createTestData();

processor.mergeFiles("test-file-1.txt", "test-file-2.txt", "merge-output.txt");

processor.sortFile("merge-output.txt", "sorted-contract-id.txt");

System.out.println("Done.");
}
}
11 changes: 11 additions & 0 deletions merge_sort/java-implementation/src/main/java/merge-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
00001last-1 first-1 05423comment-1
00003last-03 first-03 03331comment-03
00005last-5 first-5 12323comment-5
00010last-10 first-10 00653comment-10
00024last-24 first-24 00247comment-24
00025last-25 first-25 07725comment-25
00030last-30 first-30 08765comment-30
00050last-50 first-50 05050comment-50
00075last-75 first-75 01175comment-75
00085last-85 first-85 04567comment-85
00999last-999 first-999 01610comment-99
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
00005last-5 first-5 12323comment-5
00030last-30 first-30 08765comment-30
00025last-25 first-25 07725comment-25
00001last-1 first-1 05423comment-1
00050last-50 first-50 05050comment-50
00085last-85 first-85 04567comment-85
00003last-03 first-03 03331comment-03
00999last-999 first-999 01610comment-99
00075last-75 first-75 01175comment-75
00010last-10 first-10 00653comment-10
00024last-24 first-24 00247comment-24
6 changes: 6 additions & 0 deletions merge_sort/java-implementation/src/main/java/test-file-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
00001last-1 first-1 05423comment-1
00005last-5 first-5 12323comment-5
00010last-10 first-10 00653comment-10
00050last-50 first-50 05050comment-50
00025last-25 first-25 07725comment-25
00075last-75 first-75 01175comment-75
5 changes: 5 additions & 0 deletions merge_sort/java-implementation/src/main/java/test-file-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
00999last-999 first-999 01610comment-99
00003last-03 first-03 03331comment-03
00030last-30 first-30 08765comment-30
00085last-85 first-85 04567comment-85
00024last-24 first-24 00247comment-24