From 7b19243d94b4c8f981f5d6fb39415c4fa09e7b9a Mon Sep 17 00:00:00 2001 From: Matthew <80854685+Matthew-Mak@users.noreply.github.com> Date: Wed, 8 Oct 2025 16:13:38 +0500 Subject: [PATCH 1/2] Added lesson 3 files --- src/lessons/lesson03/Main.java | 489 +++++++++++++++++++++++++++++++++ 1 file changed, 489 insertions(+) create mode 100644 src/lessons/lesson03/Main.java diff --git a/src/lessons/lesson03/Main.java b/src/lessons/lesson03/Main.java new file mode 100644 index 0000000..a3d3a99 --- /dev/null +++ b/src/lessons/lesson03/Main.java @@ -0,0 +1,489 @@ +package lessons.lesson03; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +class Person { + private String name; + private int age; + + public Person(String Name, int Age) { + name = Name; + age = Age; + } + + public void introduce() { + System.out.println("Здравствуйте, моё имя: " + name + ", возраст: " + age); + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} + +class Rectangle { + private double length; + private double width; + + public Rectangle(double Length, double Width) { + length = Length; + width = Width; + } + + public double calculateSquare() { + return length * width; + } + + public double calculatePerimeter() { + return 2 * (length + width); + } + + public double getLength() { + return length; + } + + public double getWidth() { + return width; + } +} + +class Car { + private String country; + private String model; + private int yearOfManufacture; + + public Car(String Country, String Model, int YearOfManufacture) { + country = Country; + model = Model; + yearOfManufacture = YearOfManufacture; + } + + public void printCarInfo() { + System.out.println("Автомобиль: " + model); + System.out.println("Страна производства: " + country); + System.out.println("Год выпуска: " + yearOfManufacture); + } + + public String getCountry() { + return country; + } + + public String getModel() { + return model; + } + + public int getYearOfManufacture() { + return yearOfManufacture; + } +} + +class BankAccount { + private String fio; + private String accountNumber; + private double balance; + + public BankAccount(String Fio, String AccountNumber, double InitialBalance) { + fio = Fio; + accountNumber = AccountNumber; + balance = InitialBalance; + } + + public void deposit(double amount) { + if (amount > 0) { + balance += amount; + System.out.println("Пополнение счёта на: " + amount + ". Баланс: " + balance); + } else { + System.out.println("Сумма не должна быть меньше или равной нулю."); + } + } + + public void withdraw(double amount) { + if (amount > 0 && amount <= balance) { + balance -= amount; + System.out.println("Снято " + amount + ". Новый баланс: " + balance); + } else { + System.out.println("Недостаточно средств, либо сумма меньше или равна нулю."); + } + } + + public double getBalance() { + return balance; + } + + public String getFio() { + return fio; + } + + public String getAccountNumber() { + return accountNumber; + } +} + +class Book { + private String isbn; + private String name; + private String author; + private int year; + private boolean status; // true - доступна, false - зарезервирована + + private static List booksList = new ArrayList<>(); + + public Book(String isbn, String name, String author, int year) { + this.isbn = isbn; + this.name = name; + this.author = author; + this.year = year; + this.status = true; + } + + public String getBookInfo() { + String theStatus; + if (status) { + theStatus = "Доступна"; + } else { + theStatus = "Зарезервирована"; + } + return "ISBN: " + isbn + ", Название: " + name + ", Автор: " + author + + ", Год: " + year + ", Статус: " + theStatus; + } + + public static List getBooks() { + return booksList; + } + + public static void addNewBook(Book book) { + booksList.add(book); + System.out.println("Книга добавлена: " + book.getName()); + } + + public void reserveBook() { + if (status) { + status = false; + System.out.println("Книга \"" + name + "\" зарезервирована."); + } else { + System.out.println("Книга \"" + name + "\" уже зарезервирована кем-то другим."); + } + } + + public String getIsbn() { + return isbn; + } + + public String getName() { + return name; + } + + public String getAuthor() { + return author; + } + + public int getYear() { + return year; + } + + public boolean isStatus() { + return status; + } +} + +class OnlineStore { + private String code; + private String name; + private double price; + private int count; + + private static List productsList = new ArrayList<>(); + + public OnlineStore(String code, String name, double price, int count) { + this.code = code; + this.name = name; + this.price = price; + this.count = count; + } + + public static void addProduct(OnlineStore product) { + productsList.add(product); + System.out.println("Товар успшено добавлен: " + product.getName()); + } + + public void buyProduct(int quantity) { + if (quantity <= count && quantity > 0) { + count -= quantity; + double total = price * quantity; + System.out.println("Куплено " + quantity + " количество \"" + name + + "\". Сумма: " + total); + } else { + System.out.println("Недостаточно товара на складе или количество меньше/равно нулю."); + } + } + + public String getProductInfo() { + return "Код: " + code + ", Название: " + name + ", Цена: " + price + + ", Количество: " + count; + } + + public static List getProducts() { + return productsList; + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } + + public int getCount() { + return count; + } +} + +class BankSystem { + private String accountNumber; + private String fio; + private double balance; + + private static List accountsList = new ArrayList<>(); + + public BankSystem(String accountNumber, String fio, double initialBalance) { + this.accountNumber = accountNumber; + this.fio = fio; + this.balance = initialBalance; + } + + public String getAccountInfo() { + return "Счет: " + accountNumber + ", ФИО: " + fio + ", Баланс: " + balance; + } + + public static void addAccount(BankSystem account) { + accountsList.add(account); + System.out.println("Счет добавлен: " + account.getAccountNumber()); + } + + public static void deleteAccount(String accountNumber) { + accountsList.removeIf(acc -> acc.getAccountNumber().equals(accountNumber)); + System.out.println("Счет " + accountNumber + " удален."); + } + + public void replenishAccount(double amount) { + if (amount > 0) { + balance += amount; + System.out.println("Счет пополнен на " + amount + ". Новый баланс: " + balance); + } else { + System.out.println("Сумма должна быть положительной!"); + } + } + + public static void transferMoneyBetweenAccounts(String fromAccount, String toAccount, double amount) { + BankSystem from = null; + BankSystem to = null; + + for (BankSystem acc : accountsList) { + if (acc.getAccountNumber().equals(fromAccount)) { + from = acc; + } + if (acc.getAccountNumber().equals(toAccount)) { + to = acc; + } + } + + if (from != null && to != null) { + if (from.balance >= amount && amount > 0) { + from.balance -= amount; + to.balance += amount; + System.out.println("Переведено " + amount + " с " + fromAccount + + " на " + toAccount); + } else { + System.out.println("Недостаточно средств или неверная сумма!"); + } + } else { + System.out.println("Один из счетов не найден!"); + } + } + + public String getAccountNumber() { + return accountNumber; + } + + public String getFio() { + return fio; + } + + public double getBalance() { + return balance; + } + + public static List getAccounts() { + return accountsList; + } +} + +class Fighter { + private String code; + private String name; + private int health; + private int attack; + + private static List fighters = new ArrayList<>(); + + public Fighter(String Code, String Name, int Health, int Attack) { + code = Code; + name = Name; + health = Health; + attack = Attack; + } + + public void fight(Fighter opponent) { + Random random = new Random(); + Fighter first, second; + + // Подкидываю монетку. + if (random.nextBoolean()) { + first = this; + second = opponent; + } else { + first = opponent; + second = this; + } + + System.out.println("\n=== ХАДЖИМЕ ==="); + System.out.println(first.getName() + " ломает колени оппоненту!"); + System.out.println(); + + int round = 1; + while (this.health > 0 && opponent.health > 0) { + System.out.println("Раунд " + round); + + if (first.health > 0) { + int damage = random.nextInt(first.attack) + 1; + second.health -= damage; + System.out.println("Поинто, " + first.getName() + " наносит " + damage + " единиц урона!"); + System.out.println(second.getName() + " HP: " + Math.max(0, second.health)); + + if (second.health <= 0) { + System.out.println("\nЦУЗУКЕ, ПОБЕДА ЗА" + first.getName()); + break; + } + } + + // Удар второго бойца + if (second.health > 0) { + int damage = random.nextInt(second.attack) + 1; + first.health -= damage; + System.out.println("Поинто, " + second.getName() + " наносит " + damage + " урона!"); + System.out.println(first.getName() + " HP: " + Math.max(0, first.health)); + + if (first.health <= 0) { + System.out.println("\nЦУЗУКЕ, ПОБЕДА ЗА" + second.getName()); + break; + } + } + + System.out.println(); + round++; + } + + System.out.println("=== ЙАМЕ ===\n"); + } + + public String getFighterInfo() { + return "Код: " + code + ", Имя: " + name + ", HP: " + health + + ", Атака: " + attack; + } + + public static List getFighters() { + return fighters; + } + + public static void addFighter(Fighter fighter) { + fighters.add(fighter); + System.out.println("Новый боец: " + fighter.getName()); + } + + public String getCode() { + return code; + } + + public String getName() { + return name; + } + + public int getHealth() { + return health; + } + + public int getAttack() { + return attack; + } +} + +public class Main { + public static void main(String[] args) { + // Тест классов и методов один за другим . + + // 1. Person + System.out.println("\n----------------------------------------------------------"); + Person person = new Person("Коил Кирвесгеин", 22); + person.introduce(); + + // 2. Rectangle + System.out.println("\n----------------------------------------------------------"); + Rectangle rect = new Rectangle(5, 3); + System.out.println("Площадь: " + rect.calculateSquare()); + System.out.println("Периметр: " + rect.calculatePerimeter()); + + // 3. Car + System.out.println("\n----------------------------------------------------------"); + Car car = new Car("Япония", "Suzuki Gemini", 2024); + car.printCarInfo(); + + // 4. BankAccount + System.out.println("\n----------------------------------------------------------"); + BankAccount account = new BankAccount("Дон Фон Пан Эклер", "7355608", 1000); + account.deposit(500); + account.withdraw(200); + System.out.println("Текущий баланс: " + account.getBalance()); + + // 5. Book + System.out.println("\n----------------------------------------------------------"); + Book book1 = new Book("8-800-555-35-35", "Волчица и Пряности", "Изуна Хасекура", 1869); + Book.addNewBook(book1); + System.out.println(book1.getBookInfo()); + book1.reserveBook(); + + // 6. OnlineStore + System.out.println("\n----------------------------------------------------------"); + OnlineStore product = new OnlineStore("Lenovo-Ideapad-3-001", "Ноутбук", 50000, 10); + OnlineStore.addProduct(product); + System.out.println(product.getProductInfo()); + product.buyProduct(2); + + // 7. BankSystem + System.out.println("\n----------------------------------------------------------"); + BankSystem acc1 = new BankSystem("880055", "Фрайда К.Ф.", 1700); + BankSystem acc2 = new BankSystem("535350", "Зилфатон К.Ф.", 1800); + BankSystem.addAccount(acc1); + BankSystem.addAccount(acc2); + BankSystem.transferMoneyBetweenAccounts("880055", "535350", 1000); + System.out.println(acc1.getAccountInfo()); + System.out.println(acc2.getAccountInfo()); + + // 8. StreetFighter + System.out.println("\n----------------------------------------------------------"); + Fighter amiya = new Fighter("5-Star", "Amiya", 100, 35); + Fighter lappland = new Fighter("6-Star", "Lappland", 100, 30); + Fighter.addFighter(amiya); + Fighter.addFighter(lappland); + amiya.fight(lappland); + } +} From 3b93f31c6ab89a1e5e16cee91ca5469c707a345c Mon Sep 17 00:00:00 2001 From: Matthew <80854685+Matthew-Mak@users.noreply.github.com> Date: Wed, 8 Oct 2025 16:21:16 +0500 Subject: [PATCH 2/2] Small fixes --- src/lessons/lesson03/Main.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lessons/lesson03/Main.java b/src/lessons/lesson03/Main.java index a3d3a99..9d75a2e 100644 --- a/src/lessons/lesson03/Main.java +++ b/src/lessons/lesson03/Main.java @@ -371,7 +371,7 @@ public void fight(Fighter opponent) { System.out.println(second.getName() + " HP: " + Math.max(0, second.health)); if (second.health <= 0) { - System.out.println("\nЦУЗУКЕ, ПОБЕДА ЗА" + first.getName()); + System.out.println("\nЦУЗУКЕ, ПОБЕДА ЗА " + first.getName()); break; } } @@ -384,7 +384,7 @@ public void fight(Fighter opponent) { System.out.println(first.getName() + " HP: " + Math.max(0, first.health)); if (first.health <= 0) { - System.out.println("\nЦУЗУКЕ, ПОБЕДА ЗА" + second.getName()); + System.out.println("\nЦУЗУКЕ, ПОБЕДА ЗА " + second.getName()); break; } } @@ -456,7 +456,7 @@ public static void main(String[] args) { // 5. Book System.out.println("\n----------------------------------------------------------"); - Book book1 = new Book("8-800-555-35-35", "Волчица и Пряности", "Изуна Хасекура", 1869); + Book book1 = new Book("8-800-555-35-35", "Волчица и Пряности", "Изуна Хасекура", 2006); Book.addNewBook(book1); System.out.println(book1.getBookInfo()); book1.reserveBook();