Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.73 KB | None | 0 0
  1. package se.itello.example.payments;
  2.  
  3. import java.io.InputStreamReader;
  4. import java.math.BigDecimal;
  5. import java.text.DateFormat;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11.  
  12.  
  13. public class FileProcessor{
  14.     static BufferedReader reader;
  15.     private static PaymentReceiver receiver;
  16.    
  17.     public FileProcessor() {
  18.         // Assign receiver as an implementation of PaymentReceiver
  19.     }
  20.    
  21.     /**
  22.      * Function to determine what kind of file is passed to the API and
  23.      * forwards it to the correct method.
  24.      *
  25.      * @param file File to be read and forwarded.
  26.      */
  27.     public static void readFile(File file) {
  28.         try {
  29.             reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
  30.             String fileType = file.getName().substring(file.getName().lastIndexOf('_'));                        // Assuming the same naming convention will hold in other files as well (filename_filekind.fileextension)
  31.             switch(fileType) {
  32.             case("_inbetalningstjansten.txt"):
  33.                 inbetalningsTjanst(file);
  34.                 break;
  35.             case("_betalningsservice.txt"):
  36.                 betalningsService(file);
  37.                 break;
  38.             default:
  39.                 System.out.println("File doesn't match any naming convention, check if it follows naming standard or if it needs implementation?");
  40.                 break;
  41.             }
  42.            
  43.         } catch (Exception e){
  44.             System.err.println("Error: " + e);
  45.         }
  46.     }
  47.    
  48.     /**
  49.      * Reads file of type "_betalningsservice" and passes the appropriate
  50.      * data to the payment receiver.
  51.      * @param file File of the type "_betalningsservice" to be read
  52.      */
  53.     private static void betalningsService(File file) {
  54.         // Ensures that the file follows expected standard
  55.         if(!BetalServiceHelpers.betalServiceFileCheck(file)) {
  56.             System.err.println("File doesn't match pattern");
  57.             return;
  58.         }
  59.         try {
  60.             reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
  61.            
  62.             String line = reader.readLine();
  63.             String accountNumber, currency;
  64.             java.util.Date date;
  65.             int numberOfPayments;
  66.             accountNumber = line.substring(1, 16).trim().replace(" ","");                                   // Clearingnummer och kontonummer utan mellanslag
  67.             currency = line.substring(48);
  68.            
  69.             DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  70.             date = dateFormat.parse(line.substring(40, 48));
  71.            
  72.             receiver.startPaymentBundle(accountNumber, date, currency);
  73.            
  74.             numberOfPayments = Integer.parseInt(line.substring(30, 40).trim());
  75.            
  76.             while((line = reader.readLine()) != null && numberOfPayments > 0) {
  77.                 BigDecimal amount = new BigDecimal(line.substring(1, 15).trim().replace(',', '.'));         // Replaces comma with a dot as the BigDecimal constructor assumes English standard
  78.                 String ref = line.substring(16).trim();
  79.            
  80.                 receiver.payment(amount, ref);
  81.             }
  82.            
  83.             receiver.endPaymentBundle();
  84.         } catch (Exception e) {
  85.             System.err.println(e);
  86.         }
  87.     }
  88.    
  89.     /**
  90.      * Reads file of type "_inbetalningstjanst" and passes the appropriate
  91.      * data to the payment receiver.
  92.      * @param file File of the type "_inbetalningstjanst" to be read
  93.      */
  94.     private static void inbetalningsTjanst(File file) {
  95.         if(!inbetalningsTjanstHelpers.inbetalningsTjanstFileCheck(file)) {
  96.             // Ensures that the file follows expected standard
  97.             System.err.println("File doesn't match pattern");
  98.             return;
  99.         }
  100.         try {
  101.             reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
  102.             String line = reader.readLine();
  103.             String accountNumber;
  104.             String currency = "SEK";
  105.             java.util.Date date;
  106.             int numberOfPayments;
  107.             accountNumber = line.substring(10, 24).trim();                                                  // Clearingnumber and accountnumber
  108.             date = new Date();
  109.            
  110.             receiver.startPaymentBundle(accountNumber, date, currency);
  111.        
  112.             BigDecimal sum = new BigDecimal(0);
  113.             numberOfPayments = 0;
  114.        
  115.             while((line = reader.readLine()) != null) {
  116.                 if(line.substring(0, 2).equals("99")) {
  117.                     if(Integer.parseInt(line.substring(30, 38).trim()) != numberOfPayments) {
  118.                         System.err.println("Something's wrong with the number of payments");
  119.                     }
  120.                     if(!(new BigDecimal(line.substring(2, 22))).divide(new BigDecimal(100)).equals(sum)) {
  121.                         System.err.println("Something's wrong with the sum of the payments");
  122.                     }
  123.                     break;
  124.                 }
  125.                
  126.                 String amountString = line.substring(2, 22).trim();
  127.                 BigDecimal amount = new BigDecimal(amountString).divide(new BigDecimal(100));               // Sets "amount" as a BigDecimal with 2 decimals
  128.                 String ref = line.substring(40).trim();
  129.                
  130.                 receiver.payment(amount, ref);
  131.            
  132.                 sum = sum.add(amount);
  133.            
  134.                 numberOfPayments += 1;
  135.             }
  136.        
  137.             receiver.endPaymentBundle();
  138.         } catch (Exception e) {
  139.             System.err.println(e);
  140.         }
  141.        
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement