JetBrains Academy: Enum - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Enum
Currencies:
Declare an enum Currency
.
It should include the following currency codes (according to ISO 4217):
USD — United States dollar EUR — Euro GBP — Pound sterling RUB — Russian ruble UAH — Ukrainian hryvnia KZT — Kazakhstani tenge CAD — Canadian dollar JPY — Japanese yen CNY — Chinese yuan
You must include all of the codes presented above and nothing else. The constants in the enum can be declared in any order.
public enum Currency{
USD, EUR, GBP, RUB, UAH, KZT, CAD, JPY, CNY;
}
Balance management:
You are given the enum Operation
and the class Account
. Their implementations are shown below.
Implement the method changeBalance
to control balances. It has the following declaration:
public static boolean changeBalance(Account account, Operation operation, Long sum)
- If the given operation is DEPOSIT the method adds the sum to the account's balance.
- If the operation is WITHDRAW the method subtracts the given sum from the account's balance. If the given sum is greater than the balance, the method shouldn't change it. Instead, the method must output the string "Not enough money to withdraw." (without quotes, don't forget the full point).
- In any case, the method returns a boolean value — true if the balance has changed, false if it hasn't.
import java.util.Scanner;
public class Main {
/**
* The method change the balance of the given account according to an operation.
* @param account
* @param operation
* @return true if the balance has changed, otherwise - false.
*/
//--------------------------------------------------//
public static boolean changeBalance(Account account, Operation operation, Long sum) {
switch(operation){
case DEPOSIT:
account.setBalance(account.getBalance() + sum);
return true;
case WITHDRAW:
if (sum <= account.getBalance()){
account.setBalance(account.getBalance() - sum);
return true;
} else {
System.out.println("Not enough money to withdraw.");
return false;
}
default:
break;
}
return false;
}
//--------------------------------------------------//
/* Do not change code below */
enum Operation {
/**
* deposit (add) an amount into an Account
*/
DEPOSIT,
/**
* withdraw (subtract) an amount from an Account
*/
WITHDRAW
}
static class Account {
private String code;
private Long balance;
public String getCode() {
return code;
}
public Long getBalance() {
return balance;
}
public void setBalance(Long balance) {
this.balance = balance;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] parts = scanner.nextLine().split("\\s+");
Account account = new Account();
account.setBalance(Long.parseLong(parts[0]));
Operation operation = Operation.valueOf(parts[1]);
Long sum = Long.parseLong(parts[2]);
if (changeBalance(account, operation, sum)) {
System.out.println(account.getBalance());
}
}
}
Robot control:
There is a robot on the game field. The position of the robot on this field is described by two integer coordinates: X and Y. The X axis is oriented from left to right, the Y axis — from bottom to top.
At the initial moment, the robot is in some coordinate on the field. It's also known where the robot looks: up, down, to the right or to the left. The initial position of the robot and its direction can have any values. You need to bring the robot to the destination point of the game field.
A robot is described by the Robot class. You can use the following methods of this class (with unknown implementation):
public class Robot {
public Direction getDirection() {
// current direction
}
public int getX() {
// current X coordinate
}
public int getY() {
// current Y o
}
public void turnLeft() {
// rotate the robot 90 degrees counterclockwise
}
public void turnRight() {
// rotate the robot 90 degrees clockwise
}
public void stepForward() {
// take one step in the current direction
// x or y coordinate will be changed by 1
}
}
The direction of the robot is an enumeration:
public enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
Create moveRobot(Robot robot, int toX, int toY)
method.
static void moveRobot(Robot robot, int toX, int toY) {
int currentX = robot.getX();
int currentY = robot.getY();
int numberOfStepsX = toX - currentX;
int numberOfStepsY = toY - currentY;
int moveX = Math.abs(numberOfStepsX);
int moveY = Math.abs(numberOfStepsY);
// reset direction
while (robot.getDirection() != Direction.UP){
robot.turnRight();
}
if (numberOfStepsY > 0){
for (int i = 0; i < moveY; i++) {
robot.stepForward();
numberOfStepsY--;
}
} else if (numberOfStepsY < 0){
robot.turnRight();
robot.turnRight();
for (int i = 0; i < moveY; i++) {
robot.stepForward();
numberOfStepsY--;
}
}
// reset direction
while (robot.getDirection() != Direction.UP){
robot.turnRight();
}
if (numberOfStepsX > 0){
robot.turnRight();
for (int i = 0; i < moveX; i++) {
robot.stepForward();
numberOfStepsX--;
}
} else if (numberOfStepsX < 0){
robot.turnLeft();
for (int i = 0; i < moveX; i++){
robot.stepForward();
numberOfStepsX--;
}
}
}
Days of week:
Declare an enum named DayOfWeek
. It should include all days of the week in uppercase.
enum DayOfWeek{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
The secret enumeration:
You are given a hidden enum named Secret
with some constants in uppercase.
Write a program that counts how many constants in the enumeration start with "STAR".
The enum is accessible during testing.
public class Main {
public static void main(String[] args) {
int counter = 0;
for(Secret e : Secret.values()) {
if (e.name().startsWith("STAR")){
counter++;
}
}
System.out.println(counter);
}
}
Danger levels:
You are given an enum named DangerLevel
.
Add an integer field to store danger levels and match the number with each constant:
HIGH — 3 MEDIUM — 2 LOW — 1
You should also add the instance method getLevel
that returns the associated integer number.
enum DangerLevel {
HIGH(3),
MEDIUM(2),
LOW(1);
private int level;
DangerLevel(int danger){
this.level = danger;
}
public int getLevel(){
return level;
}
}