Skip to content
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
50 changes: 50 additions & 0 deletions lessons/src/main/java/lessons/lesson03/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Lesson3;

public class App {
public static void main(String[] args) {
/*Person person = new Person("Shohjahon", 30);
person.intro();*/

/*Rectangle rectangle = new Rectangle(4, 5);
System.out.println("Area: " + rectangle.calcSquare());
System.out.println("Perimeter: " + rectangle.calcPerimeter());*/

/*Car car = new Car("Japan", "Toyota", 2025);
car.print();*/

/*BankAcc bankAcc = new BankAcc("Shohjahon", "123456789", 100);
bankAcc.deposit(150);
bankAcc.withdraw(50);
bankAcc.print();*/

/*Book book = new Book("123456789", "Awareness", "Osho", 2000, true);
book.addNewBook(book);
book.getBookInfo();
book.reserveBook();
book.getBooks();*/

/*OnlineStore product = new OnlineStore("001", "Phone", 600, 10);
product.addProduct(product);
product.getProducts();
product.buyProduct(1);
product.getProducts();*/

/*BankSystem acc1 = new BankSystem("001", "Shohjahon", 100);
BankSystem acc2 = new BankSystem("002", "Ali", 150);
BankSystem.addAcc(acc1);
BankSystem.addAcc(acc2);
BankSystem.showAllAcc();
acc1.replenishAcc(100);
BankSystem.transferBetweenAcc("002", "001", 50);
BankSystem.showAllAcc();
BankSystem.deleteAcc("002");
BankSystem.showAllAcc();*/

/*Fighter ali = new Fighter("F1", "Ali", 100, 20);
Fighter bob = new Fighter("F2", "Bob", 100, 20);
Fighter.addFighter(ali);
Fighter.addFighter(bob);
Fighter.getFighters();
Fighter.fight(ali, bob);*/
}
}
39 changes: 39 additions & 0 deletions lessons/src/main/java/lessons/lesson03/BankAcc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Lesson3;

public class BankAcc {
private String FIO;
private String accNumber;
private int balance;

public BankAcc(String FIO, String accNumber, int balance) {
this.FIO = FIO;
this.accNumber = accNumber;
this.balance = balance;
}

public void deposit(double amount) {
if (amount > 0) {
balance += (int) amount;
System.out.printf("Deposit: %s. New balance: %s.", amount, balance);
} else {
System.out.println("Deposit amount must be positive.");
}
}

public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= (int) amount;
System.out.printf("Withdrew: %s. New balance: %s.", amount, balance);
} else {
System.out.println("Invalid withdrawal amount or insufficient funds.");
}
}

public int getBalance() {
return balance;
}

public void print() {
System.out.printf("%s, %s, %s.", FIO, accNumber, balance);
}
}
84 changes: 84 additions & 0 deletions lessons/src/main/java/lessons/lesson03/BankSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package Lesson3;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BankSystem {
private String accNumber;
private String fullName;
private double balance;

private static List<BankSystem> accounts = new ArrayList<>();

public BankSystem(String accNumber, String fullName, double balance) {
this.accNumber = accNumber;
this.fullName = fullName;
this.balance = balance;
}

public void getInfo() {
System.out.printf("%s, %s, %s.%n", accNumber, fullName, balance);
}

public static void addAcc(BankSystem bankAcc) {
accounts.add(bankAcc);
System.out.printf("%s added.%n", bankAcc.fullName);
}

public static void deleteAcc(String accNumber) {
BankSystem accToRemove = findAcc(accNumber);
if (accToRemove != null) {
accounts.remove(accToRemove);
System.out.printf("%s deleted.%n", accNumber);
} else {
System.out.println("Account not found.%n");
}
}

private static BankSystem findAcc(String accNumber) {
for (BankSystem acc : accounts) {
if (acc.accNumber.equals(accNumber)) {
return acc;
}
}
return null;
}

public void replenishAcc(double amount) {
if (amount > 0) {
balance += amount;
System.out.printf("Replenish: %s. New balance: %s.%n", amount, balance);
} else {
System.out.println("Amount must be positive.%n");
}
}

public static void showAllAcc() {
if (accounts.isEmpty()) {
System.out.println("No account.%n");
} else {
for (BankSystem acc : accounts) {
acc.getInfo();
}
}
}

public static void transferBetweenAcc(String fromAcc, String toAcc, double amount) {
BankSystem sender = findAcc(fromAcc);
BankSystem receiver = findAcc(toAcc);

if (sender == null || receiver == null) {
System.out.println("Not found.%n");
}

if (amount >= 0 && Objects.requireNonNull(sender).balance >= amount) {
sender.balance -= amount;
assert receiver != null;
receiver.balance += amount;
System.out.printf("Transfer from %s to %s.%n", sender.fullName, receiver.fullName);
} else {
System.out.println("Fail.");
}
}
}
51 changes: 51 additions & 0 deletions lessons/src/main/java/lessons/lesson03/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Lesson3;

import java.util.ArrayList;
import java.util.List;

public class Book {
private String isbn;
private String name;
private String author;
private int year;
private boolean status;

private static List<Book> books = new ArrayList<>();

public Book(String isbn, String name, String author, int year, boolean status) {
this.isbn = isbn;
this.name = name;
this.author = author;
this.year = year;
this.status = status;
}

public void getBookInfo() {
System.out.printf("%s, %s, %s, %s.", isbn, name, author, year);
System.out.println(status ? "Available" : "Not Available");
}

public void getBooks() {
if (books.isEmpty()) {
System.out.println("No books.");
} else {
for (Book book : books) {
book.getBookInfo();
}
}
}

public void addNewBook(Book newBook) {
books.add(newBook);
System.out.printf("Added: %s.", newBook.name);
}

public void reserveBook() {
if (status) {
status = false;
System.out.printf("%s reserved.", name);
} else {
System.out.printf("%s is already reserved.", name);
}
}
}
17 changes: 17 additions & 0 deletions lessons/src/main/java/lessons/lesson03/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Lesson3;

public class Car {
private String country;
private String model;
private int yearOfManufacture;

public Car(String country, String model, int yearOfManufacture) {
this.country = country;
this.model = model;
this.yearOfManufacture = yearOfManufacture;
}

public void print() {
System.out.printf("%s, %s, %s.", country, model, yearOfManufacture);
}
}
66 changes: 66 additions & 0 deletions lessons/src/main/java/lessons/lesson03/Fighter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package Lesson3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Fighter {
private String code;
private String name;
private int health;
private int attack;

private static List<Fighter> fighters = new ArrayList<>();

public Fighter(String code, String name, int health, int attack) {
this.code = code;
this.name = name;
this.health = health;
this.attack = attack;
}

public void getInfo() {
System.out.printf("%s, %s, %s, %s.%n", code, name, health, attack);
}

public static void getFighters() {
if (fighters.isEmpty()) {
System.out.println("No fighters.%n");
} else {
for (Fighter fighter : fighters) {
fighter.getInfo();
}
}
}

public static void addFighter(Fighter fighter) {
fighters.add(fighter);
System.out.printf("%s added.%n", fighter.name);
}

public static void fight(Fighter fighter1, Fighter fighter2) {
Random random = new Random();

System.out.printf("Fight: %s vs %s.%n", fighter1, fighter2);

Fighter attacker = random.nextBoolean() ? fighter1 : fighter2;
Fighter defender = (attacker == fighter1) ? fighter2 : fighter1;

while (fighter1.health > 0 && fighter2.health > 0) {
int damage = attacker.attack;
defender.health -= damage;

System.out.printf("%s attacks %s for %s damage! %s health %s.%n",
attacker.name, defender.name, damage, defender.name, defender.health);

Fighter temp = attacker;
attacker = defender;
defender = temp;
}
}

@Override
public String toString() {
return name;
}
}
49 changes: 49 additions & 0 deletions lessons/src/main/java/lessons/lesson03/OnlineStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package Lesson3;

import java.util.ArrayList;
import java.util.List;

public class OnlineStore {
private String code;
private String name;
private double price;
private int count;

private static List<OnlineStore> storeList = 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 void addProduct(OnlineStore product) {
storeList.add(product);
System.out.printf("%s added.%n", product.name);
}

public void buyProduct(int quantity) {
if (quantity > 0 && quantity <= count) {
count -= quantity;
System.out.printf("Bought %d of %s. Remaining: %s.%n", quantity, name, count);
} else {
System.out.println("Not enough.");
}
}

public void getInfo() {
System.out.printf("%s, %s, %s, %s.%n", code, name, price, count);
}

public void getProducts() {
if (storeList.isEmpty()) {
System.out.println("No products.");
} else {
for (OnlineStore product : storeList) {
product.getInfo();
}
}
}
}

31 changes: 31 additions & 0 deletions lessons/src/main/java/lessons/lesson03/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Lesson3;

public class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public void intro() {
System.out.printf("I am %s, %s yo.", name, age);
}
}
Loading