Bank Customer Management Project in Java

Kategori: Java , 28 Temmuz 2019 , JanFranco


C bölümünde paylaştığım projenin Java'ya uyarlanmış halidir. Önceki yazıda belirtmeyi unuttum, müşterilerin bilgileri bir dosyaya yazılıyor. Okuma, yazma işlemleri bir dosyaya yapılıyor. Veritabanı da kullanılabilirdi tabi.

Hesap açabiliyoruz, bir hesabı güncelleyebiliyoruz, bir hesaptan başka hesaba havale yapabiliyoruz, bir hesabın bilgilerini kontrol edebiliyoruz, bir hesabı silebiliyoruz, hesaplar listesine bakabiliyoruz. Dediğim gibi fonksiyonlar oldukça kısıtlı fakat siz üstüne koyarak geliştirebilirsiniz.

Account.java:


import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Account implements Serializable {

	private static final long serialVersionUID = -9180809925470496634L;
	private int id;
	private String name, surname, address, phone, createdTime, lastUpdateTime;
	private double balance;
	transient private DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
	transient private LocalDate localDate = LocalDate.now();
	
	public Account(int id, String name, String surname, String address, String phone, double balance) {
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.address = address;
		this.phone = phone;
		this.balance = balance;
		this.createdTime = dtf.format(localDate);
		this.lastUpdateTime = dtf.format(localDate);
	}
	
	public void setId(int id) {
		this.id = id;
	}

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

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setCreatedTime(String createdTime) {
		this.createdTime = createdTime;
	}

	public void setLastUpdateTime(String lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

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

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}
	
	public double getBalance() {
		return balance;
	}
	
	public void transaction(Account acc, double balance) {
		if(balance > this.balance) {
			System.out.println("\tInsufficient balance.");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				System.out.println("Interruption.");
			}
		} else {
			this.balance -= balance;
			acc.setBalance(acc.getBalance() + balance);
			System.out.println("\tMoney successfully has sent.");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				System.out.println("Interruption.");
			}
		}
	}

	@Override
	public String toString() {
		
		return "CUSTOMER ID: " + id + "\nCUSTOMER NAME: " + name + "\nCUSTOMER SURNAME: " + surname + "\nCUSTOMER ADDRESS: " + 
		address + "\nCUSTOMER PHONE: " + phone + "\nCUSTOMER BALANCE: " + balance + "\nCUSTOMER CREATED DAY: " + createdTime +
		"\nCUSTOMER LAST OPERATION DAY: " + lastUpdateTime;
	
	}
	
}
Main.java:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	
	private static ArrayList<Account> accountList = new ArrayList<>();
	
	public static int menu() {
		
		while(true) {
		
			System.out.println("\t\t\tCUSTOMER ACCOUNT BANKING MANAGEMENT SYSTEM\n");
			System.out.println("\t\t\t\tWELCOME TO THE MAIN MENU\n\n");
			System.out.println("1. Create New Account");
			System.out.println("2. Update an Account");
			System.out.println("3. Transactions");
			System.out.println("4. Check Details of an Account");
			System.out.println("5. Remove an Account");
			System.out.println("6. Customer's List");
			System.out.println("7. Exit\n\n");
			System.out.print("Enter your choice: ");
			
			int choice;
			Scanner scanner = new Scanner(System.in);
			choice = scanner.nextInt();
			if(choice <= 7 && choice >= 1) {
				return choice;
			} else {
				System.out.println("\n\tEnter a valid number.");
			}
			
		}

	}
	
	public static Account create_account() {
		int id;
		String name, surname, address, phone;
		double balance;
		Scanner scanner = new Scanner(System.in);
		System.out.print("\tEnter Customer ID: ");
		id = scanner.nextInt();
		System.out.print("\tEnter Customer Name - Surname: ");
		scanner.nextLine();
		String[] nameSurname = scanner.nextLine().split(" ", 2);
		name = nameSurname[0];
		surname = nameSurname[1];
		System.out.print("\tEnter Customer Address: ");
		address = scanner.nextLine();
		System.out.print("\tEnter Customer Phone: ");
		phone = scanner.nextLine();
		System.out.print("\tEnter Customer Balance: ");
		balance = scanner.nextDouble();
		
		Account account = new Account(id, name, surname, address, phone, balance);
		
		return account;
	}
	
	public static void show_accounts() {
		
		for(Account account : accountList) {
			System.out.println(account);
			System.out.println("\n");
		}
		
	}
	
	public static void check_details() {
		
		int searchID;
		String searchName;
		Scanner scanner = new Scanner(System.in);
		System.out.print("Search by ID: ");
		searchID = scanner.nextInt();
		scanner.nextLine();
		System.out.print("Search by Name: ");
		searchName = scanner.nextLine();
		
		for(Account account : accountList) {
			if(searchID == account.getId() || searchName == account.getName()) {
				System.out.println(account);
			}
		}
		
	}
	
	public static void update_account() {
		
		int searchID;
		Scanner scanner = new Scanner(System.in);
		System.out.print("Search by ID: ");
		searchID = scanner.nextInt();
		
		for(Account account : accountList) {
			if(searchID == account.getId()) {
				String name, surname, address, phone;
				System.out.print("\tUpdate Customer Name - Surname: ");
				scanner.nextLine();
				String[] nameSurname = scanner.nextLine().split(" ", 2);
				name = nameSurname[0];
				surname = nameSurname[1];
				System.out.print("\tEnter Customer Address: ");
				address = scanner.nextLine();
				System.out.print("\tEnter Customer Phone: ");
				phone = scanner.nextLine();
				account.setName(name);
				account.setSurname(surname);
				account.setAddress(address);
				account.setPhone(phone);
				DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
				LocalDate localDate = LocalDate.now();
				account.setLastUpdateTime(dtf.format(localDate));
			}
		}
		
	}
	
	public static void delete_account() {
		
		int searchID;
		Scanner scanner = new Scanner(System.in);
		System.out.print("Search by ID: ");
		searchID = scanner.nextInt();
		
		for(Account account : accountList) {
			if(searchID == account.getId()) {
				accountList.remove(account);
			}
		}
		
	}
	
	public static void transaction() {
		
		int searchID;
		double money;
		Scanner scanner = new Scanner(System.in);
		System.out.print("Search by ID: ");
		searchID = scanner.nextInt();
		
		for(Account account : accountList) {
			if(searchID == account.getId()) {
				System.out.print("Money to account that has ID: ");
				searchID = scanner.nextInt();
				for(Account account2: accountList) {
					if(searchID == account2.getId()) {
						System.out.print("How much money?: ");
						money = scanner.nextDouble();
						account.transaction(account2, money);
					}
				}
			}
		}
		
	}
	
	public static void save_list() {
		
		try(ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("acc.bin"))){
			
			output.writeObject(accountList);
			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException");
		} catch (IOException e) {
			System.out.println("IOException");
		}
		
	}
	
	public static void load_list() {
		
		try(ObjectInputStream input = new ObjectInputStream(new FileInputStream("acc.bin"))){
			
			accountList = (ArrayList<Account>)input.readObject();
			
		} catch (FileNotFoundException e) {
			System.out.println("FileNotFoundException");
		} catch (IOException e) {
			System.out.println("IOException");
		} catch (ClassNotFoundException e) {
			System.out.println("ClasNotFoundException");
		}
		
	}

	public static void main(String args[]) {
		
		int choice;
		load_list();
		Scanner scanner = new Scanner(System.in);
		
		while(true) {
			for(int i=0; i<10; i++) {
				System.out.println("\n");
			}
			choice = menu();
			
			if(choice == 1) {
				Account newAccount = create_account();
				accountList.add(newAccount);
				System.out.println("\tAccount successfully created.");
				save_list();
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					System.out.println("Interruption.");
				}
			} else if (choice == 2) {
				update_account();
				System.out.println("\tAccount successfully updated.");
				save_list();
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					System.out.println("Interruption.");
				}
			} else if (choice == 3) {
				transaction();
				save_list();
			} else if (choice == 4) {
				check_details();
				System.out.println("Press enter for to return the main menu.");
				scanner.nextLine();
			} else if (choice == 5) {
				delete_account();
				System.out.println("\tAccount successfully deleted.");
				save_list();
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					System.out.println("Interruption.");
				}
			} else if (choice == 6) {
				show_accounts();
				System.out.println("Press enter for to return the main menu.");
				scanner.nextLine();
			} else if (choice == 7) {
				save_list();
				System.out.println("Have a nice day!");
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				break;
			}
			
		}
		
	}
	
}
Github: https://github.com/janFrancoo/Java-Projects/blob/master/Bank%20Customer%20Management%20Project/Main.java


Sonraki Yazı: Pong Game
Yorumlar

Henüz bir yorum bulunmuyor.
Yorum bırakın