Initial commit
Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
commit
c016d3cf61
8 changed files with 231 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
target/
|
||||||
|
.idea/
|
||||||
|
*.iml
|
91
pom.xml
Normal file
91
pom.xml
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>consoleApp</artifactId>
|
||||||
|
<groupId>dev.kaderli</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>consoleApp</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<kotlin.code.style>official</kotlin.code.style>
|
||||||
|
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>mavenCentral</id>
|
||||||
|
<url>https://repo1.maven.org/maven2/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src/main/kotlin</sourceDirectory>
|
||||||
|
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
<version>1.5.10</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>compile</id>
|
||||||
|
<phase>compile</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>test-compile</id>
|
||||||
|
<phase>test-compile</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test-compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.22.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-failsafe-plugin</artifactId>
|
||||||
|
<version>2.22.2</version>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>1.6.0</version>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>MainKt</mainClass>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-junit5</artifactId>
|
||||||
|
<version>1.5.10</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<version>5.6.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
|
<version>1.5.10</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
72
src/main/kotlin/Invoice.kt
Normal file
72
src/main/kotlin/Invoice.kt
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class Invoice {
|
||||||
|
/**
|
||||||
|
* The products that belong to the invoice.
|
||||||
|
*/
|
||||||
|
var products = ArrayList<Product>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a product to the invoice
|
||||||
|
*/
|
||||||
|
fun addProduct(product: Product) {
|
||||||
|
products.add(product)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the total price of all products.
|
||||||
|
*/
|
||||||
|
fun getTotalPrice(): Float {
|
||||||
|
return products.map { it.totalPrice }.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* Creates an invoice from a file.
|
||||||
|
*/
|
||||||
|
fun fromFile(fileName: String): Invoice {
|
||||||
|
val invoice = Invoice()
|
||||||
|
var columns: List<String> = emptyList()
|
||||||
|
var processedHeader = false
|
||||||
|
|
||||||
|
File(fileName).readLines().forEach { line ->
|
||||||
|
// Don't process table dividers
|
||||||
|
if (isDivider(line) || line.isBlank()) {
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!processedHeader) {
|
||||||
|
columns = parseLine(line)
|
||||||
|
processedHeader = true
|
||||||
|
return@forEach
|
||||||
|
}
|
||||||
|
|
||||||
|
val product = Product()
|
||||||
|
val lineItems = parseLine(line)
|
||||||
|
columns.forEachIndexed { index, column ->
|
||||||
|
val value = lineItems[index]
|
||||||
|
when (column) {
|
||||||
|
"Item" -> product.name = value
|
||||||
|
"Unit Price" -> product.price = value.toFloat()
|
||||||
|
"Quantity" -> product.quantity = value.toFloat().toInt()
|
||||||
|
// "More information about a product is possible, e.g. description, but can be ignored."
|
||||||
|
"Description", "Pos", "Amount" -> {}
|
||||||
|
else -> throw Exception("Unknown column: $column")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
invoice.addProduct(product)
|
||||||
|
}
|
||||||
|
|
||||||
|
return invoice
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseLine(line: String): List<String> {
|
||||||
|
return line.trim().trim('|').split('|').map { it.trim() }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isDivider(line: String): Boolean {
|
||||||
|
return line.trim().startsWith("|*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
src/main/kotlin/Main.kt
Normal file
14
src/main/kotlin/Main.kt
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
fun main() {
|
||||||
|
//val invoice = Invoice.fromFile("src/main/resources/invoice1.txt")
|
||||||
|
//val invoice = Invoice.fromFile("src/main/resources/invoice2.txt")
|
||||||
|
val invoice = Invoice.fromFile("src/main/resources/invoice3.txt")
|
||||||
|
|
||||||
|
// "Implement a program that determines the number of purchased products in the table..."
|
||||||
|
println(invoice.products.size)
|
||||||
|
|
||||||
|
// "...and calculates the total price of the purchased products (per file) without using the amount column."
|
||||||
|
println(invoice.getTotalPrice())
|
||||||
|
|
||||||
|
// "It should be also possible to get an array or arraylist of all purchased products in a file"
|
||||||
|
println(invoice.products)
|
||||||
|
}
|
15
src/main/kotlin/Product.kt
Normal file
15
src/main/kotlin/Product.kt
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
/**
|
||||||
|
* This class represents a row in the invoice text files.
|
||||||
|
*
|
||||||
|
* Using product.totalPrice to total price of the product can be retrieved.
|
||||||
|
*/
|
||||||
|
data class Product(
|
||||||
|
var name: String = "",
|
||||||
|
var price: Float = 0f,
|
||||||
|
var quantity: Int = 0
|
||||||
|
) {
|
||||||
|
val totalPrice: Float
|
||||||
|
get() {
|
||||||
|
return quantity * price
|
||||||
|
}
|
||||||
|
}
|
12
src/main/resources/invoice1.txt
Normal file
12
src/main/resources/invoice1.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
|****************************************|****************|**********|*********|
|
||||||
|
| Item | Unit Price | Quantity | Amount |
|
||||||
|
|****************************************|****************|**********|*********|
|
||||||
|
| Table | 699.00 | 1.00 | 699.00 |
|
||||||
|
| Dining chairs | 79.95 | 10.00 | 799.50 |
|
||||||
|
| Sofa | 329.00 | 2.00 | 658.00 |
|
||||||
|
| footstool | 199.00 | 3.00 | 597.00 |
|
||||||
|
| Armchair | 279.00 | 3.00 | 837.00 |
|
||||||
|
| Lamp | 89.95 | 4.00 | 359.80 |
|
||||||
|
|****************************************|****************|**********|*********|
|
||||||
|
|
12
src/main/resources/invoice2.txt
Normal file
12
src/main/resources/invoice2.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
|*****************************************|****************|**********|*********|
|
||||||
|
| Item | Description | Unit Price | Quantity | Amount |
|
||||||
|
|*****************************************|****************|**********|*********|
|
||||||
|
| Table | acacia235x100 cm | 699.00 | 1.00 | 699.00 |
|
||||||
|
| Dining chairs | white/in/outdoor | 79.95 | 10.00 | 799.50 |
|
||||||
|
| Sofa | 3-seat sofa, dark grey| 329.00 | 2.00 | 658.00 |
|
||||||
|
| footstool | Bomstad dark brown | 199.00 | 3.00 | 597.00 |
|
||||||
|
| Armchair | Idhult black | 279.00 | 3.00 | 837.00 |
|
||||||
|
| Lamp | Floor lamp, white | 89.95 | 4.00 | 359.80 |
|
||||||
|
|*****************************************|****************|**********|*********|
|
||||||
|
|
12
src/main/resources/invoice3.txt
Normal file
12
src/main/resources/invoice3.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
|********|********************************|****************|**********|*********|
|
||||||
|
| Pos | Item | Unit Price | Quantity | Amount |
|
||||||
|
|********|********************************|****************|**********|*********|
|
||||||
|
| 1 | Table | 699.00 | 1.00 | 699.00 |
|
||||||
|
| 2 | Dining chairs | 79.95 | 10.00 | 799.50 |
|
||||||
|
| 3 | Sofa | 329.00 | 2.00 | 658.00 |
|
||||||
|
| 4 | footstool | 199.00 | 3.00 | 597.00 |
|
||||||
|
| 5 | Armchair | 279.00 | 3.00 | 837.00 |
|
||||||
|
| 6 | Lamp | 89.95 | 4.00 | 359.80 |
|
||||||
|
|********|********************************|****************|**********|*********|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue