Skip to content

Commit 2a62a64

Browse files
TalesDiasgithub-actions[bot]SilvamirkoperilloErikSchierboom
committed
Implement concept exercise conditionals-ternary
Implement concept exercise conditionals-ternary Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Silva <silva@localhost.localdomain> Co-authored-by: mirkoperillo <mirko.perillo@gmail.com> Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>
1 parent 72985dc commit 2a62a64

10 files changed

Lines changed: 289 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Ternary Operator
2+
3+
The _ternary operators_ can be thought of as being a compact version of _if-else_. It's usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`.
4+
5+
A lot of simple _if/else_ expressions can be simplified using _ternary operators_.
6+
7+
```java
8+
// this ternary statement
9+
return 5 > 4 ? true : false;
10+
11+
// is equivalent to this if/else
12+
if ( 5 > 4 ) {
13+
return true;
14+
} else {
15+
return false;
16+
}
17+
```
18+
19+
So, how to decide between _if-else_ and _ternary_ ? Well, _ternary operators_ are used in simple scenarios, where you just need to return a value based on a condition and no extra computation is needed. Use an _if-else_ for everthing else, like nested conditions, big expressions and when more than one line is needed to decide the return value.
20+
21+
While you can nest _ternary operators_, the code often becomes hard to read. In these cases, nested if's are preferred.
22+
23+
```java
24+
// hard to read
25+
int value = expr1 ? expr2 && expr3 ? val1 : (val2 + val3) : expr4;
26+
27+
// easier to read
28+
if (expr1){
29+
30+
if (expr2 && expr3){
31+
32+
return val1;
33+
}
34+
35+
return val2 + val3;
36+
}
37+
return val4;
38+
39+
40+
```
41+
42+
_Ternary operators_ and _if/else_ statements are a good example that you have different ways of achieving the same result when programming.
43+
44+
For more examples check out [this][ternary-operator-first] and [this][ternary-operator-second] sources.
45+
46+
[ternary-operator-first]: https://www.programiz.com/java-programming/ternary-operator
47+
[ternary-operator-second]: https://www.baeldung.com/java-ternary-operator
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## General
2+
3+
- Check the [this][ternary-operator-first] and [this][ternary-operator-second] examples on how to use _ternary operators_.
4+
5+
## 1. Implement the multipliers
6+
7+
- Both multiplier methods depend on a certain condition being met, you must check that before deciding what should be returned.
8+
9+
## 2. Calculate the final salary
10+
11+
- If a salary is greater then the maximum, you can ignore the final value and use the maximim value instead.
12+
13+
[ternary-operator-first]: https://www.programiz.com/java-programming/ternary-operator
14+
[ternary-operator-second]: https://www.baeldung.com/java-ternary-operator
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
In this exercise, you'll be implementing rules for calculating the total salary of a empyoyee in a month. The International Siderurgy Company (ISC) needs help to calculate the salary for the employees, given that different factors can alter the final wage value for each employee.
2+
3+
You have three tasks and you should use the ternary operator instead of if/else statements to implement them.
4+
5+
## 1. Determine the salary multiplier
6+
7+
Implement the `multiplierPerDaysSkipped` method that returns the salary multiplier based on the number of days the employee skipped the job. A 15% penalty is applied if more than five days were skipped.
8+
9+
```java
10+
int daysSkipped = 3;
11+
multiplierPerDaysSkipped(daysSkipped);
12+
// => 1
13+
14+
daysSkipped = 7;
15+
multiplierPerDaysSkipped(daysSkipped);
16+
// => 0.85
17+
```
18+
19+
## 2. Calculate the bonus for products sold
20+
21+
Implement the `multiplierPerProductsSold` and `bonusForProductSold` methods. The ISC pays ten monetary units for each product sold, but if the employee sold more than twenty products, the multiplier is improved to thirteen. `multiplierPerProductsSold` should decide which multiplier is applied and `bonusForProductSold` should return the total bonus in monetary units.
22+
23+
```java
24+
int productsSold = 21;
25+
multiplierPerProductsSold(productsSold);
26+
// => 13
27+
28+
productsSold = 5;
29+
bonusForProductSold(productsSold);
30+
// => 50
31+
```
32+
33+
## 3. Calculate the final salary for the employee
34+
35+
Implement the `finalSalary` method. It should be able to multiply the base salary of 1000.00 by the salary multiplier and sum the bonus and return the result, but keep in mind that salaries should be capped at 2000.00;
36+
37+
```java
38+
int daysSkipped = 2;
39+
int productsSold = 3;
40+
finalSalary(daysSkipped, productsSold);
41+
// => 1030
42+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The _ternary operator_ is a lightweight, compact alternative for simple _if/else_ statements. Usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`, as follows:
2+
3+
```java
4+
boolean expr = 0 != 200;
5+
6+
// Ternary statement
7+
int value = expr ? 22 : 33;
8+
// => 22
9+
```
10+
11+
A lot of simple _if/else_ expressions can be simplified using _ternary operators_.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "TalesDias",
5+
"exercism_username": "TalesDias"
6+
}
7+
]
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Learning objectives
2+
3+
- Know how to use ternary exepressions - using the `?:` operator.
4+
5+
## Out of scope
6+
7+
- Nothing notable
8+
9+
## Concepts
10+
11+
- `ternary-operators`: how to use the ternary operator to do conditional logic
12+
13+
## Prerequisites
14+
15+
This exercise's prerequisites Concepts are:
16+
17+
- `numbers`: double and int types
18+
- `conditionals-if` : use `if else` expressions
19+
20+
## Analyzer
21+
22+
This exercise could have the following rule added to the analyzer:
23+
24+
- Verify that ternary operators were used.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class SalaryCalculator {
2+
3+
public double multiplierPerDaysSkipped (int daysSkipped) {
4+
return daysSkipped < 5 ? 1 : 0.85;
5+
}
6+
7+
public int multiplierPerProductsSold (int productsSold) {
8+
return productsSold < 20 ? 10 : 13;
9+
}
10+
11+
public double bonusForProductSold (int productsSold) {
12+
return productsSold * multiplierPerProductsSold(productsSold);
13+
}
14+
15+
public double finalSalary (int daysSkipped, int productsSold) {
16+
17+
double finalSalary = 1000.0 * multiplierPerDaysSkipped(daysSkipped) + bonusForProductSold(productsSold);
18+
19+
return finalSalary > 2000.0 ? 2000.0 : finalSalary;
20+
}
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.13"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
12+
}
13+
14+
test {
15+
testLogging {
16+
exceptionFormat = 'short'
17+
showStandardStreams = true
18+
events = ["passed", "failed", "skipped"]
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class SalaryCalculator {
2+
public double multiplierPerDaysSkipped (int daysSkipped) {
3+
throw new UnsupportedOperationException("Please implement the SalaryCalculator.multiplierPerDaysSkipped() method");
4+
}
5+
6+
public int multiplierPerProductsSold (int productsSold) {
7+
throw new UnsupportedOperationException("Please implement the SalaryCalculator.multiplierPerProductsSold() method");
8+
}
9+
10+
public double bonusForProductSold (int productsSold) {
11+
throw new UnsupportedOperationException("Please implement the SalaryCalculator.bonusForProductSold() method");
12+
}
13+
14+
public double finalSalary (int daysSkipped, int productsSold) {
15+
throw new UnsupportedOperationException("Please implement the SalaryCalculator.finalSalary() method");
16+
}
17+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import org.junit.Before;
2+
import org.junit.Test;
3+
import org.junit.Ignore;
4+
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
8+
public class SalaryCalculatorTests {
9+
10+
public SalaryCalculator calculator;
11+
12+
13+
@Before
14+
public void setUp() {
15+
calculator = new SalaryCalculator();
16+
}
17+
18+
@Test
19+
@Ignore("Remove to run test")
20+
public void regularSalary() {
21+
assertThat(calculator.finalSalary(0, 0)).isEqualTo(1000.0);
22+
}
23+
24+
@Test
25+
@Ignore("Remove to run test")
26+
public void skippedBelowThreshold () {
27+
assertThat(calculator.finalSalary(3, 0)).isEqualTo(1000.0);
28+
}
29+
30+
@Test
31+
@Ignore("Remove to run test")
32+
public void skippedAboveThreshold() {
33+
assertThat(calculator.finalSalary(5, 0)).isEqualTo(850.0);
34+
}
35+
36+
@Test
37+
@Ignore("Remove to run test")
38+
public void soldBelowThreshold() {
39+
assertThat(calculator.finalSalary(0, 10)).isEqualTo(1100.0);
40+
}
41+
42+
@Test
43+
@Ignore("Remove to run test")
44+
public void soldAboveThreshold() {
45+
assertThat(calculator.finalSalary(0, 25)).isEqualTo(1325.0);
46+
}
47+
48+
@Test
49+
@Ignore("Remove to run test")
50+
public void skippedBelowThresholdAndSoldBelowThreshold() {
51+
assertThat(calculator.finalSalary(2, 5)).isEqualTo(1050.0);
52+
}
53+
54+
@Test
55+
@Ignore("Remove to run test")
56+
public void skippedBelowThresholdAndSoldAboveThreshold() {
57+
assertThat(calculator.finalSalary(4, 40)).isEqualTo(1520.0);
58+
}
59+
60+
@Test
61+
@Ignore("Remove to run test")
62+
public void skippedAboveThresholdAndSoldBelowThreshold() {
63+
assertThat(calculator.finalSalary(10, 2)).isEqualTo(870.0);
64+
}
65+
66+
@Test
67+
@Ignore("Remove to run test")
68+
public void skippedAboveThresholdAndSoldAboveThreshold() {
69+
assertThat(calculator.finalSalary(7, 50)).isEqualTo(1500.0);
70+
}
71+
72+
@Test
73+
@Ignore("Remove to run test")
74+
public void salaryCanReachCloseToMaximum() {
75+
assertThat(calculator.finalSalary(0, 76)).isEqualTo(1988.0);
76+
}
77+
78+
@Test
79+
@Ignore("Remove to run test")
80+
public void salaryRespectMaximum() {
81+
assertThat(calculator.finalSalary(0, 77)).isEqualTo(2000.0);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)