Skip to content

Commit c46c02c

Browse files
committed
06.03.2017
1 parent fbfea0c commit c46c02c

File tree

11 files changed

+171
-31
lines changed

11 files changed

+171
-31
lines changed

com/javarush/test/level26/lesson15/big01/CashMachine.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
public class CashMachine {
1212
public static void main(String[] args) {
1313
Locale.setDefault(Locale.ENGLISH);
14-
Operation operation;
15-
do {
16-
try {
14+
15+
try {
16+
Operation operation;
17+
do {
1718
operation = ConsoleHelper.askOperation();
1819
CommandExecutor.execute(operation);
19-
} catch (InterruptOperationException e) {
20-
ConsoleHelper.writeMessage("Buy, my dear friend!");
21-
break;
22-
}
20+
} while (operation != Operation.EXIT);
2321

24-
} while (operation != Operation.EXIT);
22+
} catch (InterruptOperationException e) {
23+
ConsoleHelper.writeMessage("Buy, my dear friend!");
24+
}
2525

2626
}
2727
}

com/javarush/test/level26/lesson15/big01/ConsoleHelper.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ public static void writeMessage(String message) {
1616
System.out.println(message);
1717
}
1818

19-
public static String readString() {
19+
public static String readString() throws InterruptOperationException{
2020
String in = "";
2121

2222
try {
2323
in = reader.readLine();
24+
2425
}catch (IOException ex) {
26+
writeMessage("Wrong choice");
27+
}
28+
29+
if (in.toLowerCase().equals("exit")) {
30+
throw new InterruptOperationException();
2531

2632
}
2733

2834
return in;
2935
}
3036

31-
public static String askCurrencyCode() {
37+
public static String askCurrencyCode() throws InterruptOperationException{
3238
writeMessage("Enter the currency code.");
3339
String s = readString();
3440
while (s.length() != 3)
@@ -39,10 +45,10 @@ public static String askCurrencyCode() {
3945
return s.toUpperCase();
4046
}
4147

42-
public static String[] getValidTwoDigits(String currencyCode) {
48+
public static String[] getValidTwoDigits(String currencyCode) throws InterruptOperationException{
4349
String valueAndAmount;
4450
String[] result;
45-
writeMessage("Input nominal and amount:");
51+
writeMessage("Input nominal and amount: " + currencyCode);
4652

4753
while (true) {
4854
valueAndAmount = readString();
@@ -61,18 +67,13 @@ public static String[] getValidTwoDigits(String currencyCode) {
6167

6268
public static Operation askOperation() throws InterruptOperationException {
6369
Operation operation;
64-
String userChoice = "";
6570
while (true) {
66-
System.out.println("Choose operation: 1 - INFO, 2 - DEPOSIT, 3 - WITHDRAW, 4 - EXIT");
71+
writeMessage("Choose operation: 1 - INFO, 2 - DEPOSIT, 3 - WITHDRAW, 4 - EXIT");
6772
try {
68-
userChoice = readString();
69-
operation = Operation.getAllowableOperationByOrdinal(Integer.parseInt(userChoice));
73+
operation = Operation.getAllowableOperationByOrdinal(Integer.parseInt(readString()));
7074
break;
7175
}catch (IllegalArgumentException ex) {
72-
if (userChoice.toLowerCase().equals("exit")) throw new InterruptOperationException();
73-
System.out.println("Wrong choice. Choose operation: 1 - INFO, 2 - DEPOSIT, 3 - WITHDRAW, 4 - EXIT");
74-
}catch (Exception ex) {
75-
System.out.println("Wrong choice. Choose operation: 1 - INFO, 2 - DEPOSIT, 3 - WITHDRAW, 4 - EXIT");
76+
writeMessage("Wrong choice");
7677
}
7778

7879
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.javarush.test.level26.lesson15.big01.command;
22

3+
import com.javarush.test.level26.lesson15.big01.exception.InterruptOperationException;
4+
35
/**
46
* Created by SUSTAVOV on 06.01.2017.
57
*/
68
interface Command {
7-
void execute();
9+
void execute() throws InterruptOperationException;
810
}

com/javarush/test/level26/lesson15/big01/command/CommandExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.javarush.test.level26.lesson15.big01.command;
22

33
import com.javarush.test.level26.lesson15.big01.Operation;
4+
import com.javarush.test.level26.lesson15.big01.exception.InterruptOperationException;
45

56
import java.util.HashMap;
67
import java.util.Map;
@@ -17,7 +18,7 @@ public final class CommandExecutor {
1718
operationCommandMap.put(Operation.EXIT, new ExitCommand());
1819
}
1920

20-
public static final void execute(Operation operation) {
21+
public static final void execute(Operation operation) throws InterruptOperationException {
2122
if (operationCommandMap.containsKey(operation)) {
2223
operationCommandMap.get(operation).execute();
2324
}

com/javarush/test/level26/lesson15/big01/command/DepositCommand.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import com.javarush.test.level26.lesson15.big01.ConsoleHelper;
44
import com.javarush.test.level26.lesson15.big01.CurrencyManipulator;
55
import com.javarush.test.level26.lesson15.big01.CurrencyManipulatorFactory;
6+
import com.javarush.test.level26.lesson15.big01.exception.InterruptOperationException;
67

78
/**
89
* Created by SUSTAVOV on 06.01.2017.
910
*/
1011
class DepositCommand implements Command {
1112
@Override
12-
public void execute() {
13+
public void execute() throws InterruptOperationException{
1314
String currencyCode = ConsoleHelper.askCurrencyCode();
1415
CurrencyManipulator currencyManipulator = CurrencyManipulatorFactory.getManipulatorByCurrencyCode(currencyCode);
1516
String[] valueQuantityBanknotes = ConsoleHelper.getValidTwoDigits(currencyCode);
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.javarush.test.level26.lesson15.big01.command;
22

3+
import com.javarush.test.level26.lesson15.big01.ConsoleHelper;
4+
import com.javarush.test.level26.lesson15.big01.exception.InterruptOperationException;
5+
36
/**
47
* Created by SUSTAVOV on 06.01.2017.
58
*/
69
class ExitCommand implements Command {
7-
@Override
8-
public void execute() {
9-
1010

11+
@Override
12+
public void execute() throws InterruptOperationException{
13+
ConsoleHelper.writeMessage("Aru you sure you want to exit? <y, n>");
14+
if (ConsoleHelper.readString().toLowerCase().equals("y")) {
15+
ConsoleHelper.writeMessage("Bye");
16+
throw new InterruptOperationException();
17+
}
1118

1219
}
1320
}

com/javarush/test/level26/lesson15/big01/command/InfoCommand.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package com.javarush.test.level26.lesson15.big01.command;
22

3+
import com.javarush.test.level26.lesson15.big01.ConsoleHelper;
34
import com.javarush.test.level26.lesson15.big01.CurrencyManipulator;
45
import com.javarush.test.level26.lesson15.big01.CurrencyManipulatorFactory;
6+
import com.javarush.test.level26.lesson15.big01.exception.InterruptOperationException;
57

68
import java.util.Collection;
7-
import java.util.Collections;
8-
import java.util.Map;
9+
910

1011
/**
1112
* Created by SUSTAVOV on 06.01.2017.
1213
*/
1314
class InfoCommand implements Command {
1415
@Override
15-
public void execute() {
16+
public void execute() throws InterruptOperationException {
1617
// if (!CurrencyManipulatorFactory.hasMoney()) {
1718
// System.out.println("No money available.");
1819
// } else {
@@ -47,7 +48,7 @@ public void execute() {
4748
}
4849
}
4950
if (count == 0) {
50-
System.out.println("No money available.");
51+
ConsoleHelper.writeMessage("No money available.");
5152
}
5253
}
5354
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.javarush.test.level26.lesson15.big01.command;
22

3+
import com.javarush.test.level26.lesson15.big01.exception.InterruptOperationException;
4+
35
/**
46
* Created by SUSTAVOV on 06.01.2017.
57
*/
68
class WithdrawCommand implements Command {
79
@Override
8-
public void execute() {
10+
public void execute() throws InterruptOperationException{
911

1012
}
1113
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.javarush.test.level30.lesson08.home01;
2+
3+
public class Pair {
4+
private int x;
5+
private int y;
6+
7+
public Pair(int x, int y) {
8+
this.x = x;
9+
this.y = y;
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return String.format("x=%d, y=%d", x, y);
15+
}
16+
17+
public void swap() {
18+
x = x ^ y;
19+
y = x ^ y;
20+
x = y ^ x;
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.javarush.test.level30.lesson08.home01;
2+
3+
/* Swap по-новому
4+
В классе Pair реализуйте метод swap, который должен для x, y менять местами их значения.
5+
Можно использовать только операции 1)исключающее или, 2) присваивание
6+
Не оставляйте комментарии, не меняйте остальной код
7+
*/
8+
public class Solution {
9+
public static void main(String[] args) {
10+
Pair pair = new Pair(4, 5);
11+
System.out.println(pair);
12+
pair.swap();
13+
System.out.println(pair);
14+
15+
String a = new String("hello");
16+
String b = new String(a);
17+
String c = a;
18+
char[] d = {'h', 'e', 'l', 'l', 'o'};
19+
System.out.println(a == "hello");
20+
System.out.println(a.equals(d));
21+
System.out.println(a.equals(b));
22+
System.out.println(a == c);
23+
System.out.println(a == b);
24+
25+
int result = 12 + 2 * 5 % 3 - 15 / 4;
26+
String x = 12 - 6 + "Hello" + 7 + 5;
27+
28+
System.out.println(result + " - " + x);
29+
}
30+
}

0 commit comments

Comments
 (0)