aboutsummaryrefslogtreecommitdiff
path: root/Practical2
diff options
context:
space:
mode:
authorCamil Staps2015-11-28 14:02:39 +0100
committerCamil Staps2015-11-28 14:02:39 +0100
commit625eddb5b8e62acc8460542ccb13535b55d9aa9e (patch)
treee5fb7154a7658d0ee087d2c2ba075f3cb78292cf /Practical2
parentstart assignment 11 (diff)
start practical 2
Diffstat (limited to 'Practical2')
-rw-r--r--Practical2/src/nl/camilstaps/cs/CheckoutHelper.java32
-rw-r--r--Practical2/src/nl/camilstaps/cs/Main.java23
-rw-r--r--Practical2/tester/samples/small_1.in3
-rw-r--r--Practical2/tester/samples/small_1.out2
-rw-r--r--Practical2/tester/samples/small_2.in3
-rw-r--r--Practical2/tester/samples/small_2.out2
-rwxr-xr-xPractical2/tester/test.sh31
7 files changed, 96 insertions, 0 deletions
diff --git a/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java b/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
new file mode 100644
index 0000000..5ad7d2d
--- /dev/null
+++ b/Practical2/src/nl/camilstaps/cs/CheckoutHelper.java
@@ -0,0 +1,32 @@
+package nl.camilstaps.cs;
+
+import java.util.ArrayList;
+
+/**
+ * Created by camil on 11/28/15.
+ */
+class CheckoutHelper {
+ private final ArrayList<Integer> products = new ArrayList<>();
+ private int minimal_price;
+
+ public CheckoutHelper() {
+ }
+
+ public int minimumPayment(int dividers) {
+ int sum = minimal_price;
+ for (int n : products)
+ sum += n;
+ return sum;
+ }
+
+ public void addProduct(int price) {
+ int has_to_pay = price / 5;
+ int rest = price % 5;
+
+ minimal_price += has_to_pay;
+ if (rest != 0) {
+ products.add(rest);
+ }
+ }
+
+}
diff --git a/Practical2/src/nl/camilstaps/cs/Main.java b/Practical2/src/nl/camilstaps/cs/Main.java
new file mode 100644
index 0000000..2009c4d
--- /dev/null
+++ b/Practical2/src/nl/camilstaps/cs/Main.java
@@ -0,0 +1,23 @@
+package nl.camilstaps.cs;
+
+import java.util.Scanner;
+
+public class Main {
+
+ public static void main(String[] args) {
+ CheckoutHelper ch = new CheckoutHelper();
+ int n_dividers = readCheckoutHelper(new Scanner(System.in), ch);
+ System.out.println(ch.minimumPayment(n_dividers));
+ }
+
+ private static int readCheckoutHelper(Scanner sc, CheckoutHelper ch) {
+ int n_products = sc.nextInt();
+ int n_dividers = sc.nextInt();
+
+ for (int i = 0; i < n_products; i++)
+ ch.addProduct(sc.nextInt());
+
+ return n_dividers;
+ }
+
+}
diff --git a/Practical2/tester/samples/small_1.in b/Practical2/tester/samples/small_1.in
new file mode 100644
index 0000000..b4e65e4
--- /dev/null
+++ b/Practical2/tester/samples/small_1.in
@@ -0,0 +1,3 @@
+5 1
+10 23 43 637 45
+
diff --git a/Practical2/tester/samples/small_1.out b/Practical2/tester/samples/small_1.out
new file mode 100644
index 0000000..2a3ce5a
--- /dev/null
+++ b/Practical2/tester/samples/small_1.out
@@ -0,0 +1,2 @@
+755
+
diff --git a/Practical2/tester/samples/small_2.in b/Practical2/tester/samples/small_2.in
new file mode 100644
index 0000000..47817f7
--- /dev/null
+++ b/Practical2/tester/samples/small_2.in
@@ -0,0 +1,3 @@
+6 2
+1 1 1 1 1 1
+
diff --git a/Practical2/tester/samples/small_2.out b/Practical2/tester/samples/small_2.out
new file mode 100644
index 0000000..77ac542
--- /dev/null
+++ b/Practical2/tester/samples/small_2.out
@@ -0,0 +1,2 @@
+0
+
diff --git a/Practical2/tester/test.sh b/Practical2/tester/test.sh
new file mode 100755
index 0000000..f0f46e8
--- /dev/null
+++ b/Practical2/tester/test.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+java="/usr/lib/jvm/java-8-openjdk-amd64/bin/java"
+
+failed=0
+
+cd "$(dirname $0)/../out/production/Practical2"
+for tc in ../../../tester/samples/*.in; do
+ answer=$(cat ${tc/in/out})
+ header=$(head -n1 "$tc" | tr -d '\n')
+ echo -n "Running $(basename $tc) $(printf '%-12s' "($answer ")$(printf '%-12s' "/ $header)") ... "
+ time_start=$(($(date +%s%N)/1000000))
+ result=$(eval "cat '$tc' | /usr/lib/jvm/java-8-openjdk-amd64/bin/java nl.camilstaps.cs.Main")
+ time_end=$(($(date +%s%N)/1000000))
+ time=`expr $time_end - $time_start`
+ if [ $result != $answer ]; then
+ echo "failure ($time ms)."
+ failed=$(($failed+1))
+ else
+ echo "success ($time ms)."
+ fi
+done
+cd - >/dev/null
+
+if [ $failed -eq 0 ]; then
+ echo "All tests passed."
+elif [ $failed -eq 1 ]; then
+ echo "1 test failed."
+else
+ echo "$failed tests failed."
+fi
+