From c016d3cf61bb51210a007231668f17867935f9d1 Mon Sep 17 00:00:00 2001 From: Severin Kaderli Date: Mon, 28 Mar 2022 17:43:04 +0200 Subject: [PATCH] Initial commit Signed-off-by: Severin Kaderli --- .gitignore | 3 ++ pom.xml | 91 +++++++++++++++++++++++++++++++++ src/main/kotlin/Invoice.kt | 72 ++++++++++++++++++++++++++ src/main/kotlin/Main.kt | 14 +++++ src/main/kotlin/Product.kt | 15 ++++++ src/main/resources/invoice1.txt | 12 +++++ src/main/resources/invoice2.txt | 12 +++++ src/main/resources/invoice3.txt | 12 +++++ 8 files changed, 231 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/kotlin/Invoice.kt create mode 100644 src/main/kotlin/Main.kt create mode 100644 src/main/kotlin/Product.kt create mode 100644 src/main/resources/invoice1.txt create mode 100644 src/main/resources/invoice2.txt create mode 100644 src/main/resources/invoice3.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cdb3d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target/ +.idea/ +*.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..0c40b80 --- /dev/null +++ b/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + consoleApp + dev.kaderli + 1.0-SNAPSHOT + jar + + consoleApp + + + UTF-8 + official + 1.8 + + + + + mavenCentral + https://repo1.maven.org/maven2/ + + + + + src/main/kotlin + src/test/kotlin + + + org.jetbrains.kotlin + kotlin-maven-plugin + 1.5.10 + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + maven-surefire-plugin + 2.22.2 + + + maven-failsafe-plugin + 2.22.2 + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + MainKt + + + + + + + + org.jetbrains.kotlin + kotlin-test-junit5 + 1.5.10 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.6.0 + test + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + 1.5.10 + + + + \ No newline at end of file diff --git a/src/main/kotlin/Invoice.kt b/src/main/kotlin/Invoice.kt new file mode 100644 index 0000000..f8a182d --- /dev/null +++ b/src/main/kotlin/Invoice.kt @@ -0,0 +1,72 @@ +import java.io.File + +class Invoice { + /** + * The products that belong to the invoice. + */ + var products = ArrayList() + + /** + * 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 = 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 { + return line.trim().trim('|').split('|').map { it.trim() } + } + + private fun isDivider(line: String): Boolean { + return line.trim().startsWith("|*") + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt new file mode 100644 index 0000000..726c4dd --- /dev/null +++ b/src/main/kotlin/Main.kt @@ -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) +} \ No newline at end of file diff --git a/src/main/kotlin/Product.kt b/src/main/kotlin/Product.kt new file mode 100644 index 0000000..f41dcd7 --- /dev/null +++ b/src/main/kotlin/Product.kt @@ -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 + } +} \ No newline at end of file diff --git a/src/main/resources/invoice1.txt b/src/main/resources/invoice1.txt new file mode 100644 index 0000000..378b7c7 --- /dev/null +++ b/src/main/resources/invoice1.txt @@ -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 | + |****************************************|****************|**********|*********| + diff --git a/src/main/resources/invoice2.txt b/src/main/resources/invoice2.txt new file mode 100644 index 0000000..25dd375 --- /dev/null +++ b/src/main/resources/invoice2.txt @@ -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 | + |*****************************************|****************|**********|*********| + diff --git a/src/main/resources/invoice3.txt b/src/main/resources/invoice3.txt new file mode 100644 index 0000000..2aef405 --- /dev/null +++ b/src/main/resources/invoice3.txt @@ -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 | + |********|********************************|****************|**********|*********| +