Skip to content

Commit

Permalink
Add concept enums with concept exercise logs-logs-logs (#2496)
Browse files Browse the repository at this point in the history
* Add `enums` concept

* Add concept exercise `logs-logs-logs`

* Add enums to practice exercise configs
  • Loading branch information
sanderploegsma authored Oct 3, 2023
1 parent 529b75a commit bbac886
Show file tree
Hide file tree
Showing 18 changed files with 740 additions and 67 deletions.
5 changes: 5 additions & 0 deletions concepts/enums/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "Enums are useful to create a predefined set of constants.",
"authors": ["sanderploegsma"],
"contributors": []
}
120 changes: 120 additions & 0 deletions concepts/enums/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# About

An _enum type_ is a special data type that enables for a variable to be a set of predefined constants.
The variable must be equal to one of the values that have been predefined for it.
Common examples include compass directions (values of `NORTH`, `SOUTH`, `EAST`, and `WEST`) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

## Defining an enum type

In the Java programming language, you define an enum type by using the `enum` keyword.
For example, you would specify a days-of-the-week enum type as:

```java
public enum DayOfWeek {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
```

You should use enum types any time you need to represent a fixed set of constants.
That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time - for example, the choices on a menu, command line flags, and so on.

## Using an enum type

Here is some code that shows you how to use the `DayOfWeek` enum defined above:

```java
public class Shop {
public String getOpeningHours(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
return "9am - 5pm";
case SATURDAY:
return "10am - 4pm"
case SUNDAY:
return "Closed.";
}
}
}
```

```java
var shop = new Shop();
shop.getOpeningHours(DayOfWeek.WEDNESDAY);
// => "9am - 5pm"
```

## Adding methods and fields

Java programming language enum types are much more powerful than their counterparts in other languages.
The `enum` declaration defines a _class_ (called an _enum type_).
The enum class body can include methods and other fields:

```java
public enum Rating {
GREAT(5),
GOOD(4),
OK(3),
BAD(2),
TERRIBLE(1);

private final int numberOfStars;

Rating(int numberOfStars) {
this.numberOfStars = numberOfStars;
}

public int getNumberOfStars() {
return this.numberOfStars;
}
}
```

Calling the `getNumberOfStars` method on a member of the `Rating` enum type:

```java
Rating.GOOD.getNumberOfStars();
// => 4
```

## Built-in methods

The compiler automatically adds some special methods when it creates an enum.

For example, they have a static `values` method that returns an array containing all of the values of the enum in the order they are declared:

```java
for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
System.out.println(dayOfWeek.toString());
}
```

The snippet above would print the following:

```text
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
```

The compiler also adds a static `valueOf` method that returns an enum member based on its name:

```java
DayOfWeek.valueOf("SUNDAY");
// => DayOfWeek.SUNDAY
```
89 changes: 89 additions & 0 deletions concepts/enums/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Introduction

An _enum type_ is a special data type that enables for a variable to be a set of predefined constants.
The variable must be equal to one of the values that have been predefined for it.
Common examples include compass directions (values of `NORTH`, `SOUTH`, `EAST`, and `WEST`) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

## Defining an enum type

In the Java programming language, you define an enum type by using the `enum` keyword.
For example, you would specify a days-of-the-week enum type as:

```java
public enum DayOfWeek {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
}
```

You should use enum types any time you need to represent a fixed set of constants.
That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time - for example, the choices on a menu, command line flags, and so on.

## Using an enum type

Here is some code that shows you how to use the `DayOfWeek` enum defined above:

```java
public class Shop {
public String getOpeningHours(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
return "9am - 5pm";
case SATURDAY:
return "10am - 4pm"
case SUNDAY:
return "Closed.";
}
}
}
```

```java
var shop = new Shop();
shop.getOpeningHours(DayOfWeek.WEDNESDAY);
// => "9am - 5pm"
```

## Adding methods and fields

Java programming language enum types are much more powerful than their counterparts in other languages.
The `enum` declaration defines a _class_ (called an _enum type_).
The enum class body can include methods and other fields:

```java
public enum Rating {
GREAT(5),
GOOD(4),
OK(3),
BAD(2),
TERRIBLE(1);

private final int numberOfStars;

Rating(int numberOfStars) {
this.numberOfStars = numberOfStars;
}

public int getNumberOfStars() {
return this.numberOfStars;
}
}
```

Calling the `getNumberOfStars` method on a member of the `Rating` enum type:

```java
Rating.GOOD.getNumberOfStars();
// => 4
```
6 changes: 6 additions & 0 deletions concepts/enums/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"url": "https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html",
"description": "Java Tuturial on Enum Types"
}
]
Loading

0 comments on commit bbac886

Please sign in to comment.