Source

ATM - CodeChef

My solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>

#include "../lib/unity/src/unity.h" // throwtheswitch.org


float solution(int withdrawal, float balance) {
    if (withdrawal % 5) return balance;
    float result = balance - withdrawal - 0.5;
    return (result >= 0) ? result : balance;
}


void test_provided_success() {
    TEST_ASSERT_EQUAL_FLOAT(solution(30.0, 120.0), 89.50);
}


// Withdrawal amount is not a multiple of 5.
void test_provided_fail_incorrect() {
    TEST_ASSERT_EQUAL_FLOAT(solution(42.0, 120.0), 120.0);
}


// Insufficient funds.
void test_provided_fail_insufficient() {
    TEST_ASSERT_EQUAL_FLOAT(solution(300, 120.0), 120.0);
}


int run_tests(void) {
    UNITY_BEGIN();
    RUN_TEST(test_provided_success);
    RUN_TEST(test_provided_fail_incorrect);
    RUN_TEST(test_provided_fail_insufficient);
    return UNITY_END();
}


int main(void) {
    run_tests();

    // OLJ input.
    int withdrawal;
    float balance;
    if (scanf("%d %f", &withdrawal, &balance) != EOF) {
        printf("%0.2f\n", solution(withdrawal, balance));
    }
    return 0;
}