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
Binary file added merge_sort/java/CustomerRecord.class
Binary file not shown.
117 changes: 117 additions & 0 deletions merge_sort/java/CustomerRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
public class CustomerRecord {
private String customerID;
private String lastName;
private String firstName;
private String contractID;
private String comment;

public CustomerRecord() {
}

public CustomerRecord(String customerID, String lastName, String firstName, String contractID, String comment) {
this.customerID = padOrTruncate(customerID, 5, true);
this.lastName = padOrTruncate(lastName, 50, false);
this.firstName = padOrTruncate(firstName, 50, false);
this.contractID = padOrTruncate(contractID, 5, true);
this.comment = padOrTruncate(comment, 25, false);
}

private String padOrTruncate(String value, int length, boolean isNumeric) {
if (value == null) {
value = "";
}

if (isNumeric) {
while (value.length() < length) {
value = "0" + value;
}
} else {
while (value.length() < length) {
value = value + " ";
}
}

if (value.length() > length) {
value = value.substring(0, length);
}

return value;
}

public String getCustomerID() {
return customerID;
}

public void setCustomerID(String customerID) {
this.customerID = padOrTruncate(customerID, 5, true);
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = padOrTruncate(lastName, 50, false);
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = padOrTruncate(firstName, 50, false);
}

public String getContractID() {
return contractID;
}

public void setContractID(String contractID) {
this.contractID = padOrTruncate(contractID, 5, true);
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = padOrTruncate(comment, 25, false);
}

public String toFixedWidthString() {
String result = customerID + lastName + firstName + contractID + comment;
return result.replaceAll("\\s+$", "");
}

public static CustomerRecord fromFixedWidthString(String line) {
if (line == null || line.length() < 115) {
return null;
}

while (line.length() < 135) {
line = line + " ";
}

CustomerRecord record = new CustomerRecord();
record.customerID = line.substring(0, 5);
record.lastName = line.substring(5, 55);
record.firstName = line.substring(55, 105);
record.contractID = line.substring(105, 110);
record.comment = line.substring(110, 135);

return record;
}

@Override
public String toString() {
return toFixedWidthString();
}

public int getCustomerIDAsInt() {
return Integer.parseInt(customerID.trim());
}

public int getContractIDAsInt() {
return Integer.parseInt(contractID.trim());
}
}
Binary file added merge_sort/java/FileReader.class
Binary file not shown.
28 changes: 28 additions & 0 deletions merge_sort/java/FileReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class FileReader {

public static List<CustomerRecord> readCustomerRecords(String filename) {
List<CustomerRecord> records = new ArrayList<>();

try (BufferedReader reader = Files.newBufferedReader(Paths.get(filename))) {
String line;
while ((line = reader.readLine()) != null) {
CustomerRecord record = CustomerRecord.fromFixedWidthString(line);
if (record != null) {
records.add(record);
}
}
} catch (IOException e) {
System.err.println("Error reading file " + filename + ": " + e.getMessage());
return new ArrayList<>();
}

return records;
}
}
Binary file added merge_sort/java/FileWriter.class
Binary file not shown.
19 changes: 19 additions & 0 deletions merge_sort/java/FileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class FileWriter {

public static void writeCustomerRecords(List<CustomerRecord> records, String filename) {
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filename))) {
for (CustomerRecord record : records) {
writer.write(record.toFixedWidthString());
writer.newLine();
}
} catch (IOException e) {
System.err.println("Error writing file " + filename + ": " + e.getMessage());
}
}
}
Binary file added merge_sort/java/MergeSortProcessor.class
Binary file not shown.
84 changes: 84 additions & 0 deletions merge_sort/java/MergeSortProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MergeSortProcessor {

public static void main(String[] args) {
createTestData();
mergeAndDisplayFiles();
sortAndDisplayFile();
System.out.println("Done.");
}

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

List<CustomerRecord> testFile1Records = new ArrayList<>();
testFile1Records.add(new CustomerRecord("1", "last-1", "first-1", "5423", "comment-1"));
testFile1Records.add(new CustomerRecord("5", "last-5", "first-5", "12323", "comment-5"));
testFile1Records.add(new CustomerRecord("10", "last-10", "first-10", "653", "comment-10"));
testFile1Records.add(new CustomerRecord("50", "last-50", "first-50", "5050", "comment-50"));
testFile1Records.add(new CustomerRecord("25", "last-25", "first-25", "7725", "comment-25"));
testFile1Records.add(new CustomerRecord("75", "last-75", "first-75", "1175", "comment-75"));

List<CustomerRecord> testFile2Records = new ArrayList<>();
testFile2Records.add(new CustomerRecord("999", "last-999", "first-999", "1610", "comment-99"));
testFile2Records.add(new CustomerRecord("3", "last-03", "first-03", "3331", "comment-03"));
testFile2Records.add(new CustomerRecord("30", "last-30", "first-30", "8765", "comment-30"));
testFile2Records.add(new CustomerRecord("85", "last-85", "first-85", "4567", "comment-85"));
testFile2Records.add(new CustomerRecord("24", "last-24", "first-24", "247", "comment-24"));

FileWriter.writeCustomerRecords(testFile1Records, "test-file-1.txt");
FileWriter.writeCustomerRecords(testFile2Records, "test-file-2.txt");
}

private static void mergeAndDisplayFiles() {
System.out.println("Merging and sorting files...");

List<CustomerRecord> file1Records = FileReader.readCustomerRecords("test-file-1.txt");
List<CustomerRecord> file2Records = FileReader.readCustomerRecords("test-file-2.txt");

if (file1Records.isEmpty()) {
System.err.println("Error opening test-file-1.txt");
return;
}

if (file2Records.isEmpty()) {
System.err.println("Error opening test-file-2.txt");
return;
}

List<CustomerRecord> mergedRecords = new ArrayList<>();
mergedRecords.addAll(file1Records);
mergedRecords.addAll(file2Records);

Collections.sort(mergedRecords, Comparator.comparing(CustomerRecord::getCustomerIDAsInt));

FileWriter.writeCustomerRecords(mergedRecords, "merge-output.txt");

for (CustomerRecord record : mergedRecords) {
System.out.println(record.toFixedWidthString());
}
}

private static void sortAndDisplayFile() {
System.out.println("Sorting merged file on descending contract id....");

List<CustomerRecord> mergedRecords = FileReader.readCustomerRecords("merge-output.txt");

if (mergedRecords.isEmpty()) {
System.err.println("Error opening merge-output.txt");
return;
}

Collections.sort(mergedRecords, Comparator.comparing(CustomerRecord::getContractIDAsInt).reversed());

FileWriter.writeCustomerRecords(mergedRecords, "sorted-contract-id.txt");

for (CustomerRecord record : mergedRecords) {
System.out.println(record.toFixedWidthString());
}
}
}
11 changes: 11 additions & 0 deletions merge_sort/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
11 changes: 11 additions & 0 deletions merge_sort/java/sorted-contract-id.txt
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/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/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