Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Match {
  5.  
  6.  
  7. public ArrayList<Account> readOldMaster() throws FileNotFoundException, IOException, ClassNotFoundException {
  8. ArrayList<Account> masterAccount = new ArrayList<Account>();
  9. try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("mainmasterfile.ser"))) {
  10. while (true) {
  11. masterAccount.add((Account) (input.readObject()));
  12. }
  13. } catch (EOFException e) {
  14.  
  15. }
  16. return masterAccount;
  17. }
  18.  
  19. public ArrayList<TransactionRecord> readTransaction() throws FileNotFoundException, IOException, ClassNotFoundException {
  20. ArrayList<TransactionRecord> masterRecord = new ArrayList<TransactionRecord>();
  21. try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("transactions.ser"))) {
  22. while (true) {
  23. masterRecord.add((TransactionRecord) (input.readObject()));
  24. }
  25.  
  26. } catch (EOFException e) {
  27.  
  28. }
  29. return masterRecord;
  30. }
  31.  
  32. public ArrayList<Account> readNewMaster() throws FileNotFoundException, IOException, ClassNotFoundException {
  33. ArrayList<Account> newMasterAccount = new ArrayList<Account>();
  34. try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("newmainmaster.ser"))) {
  35. while (true) {
  36. newMasterAccount.add((Account) (input.readObject()));
  37. }
  38. } catch (EOFException e) {
  39.  
  40. }
  41. return newMasterAccount;
  42. }
  43.  
  44. public void matchFiles() throws FileNotFoundException, IOException, ClassNotFoundException {
  45. ObjectOutputStream outNewMainMaster = new ObjectOutputStream(new FileOutputStream("newmainmaster.ser"));
  46. ArrayList<Account> oldMaster = this.readOldMaster();
  47. ArrayList<TransactionRecord> Records = this.readTransaction();
  48.  
  49. for (TransactionRecord Trans : Records) {
  50. boolean seenAccount = false;
  51.  
  52. for (Account Item : oldMaster) {
  53. if (Item.getAccount() == Trans.getAccount()) {
  54. double newbalance;
  55. newbalance = Trans.getAmount() + Item.getBalance();
  56. Item.setBalance(newbalance);
  57. seenAccount = true;
  58. }
  59. }
  60.  
  61. if (!seenAccount) {
  62. FileWriter File = new FileWriter("log.txt");
  63. PrintWriter Print = new PrintWriter(File);
  64. Print.printf("unmatched transaction record for account number..." + Trans.getAccount());
  65. Print.close();
  66. }
  67.  
  68. }
  69.  
  70. for (Account Item : oldMaster) {
  71. outNewMainMaster.writeObject(Item);
  72. }
  73.  
  74. if (outNewMainMaster != null)
  75. outNewMainMaster.close();
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement