Commit 01e32bf2 authored by Attila Bodis's avatar Attila Bodis

java test kandidat 18.2.2020

parent 5e787d3a
package eu.greyson.JavaTestRefactor;
import org.apache.commons.lang3.StringUtils;
import eu.greyson.JavaTestRefactor.model.Payment;
import eu.greyson.JavaTestRefactor.service.PaymentParser;
import eu.greyson.JavaTestRefactor.service.TrackerService;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
......@@ -8,9 +10,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
public class JavaTestRefactorApplication implements CommandLineRunner {
......@@ -19,17 +18,19 @@ public class JavaTestRefactorApplication implements CommandLineRunner {
public static final String INPUT = "Input > ";
public static final String SEPARATOR = "=================================================";
public static final String HEADER = "Payment Tracker application";
public static final String VALIDATION_EXIT = "Exiting application.";
public static final String APPLICATION_EXIT = "Exiting application.";
public static final String VALIDATION_INVALID_INPUT = "Invalid input: ";
public static final String VALIDATION_3_LETTERS = "Invalid input. Currency must be 3 letters !";
public static final String VALIDATION_AMOUNT_NUMBER = "Invalid input. Amount must consit of only numbers!";
Boolean isReading = true;
private final PaymentParser paymentParser;
private Map<String, BigDecimal> paymentMap = new HashMap<>();
private final TrackerService trackerService;
Validate validate = new Validate();
Boolean isReading = true;
public JavaTestRefactorApplication(PaymentParser paymentParser, TrackerService trackerService) {
this.paymentParser = paymentParser;
this.trackerService = trackerService;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(JavaTestRefactorApplication.class);
......@@ -40,28 +41,17 @@ public class JavaTestRefactorApplication implements CommandLineRunner {
@Override
public void run(String... args) {
trackerService();
}
private void trackerService() {
System.out.println(SEPARATOR + "\n" + HEADER + "\n" + SEPARATOR);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// fileName argument check, if true try to read file
while (isReading) {
String input = readInput(bufferedReader);
Payment payment = parsePayment(input);
if (payment != null) {
processPayment(payment);
try {
Payment parsedPayment = paymentParser.parse(readInput(bufferedReader));
trackerService.processPayment(parsedPayment);
} catch (Exception exception) {
System.out.println("\n\n=== Actual balances:"+ trackerService.getNetCurrencies());
}
System.out.println("\n\n=== Actual balances:"+getNetCurrencies());
}
System.out.println(SEPARATOR + "\n" + VALIDATION_EXIT);
System.out.println(SEPARATOR + "\n" + APPLICATION_EXIT);
}
public String readInput(BufferedReader bufferedReader) {
......@@ -84,41 +74,4 @@ public class JavaTestRefactorApplication implements CommandLineRunner {
return null;
}
}
public Payment parsePayment(String payment) {
if (validate.isValidInputFormat(payment)) {
String[] paymentSplit = payment.split(" ");
return new Payment(paymentSplit[0].toUpperCase(), paymentSplit[1]);
}
return null;
}
public void processPayment(Payment payment) {
String curenncy = payment.getCurrency();
BigDecimal amount = paymentMap.get(payment.getCurrency());
if (amount != null) {
paymentMap.put(curenncy, amount.add(payment.getAmount()));
System.out.print(" (Updated)");
} else {
paymentMap.put(curenncy, payment.getAmount());
System.out.print(" (Added new Currency:"+ curenncy);
}
}
public String getNetCurrencies() {
StringBuilder builder = new StringBuilder();
builder.append("\n");
paymentMap.forEach((k, v) -> {
if (v != BigDecimal.ZERO) {
builder.append("> "+k + " " + v + "\n");
}
});
if (StringUtils.isBlank(builder.toString())) {
return "";
}
return builder.toString();
}
}
\ No newline at end of file
package eu.greyson.JavaTestRefactor.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
@Data
@AllArgsConstructor
public class Payment {
private String currency;
private BigDecimal amount;
public Payment(String currency, String amount) {
this.currency = currency;
this.amount = new BigDecimal(amount);
}
@Override
public String toString() {
return currency + " " + amount;
}
}
package eu.greyson.JavaTestRefactor.repository;
import eu.greyson.JavaTestRefactor.model.Payment;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
@Component
public class PaymentRepository {
private Map<String, BigDecimal> payments = new HashMap<>();
public boolean isCurrencyExists (String currency) {return payments.containsKey(currency);}
public void save(Payment payment) {
payments.put(payment.getCurrency(), payment.getAmount());
System.out.print("(Added new Currency:" + payment.getCurrency() + ") \n");
}
public void update(Payment payment) {
if (!isCurrencyExists(payment.getCurrency())) {
throw new RuntimeException("Currency " + payment.getCurrency() + "not exists.");
}
BigDecimal amount = payments.get(payment.getCurrency()).add(payment.getAmount());
payments.put(payment.getCurrency(), amount);
System.out.print("(Updated currency:" + payment.getCurrency() + ") \n");
}
public Map<String, BigDecimal> getPayments() {return payments;}
}
package eu.greyson.JavaTestRefactor.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
public class PaymentInputValidator {
public static final String VALIDATION_INVALID_INPUT = "Invalid input: ";
public static final String VALIDATION_3_LETTERS = "Invalid input. Currency must be 3 letters !";
public static final String VALIDATION_AMOUNT_NUMBER = "Invalid input. Amount must consit of only numbers!";
/***
* Input validation for the file or keyboard input
*
* @param input String representations of payment or other text (like the "quit" abort keyword)
* @return <b>true</b> only if the payment string representation has a 3 letter currency and a number amount
* <p><b>false</b> if invalid payment or aborting application using the "quit" string
*/
public boolean validate(String input) {
if (StringUtils.isBlank(input)) {
return false;
}
String[] paymentSplit = input.trim().split(" ");
if (paymentSplit.length != 2) {
System.out.println(VALIDATION_INVALID_INPUT + input);
return false;
}
if (paymentSplit[0].length() != 3) {
System.out.println(VALIDATION_3_LETTERS);
return false;
}
try {
new BigDecimal(paymentSplit[1]);
} catch (NumberFormatException nfe) {
System.out.println(VALIDATION_AMOUNT_NUMBER);
return false;
}
return true;
}
}
package eu.greyson.JavaTestRefactor.service;
import eu.greyson.JavaTestRefactor.model.Payment;
import org.springframework.stereotype.Component;
@Component
final public class PaymentParser {
private PaymentInputValidator paymentInputValidator;
public PaymentParser(PaymentInputValidator paymentInputValidator) {
this.paymentInputValidator = paymentInputValidator;
}
public Payment parse(String payment) {
if (paymentInputValidator.validate(payment)) {
String[] paymentSplit = payment.split(" ");
return new Payment(paymentSplit[0].toUpperCase(), paymentSplit[1]);
}
throw new RuntimeException("Invalid payment input!");
}
}
\ No newline at end of file
package eu.greyson.JavaTestRefactor.service;
import eu.greyson.JavaTestRefactor.model.Payment;
import eu.greyson.JavaTestRefactor.repository.PaymentRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
final public class TrackerService {
private PaymentRepository paymentRepository;
public TrackerService(PaymentRepository paymentRepository) {
this.paymentRepository = paymentRepository;
}
public void processPayment(Payment payment) {
if (paymentRepository.isCurrencyExists(payment.getCurrency())) {
paymentRepository.update(payment);
return;
}
paymentRepository.save(payment);
}
public String getNetCurrencies() {
StringBuilder builder = new StringBuilder();
builder.append("\n");
paymentRepository.getPayments().forEach((k, v) -> {
if (!v.equals(BigDecimal.ZERO)) {
builder.append("> ").append(k).append(" ").append(v).append("\n");
}
});
if (StringUtils.isBlank(builder.toString())) {
return "";
}
return builder.toString();
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment