Циклы while и for. оператоторы break и continue
This commit is contained in:
+14
-2
@@ -1,8 +1,20 @@
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
UserManager manager = new UserManager();
|
int balance = 450;
|
||||||
manager.startApp();
|
int fee = 100;
|
||||||
|
|
||||||
|
for (int i = 1; i <= 5; i++) {
|
||||||
|
balance -= 100;
|
||||||
|
if (balance < 100) {
|
||||||
|
System.out.println("Деньги кончились, прерываем работу! ");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
System.out.println("Этаж " + i + " проверен. ");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
public class Practice
|
|
||||||
{
|
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
|
||||||
int age = 17;
|
|
||||||
boolean hasPermission = false;
|
|
||||||
|
|
||||||
if (age >= 18) {
|
|
||||||
System.out.println(" Доступ разрешен");}
|
|
||||||
else if (age > 18 && hasPermission == true) {
|
|
||||||
System.out.println(" Доступ разрешен по записке от родителей");
|
|
||||||
} else {
|
|
||||||
System.out.println(" Доступ заблокирован ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class ProductCard {
|
|
||||||
private String title;
|
|
||||||
private int price;
|
|
||||||
private int discount;
|
|
||||||
|
|
||||||
public ProductCard (String cardTitle, int cardPrice, int cardDiscount) {
|
|
||||||
title = cardTitle;
|
|
||||||
price = cardPrice;
|
|
||||||
discount = cardDiscount;
|
|
||||||
}
|
|
||||||
public int getFinalPrice() {
|
|
||||||
return price - (price * discount / 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPrice() {
|
|
||||||
return price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getDiscount() {
|
|
||||||
return discount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
public class User {
|
|
||||||
private String name;
|
|
||||||
private int balance;
|
|
||||||
|
|
||||||
public User(String name, int balance) {
|
|
||||||
this.name = name;
|
|
||||||
this.balance = balance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getBalance() {
|
|
||||||
return balance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBalance(int balance) {
|
|
||||||
this.balance = balance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class UserManager {
|
|
||||||
public void startApp() {
|
|
||||||
|
|
||||||
ArrayList<User> userList = new ArrayList<>();
|
|
||||||
|
|
||||||
userList.add(new User("Алексей", 5000));
|
|
||||||
userList.add(new User("Мария", 10000));
|
|
||||||
|
|
||||||
System.out.println(userList.get(0).getName() + " имеет баланс: " + userList.get(0).getBalance());
|
|
||||||
System.out.println(userList.get(1).getName() + " имеет баланс: " + userList.get(1).getBalance());
|
|
||||||
|
|
||||||
ArrayList<ProductCard> productList = new ArrayList<>();
|
|
||||||
productList.add(new ProductCard("Сапоги", 3000, 20));
|
|
||||||
productList.add(new ProductCard("Шуба", 23000, 10));
|
|
||||||
|
|
||||||
|
|
||||||
buyProduct(userList.get(0), productList.get(0));
|
|
||||||
buyProduct(userList.get(0), productList.get(1));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void buyProduct (User user, ProductCard product) {
|
|
||||||
int finalPrice = product.getFinalPrice();
|
|
||||||
|
|
||||||
if (user.getBalance() >= finalPrice) {
|
|
||||||
int newBalance = user.getBalance() - finalPrice;
|
|
||||||
user.setBalance(newBalance);
|
|
||||||
System.out.println(" Пользователь " + user.getName() + " успешно купил " + product.getTitle() + " за " + finalPrice);
|
|
||||||
System.out.println(" Остаток на балансе " + user.getBalance());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user