Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
53 lines (48 loc) · 1.7 KB
/
Copy pathCalculator.java
File metadata and controls
53 lines (48 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
ArrayList<String> name = new ArrayList<>();
ArrayList<Double> price = new ArrayList<>();
int countter = 0;
double sum;
double getSum() {return sum;}
public void newTovar() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите товар № "+(countter+1)+" или \"Завершить\", чтобы завершить ввод:");
String nameInput = scanner.nextLine();
if (nameInput.equalsIgnoreCase("Завершить")) {
break;
}
System.out.println("Сколько стоит " + nameInput + "? (формат ввода: \"XXX,xx\"):");
double priceInput;
while (true) {
if (scanner.hasNextDouble()) {
priceInput = scanner.nextDouble();
scanner.nextLine();
if (priceInput < 1){
System.out.println("Введите число больше 0");
}
else {break;}}
else {
System.out.println("Некорректный ввод, попробуйте еще раз");
scanner.nextLine();
}
}
name.add(nameInput);
price.add(priceInput);
countter++;
sum += priceInput;
System.out.println("Товар №"+countter+ "\"" + nameInput + "\" успешно добавлен!");
}
}
void print() {
System.out.println("Добавленные товары: \n");
RubFormat rub = new RubFormat();
for (int i = 0; i < countter; i++) {
System.out.println(String.format("%2d ",(i + 1)) + String.format(" |%-27s|",name.get(i)) + String.format("%7.2f", price.get(i))
+ rub.format(price.get(i))+ "\n"+ "\n");
}
System.out.println("ИТОГО:" + String.format("%7.2f", sum) + rub.format(sum));
}
}