Current location - Trademark Inquiry Complete Network - Overdue credit card - Write a bank account and a credit card account in java. Credit card account inherits bank account, seeking source code.
Write a bank account and a credit card account in java. Credit card account inherits bank account, seeking source code.
Public category account {

Protected string accId

Protected string name;

Protected double money;

Public account (string accId, string name) (

this(accId,name,0);

}

Public account (string accId, string name, double money) (

this.accId = accId

this.name = name

This.money = money;

}

public void save money(double money){

If (money < = 0) {

System.out.println ("deposit amount must be greater than 0");

}

this . money+= money;

System.out.println ("successful deposit");

}

public double getMoney(double money){

If (money < = 0) {

System.out.println ("withdrawal amount must be greater than 0");

Returns 0;

}

If (this.money < = money) {

System.out.println ("insufficient balance, unable to withdraw money");

Returns 0;

}

This.money -= money;

System.out.println ("successful withdrawal");

Pay back the money;

}

public double getBalance(){

Return the money;

}

Protected double getOverdraft(){

Returns 0;

}

//The equals method is implemented, and the list comparison can be correct.

@ Overlay

Common Boolean equals (object object) {

if(obj == null){

Returns false

}

if(this == obj){

Return true

}

If (object instance of account) {

Returns this. accid.equals ((account) obj). accId);

}

Returns false

}

@ Overlay

Common string toString() {

Return "account ="+accId+",name ="+name+",balance = "+money;

}

}

Public banks {

// Account implements the equals method, so that the list can be found correctly.

Private list & lt account & gtusersAccounts

Public bank () {

UsersAccounts = new ArrayList & lt account & gt ();

}

public void addAccount(Account Account){

if(users counts . contains(account)){

System.out.println ("Failed to add, unable to add the same account");

Return;

}

users counts . add(account);

}

Public Boolean Deaccount (account account) (

return users counts . remove(account);

}

public boolean de account(String accId){

Return delAccount (accId, null));;

}

public boolean exist Account(Account Account){

Returns users counts. contains (account);

}

Public boolean existAccount (string accid) (

Returns the existAccount (accId, null));;

}

Public account getaccount (stringacid) {

Return userscounts.get (userscounts.index of (new account (accId, null)));

}

public double getAllMoney() {

//Whether it overflows or not, just add up the balances of all users.

Double result = 0;

for(Account Account:users counts){

result+= account . get balance();

}

Return the result;

}

public double getAllOverdraft() {

//Whether it overflows or not

Double result = 0;

for(Account Account:users counts){

result+= account . get overdraft();

}

Return the result;

}

public int getAccountNum() {

Returns userscounts.size ();

}

public int getCreditAccountNum(){

int num = 0;

for(Account Account:users counts){

// instanceof performance is not as fast as simple method.

Account instance of CreditAccount) {

num++;

}

}

Quantity returned;

}

public int getSavingAccountNum(){

int num = 0;

for(Account Account:users counts){

Account instance of SavingAccount) (

num++;

}

}

Quantity returned;

}

Public list & lt account & gtgetAllAccount() {

Return to usersAccounts

}

}

Public class CreditAccount extended account {

Private double overdraft;

Public CreditAccount (string accId, string name) (

super(accId,name);

This. Overdraft =1000;

}

public credit account(String accId,String name,double money){

This (accId, name, money,1000);

}

Public credit account (String Accid, String name, double money, double Overdraft) {

Super (accId, name, money);

This. Overdraft = Overdraft;

}

@ Overlay

public double getMoney(double money){

If (money < = 0) {

System.out.println ("withdrawal amount must be greater than 0");

Returns 0;

}

If(this.money+overdraft < = money) {

System.out.println ("insufficient balance, unable to withdraw money");

Returns 0;

}

This.money -= money;

System.out.println ("successful withdrawal");

Pay back the money;

}

@ Overlay

Public double get overload () {

Return the overdraft;

}

@ Overlay

Common string toString() {

Return "account ="+accId+ ",name ="+name+", balance ="+money+ ",overdraft ="+overdraft;

}

}

public class saving Account extends Account {

Public SavingAccount (string accId, string name)

super(accId,name);

}

public saving account(String accId,String name,double money) {

Super (accId, name, money);

}

@ Overlay

public double getMoney(double money){

Return super.getMoney (money);

}

@ Overlay

Public double getOverdraft() {

return super . get overdraft();

}

}

Common class test {

Private static bank bank = newbank ();

Public static void main(String[] args) {

test . gen account();

//Open an 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);

Account a4 = new CreditAccount("4 ","4 ",500,2000);

Account a5 = new CreditAccount("4 ","5 ",600,2000); //Account number 4 is heavy

bank . add account(a 1);

bank . add account(a2);

bank . add account(a3);

bank . add account(a4);

bank . add account(a5);

showNowAccount();

//Account cancellation

bank . delaccount(" 1 ");

bank . delaccount(" 2 ");

showNowAccount();

//deposit

if(bank . exist account(" 3 "){

account = bank . get account(" 3 ");

ACC . save money( 100);

}

showNowAccount();

//Withdraw money

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());

}

Public static void genAccount(){

string s = " 1000 0000 0000 000 ";

Account a = empty;

for(int I = 1; I< 1 1; i ++){

If ((I & 2) = = 0) {

A = new credit account (s+String.valueOf(i), "account"+string.valueof (i));

} Otherwise {

A = new savings account (s+String.valueOf(i), "account"+string.valueof (i));

}

bank . add account(a);

}

}

Public static void shownowacount () {

for(Account Account:bank . getallaccount()){

system . out . println(account);

}

}

}