Build a small bank with Java code

Source: Internet
Author: User
Tags stub

First I write this bank management system is also through the establishment of classes, the class and the class to invoke data between the method of implementation, I this time the banking system used three classes, respectively, the personal account class, Bank class, the Bank interface display class, through the account class constructor to achieve the data in the call between the three classes, Bank class implementation data preservation and so on, the specific code is as follows, if you need to look at the source file, please go to my blog to find Mytest_bank compressed package.

Account Class

Package MYbank;

Import Mybank.account;

public class Account {
Account sub-class
protected String name;
Protected String accountid;////Account
protected int number;//ID number
Protected Double balance;//Balance
Protected String starttime;//Opening time

Public account (String AccountId) {
TODO auto-generated Constructor stub
This (accountid,null,null,0,0);
}
Construction method
Public account (String accountid,string name,string starttime,int number,double balance) {

This.accountid = AccountId;
This.name=name;
This.starttime=starttime;
This.number=number;
This.balance=balance;
}


The following method is very important, with it can implement the object of the addition of Delete, deposit and so on
The Equals method is implemented and the list is compared correctly
@Override
public boolean equals (Object obj) {
if (obj = = null) {
return false;
}
if (this = = obj) {
return true;
}
if (obj instanceof account) {
Return This.accountid.equals ((account) obj). AccountId);
}
return false;
}







Deposit
public void Savemoney (double money) {
if (money<=0) {
SYSTEM.OUT.PRINTLN ("Deposit amount must be greater than 0");
}
This.balance+=money;
System.out.println ("deposit success");
}
Withdrawal
Public double Getmoney (double money) {
if (money>this.balance) {
SYSTEM.OUT.PRINTLN ("Insufficient balance, withdrawal failed");
return 0;
}
if (money<=this.balance) {
This.balance-=money;
System.out.println ("successful withdrawal");
System.out.println ("The current balance is" +this.balance);
return this.balance;
}
return this.balance;

}
Public String toString () {
TODO auto-generated Method Stub
Return "account:" + AccountId + "Name:" + name + "\ nthe Opening time:" +starttime+ "\ n ID Number:" +number+ "\ n Balance:" + balance+ "\ n";
}

Deposit
public void Savemoney (double money) {
if (Money <= 0) {
SYSTEM.OUT.PRINTLN ("Deposit amount must be greater than 0");
}
this.balance+= money;
System.out.println ("deposit success");
}
Public double getbalance () {
return this.balance;

}
Withdrawal
Public double Getmoney (double money) {
if (Money <= 0) {
SYSTEM.OUT.PRINTLN ("Withdrawal amount must be greater than 0");
return 0;
}
if (this.balance <= money) {
SYSTEM.OUT.PRINTLN ("Insufficient balance, unable to withdraw money");
return 0;
}
This.balance-= money;
System.out.println ("successful withdrawal");
return money;
}

}

Bank class

Package MYbank;


Import account;
Import Mybank.account;
Import MYbank. Banks;

Import Java.util.Scanner;
public class Bank {
private static Banks Banks = new Banks ();
public static void Main (string[] args) {
choose ();
}
//Select method
public static void Choose () {
int x;
Scanner scanner=new Scanner (system.in);
System.out.println ("1. Account opening");
Bank n1 = new Bank ();
System.out.println ("2. Deposit");
System.out.println ("3. Withdrawal");
System.out.println ("4. Query");
System.out.println ("5. Pin");
System.out.print ("Please enter the service number you need:");
X=scanner.nextint ();
Switch (x) {
Case 1:
N1.account ();
Break
Case 2:
N1.savemoney ();
Break
Case 3:
N1.getmoney ();
Break
Case 4:
//banks.getaccount (2);
N1.shownowaccount ();
break;
Case 6:
Banks.getaccountnum ();
Break
Case 5:
N1.delete ();
Break
Case 7:
Banks. All ();
break;
}
Choose ();
}

//account Method opening method
public void account () {
int balance=0;
Scanner scanner=new Scanner (system.in);

System.out.print ("Please enter the name of the depositor:");
String Name=scanner.next ();
System.out.print ("Please enter opening hours:");
String starttime = Scanner.next ();
System.out.print ("Please enter your account:");
String Accountid=scanner.next ();
System.out.print ("Please enter your ID number:");
int Number=scanner.nextint ();
System.out.println ("depositor name:" +name+ "" +
"Account number:" +accountid+ "" +
"ID Number:" +number+ "" +
"Balance:" +balance+ "" +
"Opening hours:" +starttime);

Account person = new Account (Accountid,name,starttime, number,balance);
Banks.addaccount (person);



}
Method of selling households
public void Delete () {
System.out.print ("Please enter the ID of the user you want to pin:");
Scanner Scanner = new Scanner (system.in);

String Id=scanner.nextline ();
Account Accountid=id;
Banks.deleteaccount (ID);

}

Show All accounts
public static void Shownowaccount () {

For (account Account:banks.getAllAccount ()) {
SYSTEM.OUT.PRINTLN (account);
}


}

How to Deposit
public static void Savemoney () {

Scanner input = new Scanner (system.in);
System.out.println ("Please enter the account to deposit:");
String accountId = Input.nextline ();
if (Banks.existaccount (accountId)) {
Account Addmoney = Banks.getaccount (accountId);
System.out.println ("Please enter Deposit Amount:");
Double money = input.nextdouble ();
Addmoney.savemoney (Money);
System.out.println ("Current balance:" +addmoney.getbalance ());
}else {
System.out.println ("The account does not exist, please re-enter:");
Savemoney ();
}
Shownowaccount ();

}

How to withdraw money
public static void Getmoney () {

Scanner input = new Scanner (system.in);
System.out.println ("Please enter the account to be withdrawn:");
String accountId = Input.nextline ();
if (Banks.existaccount (accountId)) {
Account ACC = Banks.getaccount (accountId);
System.out.println ("Current balance =" + acc.getbalance ());
System.out.println ("Please enter the withdrawal amount:");
Double money = input.nextdouble ();
Acc.getmoney (Money);
System.out.println ("After taking the balance =" + acc.getbalance ());
}else {
System.out.println ("The account does not exist, please re-enter:");
Getmoney ();
}
Openaccount ();
}





}

Bank interface Display Class

Package MYbank;

Import Mybank.account;
Import java.util.ArrayList;
Import java.util.List;
public class Banks {
Private list<account> account_list;//What's the meaning of this sentence

Public Banks () {

Create an array of accounts
Account_list = new arraylist<account> ();//Create an Array object account_list
}

//Add account
public void AddAccount (accounts person) {
if (account_list.contains (person)) {///method returns True, When and only if this string contains the specified char value sequence
System.out.println ("Add failed, this account already exists!") ");
}else{
//Add account to the account array
Account_list.add (person);
SYSTEM.OUT.PRINTLN ("account added successfully! ");
}
/*account_list.add (account);
System.out.println ("add Success"); */
}
//Determine if there is an account in the account list to be modified
public boolean existaccount (String accountId) {
Account Existaccount = new account (ACCOUNTID);
return Account_list.contains (Existaccount);

}

//delete user
public void DeleteAccount (String accountid) {
Account Delaccount = new Client (accountid);//account_ List = new arraylist<account> ();
Account_list.remove (Delaccount);
if (Account_list.contains (AccountId)) {
System.out.println ("Pin failed");
} else{
System.out.println ("pin success");
}
/*if (Account_list.contains (AccountId)) {
if (Account_list.remove (AccountId)) {
System.out.println ("pin successful ");
}else{
System.out.println ("Failed to pin the account");
}
//account delaccount = new Account;//account_list = new arraylist<account> ();
//return Account_list.remove (delaccount);

}else{
System.out.println ("no user");
} */
/*if (Account_list.contains (AccountId)) {
Account DeleteAccount = new account (accountid);//Create a subclass
Account_list.remove (AccountId);
if (Account_list.contains (AccountId)) {
System.out.println ("Pin failed");
} else{
System.out.println ("pin success");
}
//system.out.println ("successful Customer");
}
*/
}


Get the total number of accounts
public void Getaccountnum () {

System.out.println ("currently has" +account_list.size () + "user");
return Account_list.size ();

}

Get an Account
Public account Getaccount (String accountId) {
Account Getaccount = new account (ACCOUNTID);
Return Account_list.get (Account_list.indexof (Getaccount)); There are four ways to find string substrings in//java, as follows:
1, int indexOf (String str): Returns the index of the first occurrence of the specified substring in this string.
2, int indexOf (String str, int startIndex): Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
3, int lastIndexOf (string str): Returns the index of the specified substring that appears rightmost in this string.
4, int lastIndexOf (String str, int startIndex): Searches backward at the specified index, returning the index of the specified substring that last occurred in this string.

}
Public list<account> Getallaccount () {

return account_list;

}
public void All () {

System.out.println ("currently has" +account_list+ "users");

}
}

Build a small bank with Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.