Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Проектная работа № 1 Кондрашов Виктор #258

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
private ArrayList<Item> items;
private int numberOfPeople;
public Calculator() { items = new ArrayList<>();}
public void start() {
Scanner scanner = new Scanner(System.in);
boolean validInput = false;
while (!validInput) {
System.out.println("На скольких человек необходимо разделить счёт:");
String inmput = scanner.nextLine().trim();
if (inmput.matches("\\d+")) {
numberOfPeople = Integer.parseInt(inmput);
if (numberOfPeople <= 1) {
System.out.println("Ошибка: Введите корректное количество гостей, больше одного.");
} else {
validInput = true;
}
}
}
while (true) {
System.out.println("Введите название товара и его стоимость в формате 'название стоимость': например, пиво 58.99\nЛибо введите команду 'Завершить' для того, чтоб завершить процесс добавления товаров.");
String line = scanner.nextLine().trim();
if (line.equalsIgnoreCase("завершить")) {
break;
}
String[] parts = line.split(" ");
if (parts.length != 2 || !isValidPrice(parts[1])) {
System.out.println("Ошибка: Неккоректный формат ввода или некорректная сумма товара.");
continue;
}
String name = parts[0];
double price = Double.parseDouble(parts[1]);
items.add(new Item(name, price));
System.out.println("Товар успешно добавлен.");
System.out.println("Хотите добавить еще один товар?");
}
System.out.println("Добавленные товары:");
for(Item item : items) {
System.out.println(item.getName() + " - " + item.getPrice());
}
double total = 0;
for (Item item : items) {
total += item.getPrice();
}
double perPerson = total / numberOfPeople;
String rublesString = Formatter.formatRubles(perPerson);
System.out.println("Каждый человек должен заплатить: " + rublesString);
}
private boolean isValidPrice(String priceStr) {
String[] parts = priceStr.split("\\.");
if (parts.length !=2) {
return false;
}
if (!parts[0].matches("\\d+") || !parts[1].matches("\\d{2}")){
return false;
}
return true;
}
}
14 changes: 14 additions & 0 deletions src/main/java/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Formatter {
public static String formatRubles(double amount) {
int rubles = (int) amount;
String rublesString;
if (rubles % 10 == 1 && rubles % 100 != 11){
rublesString = "Рубль";
} else if (rubles % 10 >= 2 && rubles % 10 <= 4 &&(rubles % 100 < 10 || rubles % 100 >= 20)) {
rublesString = "рубля";
} else {
rublesString = "рублей";
}
return rubles + " " + rublesString;
}
}
11 changes: 11 additions & 0 deletions src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Item {
private String name;
private double price;

public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {return name;}
public double getPrice() {return price;}
}
6 changes: 5 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@


public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
calculator.start();


}
}