Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
51 lines (47 loc) · 2.13 KB
/
Copy pathCalculator.java
File metadata and controls
51 lines (47 loc) · 2.13 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
ArrayList<String> name = new ArrayList<>();
ArrayList<Double> price = new ArrayList<>();
//int count;
double sum;
double getSum() {return sum;}
void addGoods (){
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("Введите товар №"+(name.size()+1)+" или \"Завершить\", чтобы завершить ввод:");
String nameInput = scanner.nextLine();
if (nameInput.equalsIgnoreCase("Завершить")) break;
System.out.println("Сколько стоит " + nameInput + "? (формат ввода: \"0,00\"):");
double priceInput;
while (true) {
if (scanner.hasNextDouble()) {
priceInput = scanner.nextDouble();
scanner.nextLine();
break;
}
else {
System.out.println("Некорректный ввод, попробуйте еще раз");
scanner.nextLine();
}
}
name.add(nameInput);
price.add(priceInput);
//count++;
sum += priceInput;
System.out.println("Товар №"+name.size()+" \"" + nameInput + "\" успешно добавлен!\n");
}
scanner.close();
}
void print() {
System.out.println("Добавленные товары:");
System.out.println("-----------------------------------------------");
Formatter rub = new Formatter();
for (int i = 0; i < name.size(); 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)));
}
System.out.println("-----------------------------------------------");
System.out.println("ИТОГО:\t\t\t\t\t\t\t|" + String.format("%7.2f", sum) + rub.format(sum));
System.out.println("-----------------------------------------------");
}
}