受保護的字符串accId
受保護的字符串名稱;
受保護的雙錢;
公共帳戶(字符串accId,字符串名稱){
this(accId,name,0);
}
公共賬戶(字符串accId,字符串name,double money){
this.accId = accId
this.name = name
this.money =錢;
}
public void save money(double money){
如果(錢& lt= 0){
System.out.println("存款金額必須大於0 ");
}
this . money+= money;
System.out.println("存款成功");
}
public double getMoney(double money){
如果(錢& lt= 0){
System.out.println("取款金額必須大於0 ");
返回0;
}
如果(this.money & lt=錢){
System.out.println("余額不足,無法取錢");
返回0;
}
this.money -=錢;
System.out.println("提現成功");
還錢;
}
public double getBalance(){
歸還這筆錢;
}
受保護的double getOverdraft(){
返回0;
}
//實現了equals方法,列表比較可以正確。
@覆蓋
公共布爾等於(對象對象){
if(obj == null){
返回false
}
if(this == obj){
返回true
}
if(帳戶的對象實例){
返回this . accid . equals(((Account)obj)。accId);
}
返回false
}
@覆蓋
公共字符串toString() {
Return "account ="+accId+",name ="+name+",balance = "+money;
}
}
公共類銀行{
// Account實現equals方法,這樣可以正確找到列表。
個人分發名單& lt帳戶& gtusersAccounts
公共銀行(){
usersAccounts = new ArrayList & lt帳戶& gt();
}
public void addAccount(Account Account){
if(users counts . contains(account)){
System.out.println("添加失敗,無法添加同壹賬戶");
返回;
}
users counts . add(account);
}
公共布爾de Account(Account Account){
return users counts . remove(account);
}
public boolean de account(String accId){
return delAccount(新帳戶(accId,null));
}
public boolean exist Account(Account Account){
返回users counts . contains(account);
}
public boolean existAccount(字符串accId) {
返回existAccount(新帳戶(accId,null));
}
公共帳戶getAccount(String accId){
return users counts . get(users counts . index of(新帳戶(accId,null)));
}
public double getAllMoney() {
//不管是否溢出,只把所有用戶余額加起來。
雙結果= 0;
for(Account Account:users counts){
result+= account . get balance();
}
返回結果;
}
public double getAllOverdraft() {
//不管它是否溢出
雙結果= 0;
for(Account Account:users counts){
result+= account . get overdraft();
}
返回結果;
}
public int getAccountNum() {
返回users counts . size();
}
public int getCreditAccountNum(){
int num = 0;
for(Account Account:users counts){
// instanceof性能不如simple方法快。
CreditAccount的帳戶實例){
num++;
}
}
退貨數量;
}
public int getSavingAccountNum(){
int num = 0;
for(Account Account:users counts){
SavingAccount的帳戶實例){
num++;
}
}
退貨數量;
}
公共列表& lt帳戶& gtgetAllAccount() {
返回usersAccounts
}
}
公共類CreditAccount擴展帳戶{
私人雙重透支;
public CreditAccount(字符串accId,字符串名稱){
super(accId,name);
this .透支= 1000;
}
public credit account(String accId,String name,double money){
這個(accId,名字,錢,1000);
}
public credit account(String accId,String name,double money,double透支){
超級(accId,名字,錢);
this .透支=透支;
}
@覆蓋
public double getMoney(double money){
如果(錢& lt= 0){
System.out.println("取款金額必須大於0 ");
返回0;
}
if(this.money +透支& lt=錢){
System.out.println("余額不足,無法取錢");
返回0;
}
this.money -=錢;
System.out.println("提現成功");
還錢;
}
@覆蓋
公共double getOverdraft(){
歸還透支款;
}
@覆蓋
公共字符串toString() {
Return "account ="+accId+",name ="+name+",balance ="+money+",透支= "+透支;
}
}
public class saving Account extends Account {
public SavingAccount(字符串accId,字符串名稱){
super(accId,name);
}
public saving account(String accId,String name,double money) {
超級(accId,名字,錢);
}
@覆蓋
public double getMoney(double money){
return super.getMoney(錢);
}
@覆蓋
公共double getOverdraft() {
return super . get overdraft();
}
}
公共類測試{
私有靜態銀行Bank = new Bank();
公共靜態void main(String[] args) {
test . gen account();
//開戶
account a 1 = new credit account(" 1 "," 1 ",200,2000);
Account a2 = new SavingAccount("2 "," 2 ",300);
Account a3 = new SavingAccount("3 "," 3 ",400);
賬戶a4 = new CreditAccount("4 "," 4 ",500,2000);
賬戶a5 = new CreditAccount("4 "," 5 ",600,2000);//賬號4重
bank . add account(a 1);
bank . add account(a2);
bank . add account(a3);
bank . add account(a4);
bank . add account(a5);
showNowAccount();
//賬戶註銷
bank . delaccount(" 1 ");
bank . delaccount(" 2 ");
showNowAccount();
//存款
if(bank . exist account(" 3 "){
account = bank . get account(" 3 ");
ACC . save money(100);
}
showNowAccount();
//取錢
if(bank . exist account(" 3 "){
account = bank . get account(" 3 ");
system . out . println(" balance = "+ACC . get balance());
ACC . getmoney(100);
system . out . println(" balance = "+ACC . get balance());
ACC . getmoney(1000);
system . out . println(" balance = "+ACC . get balance());
}
if(bank . exist account(" 4 "){
account = bank . get account(" 4 ");
system . out . println(" balance = "+ACC . get balance());
ACC . getmoney(100);
system . out . println(" balance = "+ACC . get balance());
ACC . getmoney(1000);
system . out . println(" balance = "+ACC . get balance());
ACC . getmoney(10000);
system . out . println(" balance = "+ACC . get balance());
}
system . out . println(bank . getaccountnum());
system . out . println(bank . getall money());
system . out . println(bank . getalloverdraft());
system . out . println(bank . getcreditaccountnum());
system . out . println(bank . getsavingaccountnum());
}
公共靜態void genAccount(){
string s = " 1000 0000 0000 000 ";
賬戶a =空;
for(int I = 1;我& lt11;i ++){
如果((我& amp2) == 0){
A =新的信用帳戶(s+String.valueOf(i)," account "+string . value of(I));
}否則{
A =新的儲蓄賬戶(s+String.valueOf(i)," account "+string . value of(I));
}
bank . add account(a);
}
}
公共靜態void showNowAccount(){
for(Account Account:bank . getallaccount()){
system . out . println(account);
}
}
}