Fonte: Medium Oggi svilupperemo una semplice applicazione Java per un sistema bancario. Ci aiuterà a capire meglio come vengono utilizzati i concetti OOP nei programmi Java. Per cominciare, avremo bisogno di un ambiente Java installato sul computer, preferibilmente Java 11. Successivamente, inizieremo con una descrizione dettagliata delle funzioni dell'applicazione console. Funzionale:
- Creare un account;
- Entra esci;
- Visualizzazione delle ultime 5 transazioni;
- Deposito contanti;
- Visualizza le informazioni dell'utente corrente.
- Eredità;
- Polimorfismo;
- Incapsulamento.
Sviluppo di applicazioni
Creiamo un nuovo progetto Java in Eclipse o IntelliJ IDEA. Definiamo una nuova interfaccia chiamata SavingsAccount .public interface SavingsAccount {
void deposit(double amount, Date date);
}
Ho implementato un'interfaccia che ospita il metodo di deposito . Chiamo questo metodo ogni volta che aggiungo denaro al mio conto corrente. Il concetto OOP qui utilizzato è il polimorfismo (i metodi in un'interfaccia non hanno un corpo). L'implementazione di questo metodo può essere trovata nella classe Customer sovrascrivendo un metodo con lo stesso nome e parametri. In questo modo sovrascrivi un metodo dall'interfaccia genitore nella classe figlia. Quindi abbiamo bisogno che un cliente aggiunga denaro al conto corrente. Ma prima definiamo la nostra classe 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;
}
Il concetto OOP utilizzato qui è l'ereditarietà , poiché la classe Customer riceve proprietà dalla classe Person . Cioè, quasi tutti gli attributi della classe Person vengono ereditati e le relazioni genitore-figlio sono visibili da Person a Customer . Ora abbiamo bisogno di un costruttore con tutti gli attributi delle due classi e dell'aggiunta di una parola chiave supercostruttore per specificare gli attributi ereditati. Quando implementiamo l' interfaccia SavingsAccount , dobbiamo sovrascrivere il metodo di deposito nella classe Customer . Per fare ciò, scriveremo un'implementazione del metodo in questa classe. Inoltre, l'elenco delle transazioni viene inizializzato per visualizzare le ultime cinque transazioni. Il costruttore chiama il metodo addTransaction , che visualizza la data delle modifiche e delle transazioni completate.
private void addTransaction(String message) {
transactions.add(0, message);
if (transactions.size() > 5) {
transactions.remove(5);
transactions.trimToSize();
}
}
Ora parliamo della classe genitore 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;
}
Nella classe Person abbiamo utilizzato il concetto di incapsulamento applicando il modificatore di accesso privato a ciascun attributo. L'incapsulamento in Java può essere definito come il meccanismo mediante il quale i metodi che operano su questi dati vengono combinati in una singola unità. In sostanza, i dati della classe Person sono disponibili solo in questa classe e non in altre classi o pacchetti. E infine, la nostra classe principale, chiamata Bank . Questa è la classe principale da cui eseguiamo l'applicazione e interagiamo con le funzionalità di tutte le classi.
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 !");
}}}}
Utilizzando la libreria java.util , chiamiamo Scanner per leggere la tastiera. Trascinando l' oggetto Customer attraverso la sua definizione, nota come Bank , creiamo un nuovo oggetto di tipo Bank() . Dopo qualche tempo, visualizziamo il menu di avvio. Quando si utilizza nextLine , viene letto il numero aggiunto dalla tastiera. Di seguito abbiamo un nuovo costruttore che salva la nostra mappa , i dati del client. Map.put viene utilizzato per salvare o aggiornare i dati dei clienti. cliente = nuovo Cliente(nome, cognome, indirizzo, telefono, nome utente, password, nuova data());
bank.customerMap.put(username, customer);
Se c'è una connessione, otteniamo un nuovo menu con le opzioni. Lo stesso approccio funziona utilizzando while e switch per chiamare la funzionalità dell'applicazione. Fase 1: aggiungi denaro al tuo conto corrente. Passaggio 2: visualizza le ultime 5 transazioni. Fase 3: trasmettiamo i dati del cliente dalla carta alla console. Fase 4: chiudere il menu. Il codice sorgente del programma può essere trovato qui . Spero che questo esempio ti aiuti a acquisire maggiore familiarità con l'uso dei concetti OOP in Java.
GO TO FULL VERSION