Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package se.itello.example.payments;
- import java.io.InputStreamReader;
- import java.math.BigDecimal;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- public class FileProcessor{
- static BufferedReader reader;
- private static PaymentReceiver receiver;
- public FileProcessor() {
- // Assign receiver as an implementation of PaymentReceiver
- }
- /**
- * Function to determine what kind of file is passed to the API and
- * forwards it to the correct method.
- *
- * @param file File to be read and forwarded.
- */
- public static void readFile(File file) {
- try {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
- String fileType = file.getName().substring(file.getName().lastIndexOf('_')); // Assuming the same naming convention will hold in other files as well (filename_filekind.fileextension)
- switch(fileType) {
- case("_inbetalningstjansten.txt"):
- inbetalningsTjanst(file);
- break;
- case("_betalningsservice.txt"):
- betalningsService(file);
- break;
- default:
- System.out.println("File doesn't match any naming convention, check if it follows naming standard or if it needs implementation?");
- break;
- }
- } catch (Exception e){
- System.err.println("Error: " + e);
- }
- }
- /**
- * Reads file of type "_betalningsservice" and passes the appropriate
- * data to the payment receiver.
- * @param file File of the type "_betalningsservice" to be read
- */
- private static void betalningsService(File file) {
- // Ensures that the file follows expected standard
- if(!BetalServiceHelpers.betalServiceFileCheck(file)) {
- System.err.println("File doesn't match pattern");
- return;
- }
- try {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
- String line = reader.readLine();
- String accountNumber, currency;
- java.util.Date date;
- int numberOfPayments;
- accountNumber = line.substring(1, 16).trim().replace(" ",""); // Clearingnummer och kontonummer utan mellanslag
- currency = line.substring(48);
- DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
- date = dateFormat.parse(line.substring(40, 48));
- receiver.startPaymentBundle(accountNumber, date, currency);
- numberOfPayments = Integer.parseInt(line.substring(30, 40).trim());
- while((line = reader.readLine()) != null && numberOfPayments > 0) {
- BigDecimal amount = new BigDecimal(line.substring(1, 15).trim().replace(',', '.')); // Replaces comma with a dot as the BigDecimal constructor assumes English standard
- String ref = line.substring(16).trim();
- receiver.payment(amount, ref);
- }
- receiver.endPaymentBundle();
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- /**
- * Reads file of type "_inbetalningstjanst" and passes the appropriate
- * data to the payment receiver.
- * @param file File of the type "_inbetalningstjanst" to be read
- */
- private static void inbetalningsTjanst(File file) {
- if(!inbetalningsTjanstHelpers.inbetalningsTjanstFileCheck(file)) {
- // Ensures that the file follows expected standard
- System.err.println("File doesn't match pattern");
- return;
- }
- try {
- reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"));
- String line = reader.readLine();
- String accountNumber;
- String currency = "SEK";
- java.util.Date date;
- int numberOfPayments;
- accountNumber = line.substring(10, 24).trim(); // Clearingnumber and accountnumber
- date = new Date();
- receiver.startPaymentBundle(accountNumber, date, currency);
- BigDecimal sum = new BigDecimal(0);
- numberOfPayments = 0;
- while((line = reader.readLine()) != null) {
- if(line.substring(0, 2).equals("99")) {
- if(Integer.parseInt(line.substring(30, 38).trim()) != numberOfPayments) {
- System.err.println("Something's wrong with the number of payments");
- }
- if(!(new BigDecimal(line.substring(2, 22))).divide(new BigDecimal(100)).equals(sum)) {
- System.err.println("Something's wrong with the sum of the payments");
- }
- break;
- }
- String amountString = line.substring(2, 22).trim();
- BigDecimal amount = new BigDecimal(amountString).divide(new BigDecimal(100)); // Sets "amount" as a BigDecimal with 2 decimals
- String ref = line.substring(40).trim();
- receiver.payment(amount, ref);
- sum = sum.add(amount);
- numberOfPayments += 1;
- }
- receiver.endPaymentBundle();
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement