Finish assignment
Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
parent
c016d3cf61
commit
af528d26c4
11 changed files with 145 additions and 25 deletions
23
pom.xml
23
pom.xml
|
@ -4,12 +4,12 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>consoleApp</artifactId>
|
<artifactId>Assessment-2</artifactId>
|
||||||
<groupId>dev.kaderli</groupId>
|
<groupId>dev.kaderli</groupId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>consoleApp</name>
|
<name>Assessment-2</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
@ -65,6 +65,15 @@
|
||||||
<mainClass>MainKt</mainClass>
|
<mainClass>MainKt</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.5.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
@ -72,7 +81,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-test-junit5</artifactId>
|
<artifactId>kotlin-test-junit5</artifactId>
|
||||||
<version>1.5.10</version>
|
<version>1.6.10</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -81,10 +90,16 @@
|
||||||
<version>5.6.0</version>
|
<version>5.6.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-params</artifactId>
|
||||||
|
<version>5.6.3</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jetbrains.kotlin</groupId>
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
<version>1.5.10</version>
|
<version>1.6.10</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class Invoice {
|
class Invoice {
|
||||||
/**
|
/**
|
||||||
|
@ -7,18 +8,12 @@ class Invoice {
|
||||||
var products = ArrayList<Product>()
|
var products = ArrayList<Product>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a product to the invoice
|
* Total price of all products.
|
||||||
*/
|
*/
|
||||||
fun addProduct(product: Product) {
|
val totalPrice: Float
|
||||||
products.add(product)
|
get() {
|
||||||
}
|
return products.map { it.totalPrice }.sum()
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Returns the total price of all products.
|
|
||||||
*/
|
|
||||||
fun getTotalPrice(): Float {
|
|
||||||
return products.map { it.totalPrice }.sum()
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/**
|
/**
|
||||||
|
@ -51,11 +46,11 @@ class Invoice {
|
||||||
"Quantity" -> product.quantity = value.toFloat().toInt()
|
"Quantity" -> product.quantity = value.toFloat().toInt()
|
||||||
// "More information about a product is possible, e.g. description, but can be ignored."
|
// "More information about a product is possible, e.g. description, but can be ignored."
|
||||||
"Description", "Pos", "Amount" -> {}
|
"Description", "Pos", "Amount" -> {}
|
||||||
else -> throw Exception("Unknown column: $column")
|
else -> throw ParseException("Unknown column: $column")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
invoice.addProduct(product)
|
invoice.products.add(product)
|
||||||
}
|
}
|
||||||
|
|
||||||
return invoice
|
return invoice
|
||||||
|
@ -65,6 +60,9 @@ class Invoice {
|
||||||
return line.trim().trim('|').split('|').map { it.trim() }
|
return line.trim().trim('|').split('|').map { it.trim() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the given line is a divider in the table.
|
||||||
|
*/
|
||||||
private fun isDivider(line: String): Boolean {
|
private fun isDivider(line: String): Boolean {
|
||||||
return line.trim().startsWith("|*")
|
return line.trim().startsWith("|*")
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ fun main() {
|
||||||
println(invoice.products.size)
|
println(invoice.products.size)
|
||||||
|
|
||||||
// "...and calculates the total price of the purchased products (per file) without using the amount column."
|
// "...and calculates the total price of the purchased products (per file) without using the amount column."
|
||||||
println(invoice.getTotalPrice())
|
println(invoice.totalPrice)
|
||||||
|
|
||||||
// "It should be also possible to get an array or arraylist of all purchased products in a file"
|
// "It should be also possible to get an array or arraylist of all purchased products in a file"
|
||||||
println(invoice.products)
|
println(invoice.products)
|
||||||
|
|
1
src/main/kotlin/ParseException.kt
Normal file
1
src/main/kotlin/ParseException.kt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
class ParseException(message: String) : RuntimeException(message) {}
|
|
@ -1,13 +1,10 @@
|
||||||
/**
|
/**
|
||||||
* This class represents a row in the invoice text files.
|
* 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(
|
data class Product(var name: String = "", var price: Float = 0f, var quantity: Int = 0) {
|
||||||
var name: String = "",
|
/**
|
||||||
var price: Float = 0f,
|
* The total price of the product.
|
||||||
var quantity: Int = 0
|
*/
|
||||||
) {
|
|
||||||
val totalPrice: Float
|
val totalPrice: Float
|
||||||
get() {
|
get() {
|
||||||
return quantity * price
|
return quantity * price
|
||||||
|
|
35
src/test/kotlin/InvoiceTest.kt
Normal file
35
src/test/kotlin/InvoiceTest.kt
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertThrows
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.Arguments
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource
|
||||||
|
import java.util.stream.Stream
|
||||||
|
|
||||||
|
internal class InvoiceTest {
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "Test that the invoice has {0} products and a total price of {1}")
|
||||||
|
@MethodSource("provideValidInvoices")
|
||||||
|
fun getTotalPrice(numberOfProducts: Int, totalPrice: Float, invoice: Invoice) {
|
||||||
|
assertEquals(numberOfProducts, invoice.products.size)
|
||||||
|
assertEquals(totalPrice, invoice.totalPrice)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test invalid columns`() {
|
||||||
|
assertThrows<ParseException> {
|
||||||
|
Invoice.fromFile("src/test/resources/invoice_with_invalid_column.txt")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
fun provideValidInvoices(): Stream<Arguments> {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(6, 3950.3f, Invoice.fromFile("src/test/resources/invoice1.txt")),
|
||||||
|
Arguments.of(6, 3950.3f, Invoice.fromFile("src/test/resources/invoice2.txt")),
|
||||||
|
Arguments.of(6, 3950.3f, Invoice.fromFile("src/test/resources/invoice3.txt"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
src/test/kotlin/ProductTest.kt
Normal file
26
src/test/kotlin/ProductTest.kt
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
|
import org.junit.jupiter.params.provider.Arguments
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource
|
||||||
|
import java.util.stream.Stream
|
||||||
|
|
||||||
|
internal class ProductTest {
|
||||||
|
@ParameterizedTest(name = "Test that the product has a total price of {0}")
|
||||||
|
@MethodSource("provideProducts")
|
||||||
|
fun getTotalPrice(totalPrice: Float, product: Product) {
|
||||||
|
assertEquals(totalPrice, product.totalPrice)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
fun provideProducts(): Stream<Arguments> {
|
||||||
|
return Stream.of(
|
||||||
|
Arguments.of(2f, Product(quantity = 1, price = 2f)),
|
||||||
|
Arguments.of(3f, Product(quantity = 2, price = 1.5f)),
|
||||||
|
Arguments.of(4.5f, Product(quantity = 3, price = 1.5f)),
|
||||||
|
Arguments.of(20f, Product(quantity = 10, price = 2f)),
|
||||||
|
Arguments.of(0f, Product(quantity = 0, price = 99f)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/test/resources/invoice1.txt
Normal file
12
src/test/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/test/resources/invoice2.txt
Normal file
12
src/test/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/test/resources/invoice3.txt
Normal file
12
src/test/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 |
|
||||||
|
|********|********************************|****************|**********|*********|
|
||||||
|
|
12
src/test/resources/invoice_with_invalid_column.txt
Normal file
12
src/test/resources/invoice_with_invalid_column.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
|****************************************|****************|**********|*********|
|
||||||
|
| Test | 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 |
|
||||||
|
|****************************************|****************|**********|*********|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue