Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

написал калькулятор счета #1

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

Open
APKArtisan wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from dev
Open
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
103 changes: 101 additions & 2 deletions src/main/java/Main.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,105 @@
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Locale;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Calculator calculator = new Calculator();
calculator.start();
}
}
}
class Calculator{
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
String rub;
public void start(){
System.out.println("Добро пожаловать в калькулятор счета!");
System.out.println("На скольких человек необходимо разделить счет?");
while (true){
System.out.println("Введите количество персон, больше одной...");
if(scanner.hasNextInt()){
int command = scanner.nextInt();
if(command > 1){
CalculatorScore(command);
break;
}else{
System.out.println("Некоректное количество гостей!");
scanner.nextLine();
}
}else{
System.out.println("Некоректное количество гостей!");
scanner.next();
}
}
}
public void CalculatorScore(int person){
ArrayList<Product> productList = new ArrayList<>();
double productSumm = 0;
while(true){
System.out.println("Введите название товара...");
scanner.nextLine();
String product = scanner.nextLine();
while (true){
System.out.println("Введите стоимость товара в формате рубли.копейки...");
if(scanner.hasNextDouble()){
double cost = scanner.nextDouble();
scanner.nextLine();
if(cost > 0 ){
Product newProduct = new Product(product, cost);
productList.add(newProduct);
productSumm = productSumm + newProduct.price;
rub = formatAmount(productSumm);
System.out.println("Товар " + newProduct.name + " успешно добавлен");
System.out.printf("Общая стоимость товаров %.2f %s%n", productSumm, rub);
break;
}else{
System.out.println("Введите неотрицательное число, больше нуля!!!");
}
}else{
System.out.println("Некоректное значение!");
scanner.nextLine();

}
}
System.out.println("Хотите добавить еще один товар?");
String answer = scanner.next();
if(answer.equalsIgnoreCase("Завершить")){
System.out.println("Добавленные товары:");
for (Product productItem : productList) {
System.out.println("Название: " + productItem.name + ", Стоимость: " + productItem.price + " " + (rub = formatAmount(productItem.price)));
}
productSumm = productSumm / person;
rub = formatAmount(productSumm);
System.out.printf("Сколько должен заплатить каждый человек:%n%.2f %s", productSumm, rub);
break;
}

}

}
public String formatAmount(Double summ){
int formatedAmount = (int) Math.floor(summ);
String rub;

if((formatedAmount % 100 >= 11) && (formatedAmount % 100 <= 14)){
Copy link

@Nick-review Nick-review Feb 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно через switch написать, будет удобнее читать

rub ="рублей";
}else if((formatedAmount % 10) == 1) {
rub ="рубль";
}else if((formatedAmount % 10 > 1) && (formatedAmount % 10 <= 4)) {
rub ="рубля";
}else{
rub ="рублей";
}

return rub;
}
}

class Product {
public String name;
public double price;

public Product(String name, double price) {
this.name = name;
this.price = price;
}
}

AltStyle によって変換されたページ (->オリジナル) /