Skip to content

Commit 9f3f591

Browse files
committed
Fixing a bug in strategy, Demo.java. Where you put C in the last prompt, it doesn't stop asking product to you, becasue it don't enter in the code block after if(strategy == null). So I limited that if statement
1 parent db29bcf commit 9f3f591

1 file changed

Lines changed: 67 additions & 67 deletions

File tree

  • src/refactoring_guru/strategy/example

src/refactoring_guru/strategy/example/Demo.java

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,79 @@
1717
* RU: Первый в мире консольный интерет магазин.
1818
*/
1919
public class Demo {
20-
private static Map<Integer, Integer> priceOnProducts = new HashMap<>();
21-
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
22-
private static Order order = new Order();
23-
private static PayStrategy strategy;
20+
private static Map<Integer, Integer> priceOnProducts = new HashMap<>();
21+
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
22+
private static Order order = new Order();
23+
private static PayStrategy strategy;
2424

25-
static {
26-
priceOnProducts.put(1, 2200);
27-
priceOnProducts.put(2, 1850);
28-
priceOnProducts.put(3, 1100);
29-
priceOnProducts.put(4, 890);
30-
}
25+
static {
26+
priceOnProducts.put(1, 2200);
27+
priceOnProducts.put(2, 1850);
28+
priceOnProducts.put(3, 1100);
29+
priceOnProducts.put(4, 890);
30+
}
3131

32-
public static void main(String[] args) throws IOException {
33-
while (!order.isClosed()) {
34-
int cost;
32+
public static void main(String[] args) throws IOException {
33+
while (!order.isClosed()) {
34+
int cost;
3535

36-
String continueChoice;
37-
do {
38-
System.out.print("Please, select a product:" + "\n" +
39-
"1 - Mother board" + "\n" +
40-
"2 - CPU" + "\n" +
41-
"3 - HDD" + "\n" +
42-
"4 - Memory" + "\n");
43-
int choice = Integer.parseInt(reader.readLine());
44-
cost = priceOnProducts.get(choice);
45-
System.out.print("Count: ");
46-
int count = Integer.parseInt(reader.readLine());
47-
order.setTotalCost(cost * count);
48-
System.out.print("Do you wish to continue selecting products? Y/N: ");
49-
continueChoice = reader.readLine();
50-
} while (continueChoice.equalsIgnoreCase("Y"));
36+
String continueChoice;
37+
do {
38+
System.out.print("Please, select a product:" + "\n" +
39+
"1 - Mother board" + "\n" +
40+
"2 - CPU" + "\n" +
41+
"3 - HDD" + "\n" +
42+
"4 - Memory" + "\n");
43+
int choice = Integer.parseInt(reader.readLine());
44+
cost = priceOnProducts.get(choice);
45+
System.out.print("Count: ");
46+
int count = Integer.parseInt(reader.readLine());
47+
order.setTotalCost(cost * count);
48+
System.out.print("Do you wish to continue selecting products? Y/N: ");
49+
continueChoice = reader.readLine();
50+
} while (continueChoice.equalsIgnoreCase("Y"));
5151

52-
if (strategy == null) {
53-
System.out.println("Please, select a payment method:" + "\n" +
54-
"1 - PalPay" + "\n" +
55-
"2 - Credit Card");
56-
String paymentMethod = reader.readLine();
52+
if (strategy == null) {
53+
System.out.println("Please, select a payment method:" + "\n" +
54+
"1 - PalPay" + "\n" +
55+
"2 - Credit Card");
56+
String paymentMethod = reader.readLine();
5757

58-
// EN: Client creates different strategies based on input from
59-
// user, application configuration, etc.
60-
//
61-
// RU: Клиент создаёт различные стратегии на основании
62-
// пользовательских данных, конфигурации и прочих параметров.
63-
if (paymentMethod.equals("1")) {
64-
strategy = new PayByPayPal();
65-
} else {
66-
strategy = new PayByCreditCard();
67-
}
58+
// EN: Client creates different strategies based on input from
59+
// user, application configuration, etc.
60+
//
61+
// RU: Клиент создаёт различные стратегии на основании
62+
// пользовательских данных, конфигурации и прочих параметров.
63+
if (paymentMethod.equals("1")) {
64+
strategy = new PayByPayPal();
65+
} else {
66+
strategy = new PayByCreditCard();
67+
}
68+
}
6869

69-
// EN: Order object delegates gathering payment data to strategy
70-
// object, since only strategies know what data they need to
71-
// process a payment.
72-
//
73-
// RU: Объект заказа делегирует сбор платёжных данны стратегии,
74-
// т.к. только стратегии знают какие данные им нужны для приёма
75-
// оплаты.
76-
order.processOrder(strategy);
70+
// EN: Order object delegates gathering payment data to strategy
71+
// object, since only strategies know what data they need to
72+
// process a payment.
73+
//
74+
// RU: Объект заказа делегирует сбор платёжных данны стратегии,
75+
// т.к. только стратегии знают какие данные им нужны для приёма
76+
// оплаты.
77+
order.processOrder(strategy);
7778

78-
System.out.print("Pay " + order.getTotalCost() + " units or Continue shopping? P/C: ");
79-
String proceed = reader.readLine();
80-
if (proceed.equalsIgnoreCase("P")) {
81-
// EN: Finally, strategy handles the payment.
82-
//
83-
// RU: И наконец, стратегия запускает приём платежа.
84-
if (strategy.pay(order.getTotalCost())) {
85-
System.out.println("Payment has been successful.");
86-
} else {
87-
System.out.println("FAIL! Please, check your data.");
88-
}
89-
order.setClosed();
90-
}
91-
}
92-
}
93-
}
79+
System.out.print("Pay " + order.getTotalCost() + " units or Continue shopping? P/C: ");
80+
String proceed = reader.readLine();
81+
if (proceed.equalsIgnoreCase("P")) {
82+
// EN: Finally, strategy handles the payment.
83+
//
84+
// RU: И наконец, стратегия запускает приём платежа.
85+
if (strategy.pay(order.getTotalCost())) {
86+
System.out.println("Payment has been successful.");
87+
} else {
88+
System.out.println("FAIL! Please, check your data.");
89+
}
90+
order.setClosed();
91+
}
92+
}
93+
}
9494
}
9595

0 commit comments

Comments
 (0)