Джерело: Medium Сьогодні ми розробимо просте Java-додаток для банківської системи. Воно допоможе нам краще зрозуміти, як концепції ОВП використовують у програмах мовою Java. Для початку нам знадобиться середовище Java, встановлене на комп'ютері, бажано Java 11. Далі ми почнемо з детального опису функцій консольної програми. Функціонал:
- Створення облікового запису;
- Вхід вихід;
- Відображення останніх 5 транзакцій;
- Депозит коштів;
- Відображення поточної інформації про користувача.
- успадкування;
- поліморфізм;
- Інкапсуляція.
Розробка програми
Створимо новий Java-проект у Eclipse чи IntelliJ IDEA. Визначимо новий інтерфейс з ім'ям SavingsAccount .public interface SavingsAccount {
void deposit(double amount, Date date);
}
Я реалізував інтерфейс, в якому розміщено метод deposit . Я викликаю цей метод щоразу, коли додаю гроші на поточний рахунок. Концепція ОВП, що використовується тут, — поліморфізм (методи в інтерфейсі не мають тіла). Реалізацію цього методу можна знайти у класі Customer , перевизначивши метод із тим самим ім'ям та параметрами. Так ви перевизначаєте метод із батьківського інтерфейсу в дочірньому класі. Потім нам знадобиться клієнт (customer), щоб додати гроші на поточний рахунок. Але спочатку визначимо наш клас Customer .
public class Customer extends Person implements SavingsAccount {
private String username;
private String password;
private double balance;
private ArrayList<String> transactions = new ArrayList<>(5);
public Customer(String firstName, String lastName, String address, String phone, String username, String password, double balance, ArrayList<String> transactions, Date date) {
super(firstName, lastName, address, phone);
this.username = username;
this.password = password;
this.balance = balance;
addTransaction(String.format("Initial deposit - " + NumberFormat.getCurrencyInstance().format(balance) + " as on " + "%1$tD" + " at " + "%1$tT.", date));
}
private void addTransaction(String message) {
transactions.add(0, message);
if (transactions.size() > 5) {
transactions.remove(5);
transactions.trimToSize();
}
}
//Getter Setter
public ArrayList<String> getTransactions() {
return transactions;
}
@Override
public void deposit(double amount, Date date) {
balance += amount;
addTransaction(String.format(NumberFormat.getCurrencyInstance().format(amount) + " credited to your account. Balance - " + NumberFormat.getCurrencyInstance().format(balance) + " as on " + "%1$tD" + " at " + "%1$tT.", date));
}
@Override
public String toString() {
return "Customer{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", balance=" + balance +
", transactions=" + transactions +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
if (Double.compare(customer.getBalance(), getBalance()) != 0) return false;
if (getUsername() != null ? !getUsername().equals(customer.getUsername()) : customer.getUsername() != null)
return false;
if (getPassword() != null ? !getPassword().equals(customer.getPassword()) : customer.getPassword() != null)
return false;
return getTransactions() != null ? getTransactions().equals(customer.getTransactions()) : customer.getTransactions() == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = getUsername() != null ? getUsername().hashCode() : 0;
result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
temp = Double.doubleToLongBits(getBalance());
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (getTransactions() != null ? getTransactions().hashCode() : 0);
return result;
}
Концепція ООП, що використовується тут, — успадкування , оскільки клас Customer отримує властивості від класу Person . Тобто практично всі атрибути класу Person успадковуються і видно відносини за батьківсько-дочірнім принципом від Person до Customer . Зараз нам потрібний конструктор з усіма атрибутами двох класів та додавання ключового слова суперконструктора для вказівки успадкованих атрибутів. Реалізуючи інтерфейс SavingsAccount , ми повинні перевизначити метод deposit у класі Customer . Для цього ми напишемо реалізацію методу у цьому класі. Крім того, список транзакцій ініціалізується для відображення останніх п'яти транзакцій. У конструкторі викликається метод add Transaction , в якому відображається дата змін та скоєних транзакцій.
private void addTransaction(String message) {
transactions.add(0, message);
if (transactions.size() > 5) {
transactions.remove(5);
transactions.trimToSize();
}
}
Тепер поговоримо про батьківський клас Person .
public class Person {
private String firstName;
private String lastName;
private String address;
private String phone;
public Person() {}
public Person(String firstName, String lastName, String address, String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
}
//Getters Setters
@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (getFirstName() != null ? !getFirstName().equals(person.getFirstName()) : person.getFirstName() != null)
return false;
if (getLastName() != null ? !getLastName().equals(person.getLastName()) : person.getLastName() != null)
return false;
if (getAddress() != null ? !getAddress().equals(person.getAddress()) : person.getAddress() != null)
return false;
return getPhone() != null ? getPhone().equals(person.getPhone()) : person.getPhone() == null;
}
@Override
public int hashCode() {
int result = getFirstName() != null ? getFirstName().hashCode() : 0;
result = 31 * result + (getLastName() != null ? getLastName().hashCode() : 0);
result = 31 * result + (getAddress() != null ? getAddress().hashCode() : 0);
result = 31 * result + (getPhone() != null ? getPhone().hashCode() : 0);
return result;
}
У класі Person ми використовували концепцію інкапсуляції , застосовуючи модифікатор private доступу для кожного атрибуту. Інкапсуляція Java може бути визначена як механізм, за допомогою якого методи, що працюють з цими даними, об'єднуються в єдине ціле. По суті, дані з класу Person доступні тільки в цьому класі, але не в інших класах або пакетах. І нарешті, наш основний клас, названий Bank . Це основний клас, звідки ми запускаємо програму та взаємодіємо з функціональністю всіх класів.
public class Bank {
private static double amount = 0;
Map<String, Customer> customerMap;
Bank() {
customerMap = new HashMap<String, Customer>();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Customer customer;
Bank bank = new Bank();
int choice;
outer:
while (true) {
System.out.println("\n-------------------");
System.out.println("BANK OF JAVA");
System.out.println("-------------------\n");
System.out.println("1. Registrar cont.");
System.out.println("2. Login.");
System.out.println("3. Exit.");
System.out.print("\nEnter your choice : ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter First Name : ");
String firstName = sc.nextLine();
System.out.print("Enter Last Name : ");
String lastName = sc.nextLine();
System.out.print("Enter Address : ");
String address = sc.nextLine();
System.out.print("Enter contact number : ");
String phone = sc.nextLine();
System.out.println("Set Username : ");
String username = sc.next();
while (bank.customerMap.containsKey(username)) {
System.out.println("Username already exists. Set again : ");
username = sc.next();
}
System.out.println("Set a password:");
String password = sc.next();
sc.nextLine();
customer = new Customer(firstName, lastName, address, phone, username, password, new Date());
bank.customerMap.put(username, customer);
break;
case 2:
System.out.println("Enter username : ");
username = sc.next();
sc.nextLine();
System.out.println("Enter password : ");
password = sc.next();
sc.nextLine();
if (bank.customerMap.containsKey(username)) {
customer = bank.customerMap.get(username);
if (customer.getPassword().equals(password)) {
while (true) {
System.out.println("\n-------------------");
System.out.println("W E L C O M E");
System.out.println("-------------------\n");
System.out.println("1. Deposit.");
System.out.println("2. Transfer.");
System.out.println("3. Last 5 transactions.");
System.out.println("4. User information.");
System.out.println("5. Log out.");
System.out.print("\nEnter your choice : ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter amount : ");
while (!sc.hasNextDouble()) {
System.out.println("Invalid amount. Enter again :");
sc.nextLine();
}
amount = sc.nextDouble();
sc.nextLine();
customer.deposit(amount, new Date());
break;
case 2:
System.out.print("Enter beneficiary username : ");
username = sc.next();
sc.nextLine();
System.out.println("Enter amount : ");
while (!sc.hasNextDouble()) {
System.out.println("Invalid amount. Enter again :");
sc.nextLine();
}
amount = sc.nextDouble();
sc.nextLine();
if (amount > 300) {
System.out.println("Transfer limit exceeded. Contact bank manager.");
break;
}
if (bank.customerMap.containsKey(username)) {
Customer payee = bank.customerMap.get(username); //Todo: check
payee.deposit(amount, new Date());
customer.withdraw(amount, new Date());
} else {
System.out.println("Username doesn't exist.");
}
break;
case 3:
for (String transactions : customer.getTransactions()) {
System.out.println(transactions);
}
break;
case 4:
System.out.println("Titularul de cont cu numele: " + customer.getFirstName());
System.out.println("Titularul de cont cu prenumele : " + customer.getLastName());
System.out.println("Titularul de cont cu numele de utilizator : " + customer.getUsername());
System.out.println("Titularul de cont cu addresa : " + customer.getAddress());
System.out.println("Titularul de cont cu numarul de telefon : " + customer.getPhone());
break;
case 5:
continue outer;
default:
System.out.println("Wrong choice !");
}
}
} else {
System.out.println("Wrong username/password.");
}
} else {
System.out.println("Wrong username/password.");
}
break;
case 3:
System.out.println("\nThank you for choosing Bank Of Java.");
System.exit(1);
break;
default:
System.out.println("Wrong choice !");
}}}}
Використовуючи бібліотеку java.util , ми викликаємо Scanner для читання даних із клавіатури. Наводячи об'єкт Customer через його визначення, відоме як Bank , ми створюємо новий об'єкт типу Bank() . Через деякий час виводимо стартове меню. При використанні nextLine зчитується число, додане з клавіатури. Нижче ми маємо новий конструктор, який зберігає нашу map , дані клієнта. Map.put використовується для збереження чи оновлення даних клієнтів. customer = новий Customer(firstName, lastName, address, phone, username, password, new Date());
bank.customerMap.put(username, customer);
За наявності з'єднання ми отримуємо нове меню з опціями. Той самий підхід працює з використанням while та switch для виклику функціоналу програми. Етап 1: Додаємо гроші на поточний рахунок. Етап 2: Відображаємо останні 5 транзакцій. Етап 3: Виводимо дані клієнта з картки у консоль. Етап 4: Закриваємо меню. Вихідний код програми можна знайти тут . Сподіваюся, що цей приклад допоможе вам краще познайомитися з використанням концепцій ООП Java.
ПЕРЕЙДІТЬ В ПОВНУ ВЕРСІЮ