|
17 | 17 | * RU: Первый в мире консольный интерет магазин. |
18 | 18 | */ |
19 | 19 | 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; |
24 | 24 |
|
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 | + } |
31 | 31 |
|
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; |
35 | 35 |
|
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")); |
51 | 51 |
|
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(); |
57 | 57 |
|
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 | + } |
68 | 69 |
|
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); |
77 | 78 |
|
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 | + } |
94 | 94 | } |
95 | 95 |
|
0 commit comments