Finish assignment

Signed-off-by: Severin Kaderli <severin@kaderli.dev>
This commit is contained in:
Severin Kaderli 2022-03-30 19:17:43 +02:00
parent c016d3cf61
commit af528d26c4
Signed by: severinkaderli
GPG key ID: F419F8835B72F0C4
11 changed files with 145 additions and 25 deletions

23
pom.xml
View file

@ -4,12 +4,12 @@
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>
<artifactId>Assessment-2</artifactId>
<groupId>dev.kaderli</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consoleApp</name>
<name>Assessment-2</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -65,6 +65,15 @@
<mainClass>MainKt</mainClass>
</configuration>
</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>
</build>
@ -72,7 +81,7 @@
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.5.10</version>
<version>1.6.10</version>
<scope>test</scope>
</dependency>
<dependency>
@ -81,10 +90,16 @@
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.5.10</version>
<version>1.6.10</version>
</dependency>
</dependencies>

View file

@ -1,4 +1,5 @@
import java.io.File
import java.util.*
class Invoice {
/**
@ -7,18 +8,12 @@ class Invoice {
var products = ArrayList<Product>()
/**
* Adds a product to the invoice
* Total price of all products.
*/
fun addProduct(product: Product) {
products.add(product)
}
/**
* Returns the total price of all products.
*/
fun getTotalPrice(): Float {
return products.map { it.totalPrice }.sum()
}
val totalPrice: Float
get() {
return products.map { it.totalPrice }.sum()
}
companion object {
/**
@ -51,11 +46,11 @@ class Invoice {
"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")
else -> throw ParseException("Unknown column: $column")
}
}
invoice.addProduct(product)
invoice.products.add(product)
}
return invoice
@ -65,6 +60,9 @@ class Invoice {
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 {
return line.trim().startsWith("|*")
}

View file

@ -7,7 +7,7 @@ fun main() {
println(invoice.products.size)
// "...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"
println(invoice.products)

View file

@ -0,0 +1 @@
class ParseException(message: String) : RuntimeException(message) {}

View file

@ -1,13 +1,10 @@
/**
* 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
) {
data class Product(var name: String = "", var price: Float = 0f, var quantity: Int = 0) {
/**
* The total price of the product.
*/
val totalPrice: Float
get() {
return quantity * price

View 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"))
)
}
}
}

View 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)),
)
}
}
}

View 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 |
|****************************************|****************|**********|*********|

View 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 |
|*****************************************|****************|**********|*********|

View 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 |
|********|********************************|****************|**********|*********|

View 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 |
|****************************************|****************|**********|*********|