用java模擬銀行賬戶儲存系統
阿新 • • 發佈:2019-01-07
package cn.mdln.study2;
/**
* 模擬銀行賬戶儲存系統
* @author Administrator
*
*/
public class TestDemo16 {
public static void main(String[] args) {
Accounts2 account1=new Accounts2(500);
Accounts2 account2=new Accounts2(600);
Accounts2 account3=new Accounts2(700);
Customer2 customer1=new Customer2("Smith","12345","13075325795");
Customer2 customer2=new Customer2("lucyy","a1b2c","23075325795");
Customer2 customer3=new Customer2("bobye","se875","33075325795");
Bank band=new Bank();
band.addCustome(customer1);
band.addCustome(customer2);
band.addCustome(customer3);
customer1.setAccounts(account1);
customer2.setAccounts(account2);
customer3.setAccounts(account3);
for(int x=0;x<band.getNumberOfCustomer();x++)
{
System.out.println("index["+x+"]="+band.getCustomer2(x));
}
for(int x=0;x<band.getNumberOfCustomer();x++)
{
System.out.println("您的存款金額為:"+band.getCustomer2(x).getAccounts().getBanlance());
System.out.println("是否交易成功:"+band.getCustomer2(x).getAccounts().withdraw(150));
System.out.println(band.getCustomer2(x).toString()+"您的賬戶當前可用金額:"+band.getCustomer2(x).getAccounts().getBanlance());
System.out.println("是否交易成功:"+band.getCustomer2(x).getAccounts().deposit(22.5)); ;
System.out.println(band.getCustomer2(x).toString()+"您的賬戶當前可用金額:"+band.getCustomer2(x).getAccounts().getBanlance());
System.out.println("是否交易成功:"+band.getCustomer2(x).getAccounts().withdraw(47.62));
System.out.println(band.getCustomer2(x).toString()+"您的賬戶當前可用金額:"+band.getCustomer2(x).getAccounts().getBanlance());
}
}
}
class Accounts2//賬戶
{
private double balance;//賬戶餘額
public Accounts2(double balance)
{
this.balance=balance;
}
public double getBanlance()//獲取賬戶餘額
{
return this.balance;
}
public boolean deposit(double money)//存錢
{
this.balance+=money;
return true;
}
public boolean withdraw(double money)//取錢
{
if(this.balance<money)
{
System.out.println("餘額不足");
return false;
}
else
{
this.balance-=money;
return true;
}
}
}
class Customer2//一個使用者
{
private String name;//使用者名稱
private String idCard;//使用者身份號
private String phone;//使用者聯絡方式
private Accounts2 account;//類與類之間是組合關係
public Customer2(String name,String idCard,String phone)
{
this.name=name;
this.idCard=idCard;
this.phone=phone;
}
public String getName()
{
return this.name;
}
public String toString()
{
return "\t使用者名稱:"+this.name+"\n\t身份證號:"+this.idCard+"\n\t聯絡方式:"+this.phone+"\n";
}
public Accounts2 getAccounts()
{
return this.account;
}
public void setAccounts(Accounts2 account)
{
this.account=account;
}
}
class Bank//一個銀行
{
private Customer2[] customer;//銀行有多個使用者
private static int count=0;
public Bank()
{
customer=new Customer2[10];
}
public void addCustome(Customer2 customer)
{
this.customer[count++]=customer;
}
@SuppressWarnings("static-access")
public int getNumberOfCustomer()
{
return this.count;
}
public Customer2 getCustomer2(int index)
{
return this.customer[index];
}
}
/**
* 模擬銀行賬戶儲存系統
* @author Administrator
*
*/
public class TestDemo16 {
public static void main(String[] args) {
Accounts2 account1=new Accounts2(500);
Accounts2 account2=new Accounts2(600);
Accounts2 account3=new Accounts2(700);
Customer2 customer1=new Customer2("Smith","12345","13075325795");
Customer2 customer2=new Customer2("lucyy","a1b2c","23075325795");
Customer2 customer3=new Customer2("bobye","se875","33075325795");
Bank band=new Bank();
band.addCustome(customer1);
band.addCustome(customer2);
band.addCustome(customer3);
customer1.setAccounts(account1);
customer2.setAccounts(account2);
customer3.setAccounts(account3);
for(int x=0;x<band.getNumberOfCustomer();x++)
{
System.out.println("index["+x+"]="+band.getCustomer2(x));
}
for(int x=0;x<band.getNumberOfCustomer();x++)
{
System.out.println("您的存款金額為:"+band.getCustomer2(x).getAccounts().getBanlance());
System.out.println("是否交易成功:"+band.getCustomer2(x).getAccounts().withdraw(150));
System.out.println(band.getCustomer2(x).toString()+"您的賬戶當前可用金額:"+band.getCustomer2(x).getAccounts().getBanlance());
System.out.println("是否交易成功:"+band.getCustomer2(x).getAccounts().deposit(22.5)); ;
System.out.println(band.getCustomer2(x).toString()+"您的賬戶當前可用金額:"+band.getCustomer2(x).getAccounts().getBanlance());
System.out.println("是否交易成功:"+band.getCustomer2(x).getAccounts().withdraw(47.62));
System.out.println(band.getCustomer2(x).toString()+"您的賬戶當前可用金額:"+band.getCustomer2(x).getAccounts().getBanlance());
}
}
}
class Accounts2//賬戶
{
private double balance;//賬戶餘額
public Accounts2(double balance)
{
this.balance=balance;
}
public double getBanlance()//獲取賬戶餘額
{
return this.balance;
}
public boolean deposit(double money)//存錢
{
this.balance+=money;
return true;
}
public boolean withdraw(double money)//取錢
{
if(this.balance<money)
{
System.out.println("餘額不足");
return false;
}
else
{
this.balance-=money;
return true;
}
}
}
class Customer2//一個使用者
{
private String name;//使用者名稱
private String idCard;//使用者身份號
private String phone;//使用者聯絡方式
private Accounts2 account;//類與類之間是組合關係
public Customer2(String name,String idCard,String phone)
{
this.name=name;
this.idCard=idCard;
this.phone=phone;
}
public String getName()
{
return this.name;
}
public String toString()
{
return "\t使用者名稱:"+this.name+"\n\t身份證號:"+this.idCard+"\n\t聯絡方式:"+this.phone+"\n";
}
public Accounts2 getAccounts()
{
return this.account;
}
public void setAccounts(Accounts2 account)
{
this.account=account;
}
}
class Bank//一個銀行
{
private Customer2[] customer;//銀行有多個使用者
private static int count=0;
public Bank()
{
customer=new Customer2[10];
}
public void addCustome(Customer2 customer)
{
this.customer[count++]=customer;
}
@SuppressWarnings("static-access")
public int getNumberOfCustomer()
{
return this.count;
}
public Customer2 getCustomer2(int index)
{
return this.customer[index];
}
}