diff --git a/merge_sort/CustomerRecord.java b/merge_sort/CustomerRecord.java new file mode 100644 index 0000000..6a89bb6 --- /dev/null +++ b/merge_sort/CustomerRecord.java @@ -0,0 +1,87 @@ +public class CustomerRecord { + private int customerId; + private String lastName; + private String firstName; + private int contractId; + private String comment; + + 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 String toFixedWidthString() { + return String.format("%05d%-50s%-50s%05d%-25s", + customerId, + lastName, + firstName, + contractId, + comment); + } + + public String toDisplayString() { + return String.format("%05d%-50s%-50s%05d%-25s", + customerId, + lastName, + firstName, + contractId, + comment); + } + + public static CustomerRecord fromFixedWidthString(String line) { + if (line.length() < 135) { + return null; + } + + int customerId = Integer.parseInt(line.substring(0, 5)); + String lastName = line.substring(5, 55).trim(); + String firstName = line.substring(55, 105).trim(); + int contractId = Integer.parseInt(line.substring(105, 110)); + String comment = line.substring(110, 135).trim(); + + return new CustomerRecord(customerId, lastName, firstName, contractId, comment); + } +} diff --git a/merge_sort/MergeSortExample.java b/merge_sort/MergeSortExample.java new file mode 100644 index 0000000..afd0137 --- /dev/null +++ b/merge_sort/MergeSortExample.java @@ -0,0 +1,102 @@ +import java.io.*; +import java.util.*; + +public class MergeSortExample { + + public static void main(String[] args) { + MergeSortExample example = new MergeSortExample(); + + example.createTestData(); + example.mergeAndDisplayFiles(); + example.sortAndDisplayFile(); + + System.out.println("Done."); + } + + public void createTestData() { + System.out.println("Creating test data files..."); + + List file1Records = Arrays.asList( + 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") + ); + + List file2Records = Arrays.asList( + 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") + ); + + writeRecordsToFile("test-file-1.txt", file1Records); + writeRecordsToFile("test-file-2.txt", file2Records); + } + + public void mergeAndDisplayFiles() { + System.out.println("Merging and sorting files..."); + + List file1Records = readRecordsFromFile("test-file-1.txt"); + List file2Records = readRecordsFromFile("test-file-2.txt"); + + List allRecords = new ArrayList<>(); + allRecords.addAll(file1Records); + allRecords.addAll(file2Records); + + allRecords.sort(Comparator.comparingInt(CustomerRecord::getCustomerId)); + + for (CustomerRecord record : allRecords) { + System.out.println(record.toDisplayString()); + } + + writeRecordsToFile("merge-output.txt", allRecords); + } + + public void sortAndDisplayFile() { + System.out.println("Sorting merged file on descending contract id...."); + + List records = readRecordsFromFile("merge-output.txt"); + + records.sort(Comparator.comparingInt(CustomerRecord::getContractId).reversed()); + + for (CustomerRecord record : records) { + System.out.println(record.toDisplayString()); + } + + writeRecordsToFile("sorted-contract-id.txt", records); + } + + private void writeRecordsToFile(String filename, List records) { + try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) { + for (CustomerRecord record : records) { + writer.println(record.toFixedWidthString()); + } + } catch (IOException e) { + System.err.println("Error writing to file " + filename + ": " + e.getMessage()); + } + } + + private List readRecordsFromFile(String filename) { + List records = new ArrayList<>(); + + try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = reader.readLine()) != null) { + if (!line.trim().isEmpty()) { + CustomerRecord record = CustomerRecord.fromFixedWidthString(line); + if (record != null) { + records.add(record); + } + } + } + } catch (IOException e) { + System.err.println("Error reading from file " + filename + ": " + e.getMessage()); + } + + return records; + } +}