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
38 changes: 38 additions & 0 deletions src/main/java/lessons/lesson03/bankSystem/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lessons.lesson03.bankSystem;

public class Account {
private int accountNumber;
private String fio;
private double balance;

public String getFio() {
return fio;
}

public Account(int accountNumber, String fio, double balance) {
this.accountNumber = accountNumber;
this.fio = fio;
this.balance = balance;
}

public String getAccountInfo() {
return "AccountNumber: " + accountNumber + "\nFIO: " + fio + "\nBalance: " + balance + "\n";
}

public int getAccountNumber() {
return accountNumber;
}

public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}

public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}
}

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

import java.util.ArrayList;

public class BankSystem {
private ArrayList<Account> accounts = new ArrayList<>();

public void addAccount(Account account) {
accounts.add(account);
}

public void deleteAccount(int accountNumber) {
accounts.removeIf(account -> accountNumber == account.getAccountNumber());
}

public void replenishAccount(int accountNumber, double amount) {
if (amount < 0) {
System.out.println("You cannot send negative sum!");
return;
}
for (Account account : accounts) {
if (accountNumber == account.getAccountNumber()) {
account.setBalance(account.getBalance() + amount);
System.out.println(account.getFio() + " balance has been replenished: " + amount);
System.out.println("Current balance: " + account.getBalance() + "\n");
}
}
}

public void transferMoneyBetweenAccounts(int from, int to, double amount) {
Account sender = null;
Account receiver = null;
for (Account account : accounts) {
if (account.getAccountNumber() == from) sender = account;
if (account.getAccountNumber() == to) receiver = account;
}
if (sender == null || receiver == null) {
System.out.println("One of the accounts was not found!");
return;
}
if (sender.getBalance() < amount) {
System.out.println("Not enough money for transfer!");
return;
}
sender.setBalance(sender.getBalance() - amount);
receiver.setBalance(receiver.getBalance() + amount);
System.out.println("Transfer successful! " + amount + " from " + from + " to " + to + "\n");
}

public void getAccounts() {
if (accounts.isEmpty()) {
System.out.println("No accounts found!");
return;
}
for (Account account : accounts) {
System.out.println(account.getAccountInfo());
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/lessons/lesson03/bankSystem/MainBankSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lessons.lesson03.bankSystem;

public class MainBankSystem {
public static void main(String[] args) {
Account accountNew = new Account(1234, "Aleksey Kivovarov", 9022212.5);
Account accountNew1 = new Account(1235, "Aleksey Kavalniy", 123);
BankSystem bankSystem = new BankSystem();
bankSystem.addAccount(accountNew);
bankSystem.getAccounts();
System.out.println();

bankSystem.transferMoneyBetweenAccounts(1234, 1235, 877);
System.out.println();

bankSystem.replenishAccount(1234, -456);
System.out.println();

bankSystem.replenishAccount(1234, 0.5);
System.out.println();

bankSystem.addAccount(accountNew1);
bankSystem.transferMoneyBetweenAccounts(1234, 1235, 9022212);
bankSystem.getAccounts();
System.out.println();

bankSystem.deleteAccount(1235);
bankSystem.deleteAccount(1234);
bankSystem.getAccounts();
}
}
30 changes: 30 additions & 0 deletions src/main/java/lessons/lesson03/basicClasses/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lessons.lesson03.basicClasses;

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

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

public void getBookInfo() {
System.out.println("Information about " + title +
"\nISBN: " + isbn +
"\nTitle: " + title +
"\nAuthor: " + author +
"\nYear: " + year +
"\nStatus: " + status);
}

public void reserveBook() {
status = false;
}
}
20 changes: 20 additions & 0 deletions src/main/java/lessons/lesson03/basicClasses/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lessons.lesson03.basicClasses;

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

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

public void printInfo() {
System.out.println("Information about car:" +
"\nThe country of a car is " + country +
"\nThe model is " + model +
"\nThe year of made is " + yearOfManufacture);
}
}
17 changes: 17 additions & 0 deletions src/main/java/lessons/lesson03/basicClasses/Library.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package lessons.lesson03.basicClasses;

import java.util.ArrayList;

public class Library {
private ArrayList<Book> books = new ArrayList<>();

public void addNewBook(Book book) {
books.add(book);
}

public void getBooks() {
for (Book book : books) {
book.getBookInfo();
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/lessons/lesson03/basicClasses/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package lessons.lesson03.basicClasses;

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

public Person(String name, int age) {
this.age = age;
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void introduce() {
System.out.println("Hello! My name is " + name + " and I am " + age + " years old.");
}
}
23 changes: 23 additions & 0 deletions src/main/java/lessons/lesson03/basicClasses/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lessons.lesson03.basicClasses;

public class Rectangle {
private final int length;
private final int width;

public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}

public void calculateSquare() {
int square = width * length;
System.out.println("Square of rectangle is: " + square);
}

public void calculatePerimeter() {
double perimeter = (width + length) / 2.0;
System.out.println("Perimeter of rectangle is: " + perimeter);
}


}
35 changes: 35 additions & 0 deletions src/main/java/lessons/lesson03/onlineStore/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lessons.lesson03.onlineStore;

public class Main {
public static void main(String[] args) {
Product product = new Product(1234,"SAMSUNG A12", 100, 10);
Product product1 = new Product(1234,"SAMSUNG A12", 100, 10);
Product product2 = new Product(1233,"SAMSUNG A11", 80, 30);
Product product3 = new Product(1234,"SAMSUNG A12", 100, 5);


OnlineStore onlineStore = new OnlineStore();
onlineStore.addProduct(product);

onlineStore.addProduct(product1);

// onlineStore.addProduct(product2);

onlineStore.buyProduct(1234, 29);
// onlineStore.getProducts(1233);
// onlineStore.buyProduct(123, 31);
// onlineStore.getProducts(1);
// onlineStore.getProducts();
// onlineStore.getProducts(1233);
// onlineStore.buyProduct(1233, 31);
// onlineStore.buyProduct(1233, 2);
// onlineStore.getProducts();
onlineStore.addProduct(product3);
onlineStore.getProducts();
product3.setPrice(95.0);
onlineStore.getProducts(1234);
onlineStore.addProduct(product3);
onlineStore.getProducts();

}
}
75 changes: 75 additions & 0 deletions src/main/java/lessons/lesson03/onlineStore/OnlineStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package lessons.lesson03.onlineStore;

import java.util.ArrayList;
import java.util.Scanner;

public class OnlineStore {

private ArrayList<Product> products = new ArrayList<>();

public void addProduct(Product product) {
boolean found = false;
for (Product product1 : products) {
if (product1.getCode() == product.getCode()) {
product1.setCount(product1.getCount() + product.getCount());
product1.setPrice(product.getPrice());
found = true;
break;
}
}
if (!found) {
products.add(product);
}
}

public void buyProduct(int code, int count) {
for (Product product : products) {
if (code != product.getCode()) {
continue;
}
if (product.getCount() == 0) {
System.out.println("Product is not available!");
return;
}
if (product.getCount() < count) {
System.out.print("Just " + product.getCount() + " products are available!" +
"\nDo you want buy all of them - " + product.getCount() + "? Yes or No - ");
Scanner sc = new Scanner(System.in);
String yesOrNo = sc.next();
if (yesOrNo.equals("Yes")) {
product.setCount(0);
System.out.println("You bought all of them!\n");
} else {
System.out.println("Purchase cancelled.\n");
}
return;
}
product.setCount(product.getCount() - count);
System.out.println("You bought " + count + " products");
return;
}
System.out.println("The code of product is incorrect. Try again!\n");
}

public void getProducts(int code) {
boolean found = false;
for (Product product : products) {
if (product.getCode() == code) {
System.out.println(product.getProductInfo());
found = true;
}
}
if (!found) {
System.out.println("There is no such Product! Try again.\n");
}
}

public void getProducts() {
System.out.println("Information about all of Products in List");
for (Product product : products) {
System.out.println(product.getProductInfo());
}
System.out.println("Thats it!\n");
}

}
38 changes: 38 additions & 0 deletions src/main/java/lessons/lesson03/onlineStore/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lessons.lesson03.onlineStore;

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

public Product(int code, String name, double price, int count) {
this.code = code;
this.name = name;
this.price = price;
this.count = count;
}
public String getProductInfo() {
return "Code: " + code + "\nName: " + name + "\nPrice: " + price + "\nCount: " + count + "\n";
}

public int getCode() {
return code;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}
Loading