Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/target/
metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>cashregister</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions SOLUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
To test the solution, go to the tests for the file parser service and run. Check the output.txt file to see the output.
Feel free to add files for testing. Just make sure to update the file paths in the tests.
67 changes: 67 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.softwriters</groupId>
<artifactId>cashregister</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cashregister</name>
<description>Softwriters Code Assessment</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- junit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.softwriters.cashregister;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CashregisterApplication {

public static void main(String[] args) {
SpringApplication.run(CashregisterApplication.class, args);
}

}
52 changes: 52 additions & 0 deletions src/main/java/com/softwriters/cashregister/model/ChangeModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.softwriters.cashregister.model;

import java.math.BigDecimal;

public class ChangeModel {

private BigDecimal value;

private String currencyNameSingular;

private String currencyNamePlural;

public ChangeModel(String value, String currencyNameSingular, String currencyNamePlural) {
this.value = new BigDecimal(value);
this.currencyNameSingular = currencyNameSingular;
this.currencyNamePlural = currencyNamePlural;
}

public BigDecimal getValue() {
return value;
}

public void setValue(BigDecimal value) {
this.value = value;
}

public String getCurrencyNameSingular() {
return currencyNameSingular;
}

public void setCurrencyNameSingular(String currencyNameSingular) {
this.currencyNameSingular = currencyNameSingular;
}

public String getCurrencyNamePlural() {
return currencyNamePlural;
}

public void setCurrencyNamePlural(String currencyNamePlural) {
this.currencyNamePlural = currencyNamePlural;
}

public String getCurrencyName(int numberOfDenominations) {
if(numberOfDenominations>1) {
return this.currencyNamePlural;
}
else {
return this.currencyNameSingular;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.softwriters.cashregister.service;

import java.math.BigDecimal;
import java.util.HashSet;

import com.softwriters.cashregister.util.ChangeUtil;

public class ChangeProcessingService {

public ChangeProcessingService() {

}

public String calculateChange(BigDecimal totalDue, BigDecimal amountGiven) {

String change = "";
//Determine change needed
BigDecimal changeNeeded = amountGiven.subtract(totalDue);

for(int i=0;i<ChangeUtil.change.length;i++) {

int numberOfChange = 0;

//iterate from highest change value to lowest and subtract till less than zero or zero
while((changeNeeded.subtract(ChangeUtil.change[i].getValue()).compareTo(ChangeUtil.zero)==1)||
(changeNeeded.subtract(ChangeUtil.change[i].getValue()).compareTo(ChangeUtil.zero)==0)) {

//track number of change denominations and subtract from total change required
numberOfChange++;
changeNeeded = changeNeeded.subtract(ChangeUtil.change[i].getValue());
}
//create strings for change based on number of change denominations
if(numberOfChange>0)
change += numberOfChange + " " + ChangeUtil.change[i].getCurrencyName(numberOfChange) + ",";


}

return change.substring(0, change.length()-1);
}

public String calculateRandomChange(BigDecimal totalDue, BigDecimal amountGiven) {

String change = "";

//Determine change needed
BigDecimal changeNeeded = amountGiven.subtract(totalDue);

//Create a hash set to keep track of used change value indexes
HashSet<Integer> indexSet = new HashSet<>();

//While all denominations haven't been used and change needed isn't zero
while(indexSet.size()!=ChangeUtil.change.length && changeNeeded.compareTo(ChangeUtil.zero)!= 0) {

//generate random index
int randomIndex = generateRandomIndex();

//check to see if random index already used; if so re-calculate till find one
while(indexSet.contains(randomIndex)) {
randomIndex = generateRandomIndex();
}

int numberOfChange = 0;

//subtract change till at zero or less than zero
while((changeNeeded.subtract(ChangeUtil.change[randomIndex].getValue()).compareTo(ChangeUtil.zero)==1)||
(changeNeeded.subtract(ChangeUtil.change[randomIndex].getValue()).compareTo(ChangeUtil.zero)==0)) {

numberOfChange++;
changeNeeded = changeNeeded.subtract(ChangeUtil.change[randomIndex].getValue());
}

if(numberOfChange>0)
change += numberOfChange + " " + ChangeUtil.change[randomIndex].getCurrencyName(numberOfChange) + ",";

//Add index to set
indexSet.add(randomIndex);
}


return change.substring(0, change.length()-1);
}

private int generateRandomIndex() {
int random = (int)(Math.random() * ChangeUtil.change.length);
return random;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.softwriters.cashregister.service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Scanner;

public class FlatFileParserService {

public FlatFileParserService() {

}

public File processFile(File inputFile) throws IOException {

//Create Change Processing Service
ChangeProcessingService changeProcessor = new ChangeProcessingService();

//Scan in the input file
Scanner readFile = new Scanner(inputFile);

//Create an output file
File output = new File("output.txt");
output.createNewFile();

//Create a file writer to write to output file
FileWriter fileWriter = new FileWriter("output.txt");
//While the scanner sees new tokens
while(readFile.hasNext()) {

//Grab the next String token and split using a delimiter
String currentString = readFile.next();
String[] values = currentString.split(",");

//Convert two numbers to double values
BigDecimal totalDue = new BigDecimal(values[0]);
BigDecimal paid = new BigDecimal(values[1]);

//Write change due strings to the file
if(isDivisibleByThree(values[0])) {
fileWriter.write("\n"+changeProcessor.calculateRandomChange(totalDue, paid));
}
else {
fileWriter.write("\n"+changeProcessor.calculateChange(totalDue, paid));
}

//If more input in file, go to next line
if(readFile.hasNextLine()) {
readFile.nextLine();
}
}

//close scanner and writer
fileWriter.close();
readFile.close();

return output;
}


private boolean isDivisibleByThree(String totalDue) {

int sumOfDigits = 0;
//Add each digit in the string
for(int i = 0;i<totalDue.length();i++) {

if(totalDue.charAt(i)!='.') {
sumOfDigits += (int)totalDue.charAt(i) - '0';
}
}

//if the sum mod 3 equals 0 then it is divisible by 3
if(sumOfDigits%3==0) {
return true;
}
else {
return false;
}

}
}
Loading