본문 바로가기

Java

[자바 객체지향] 인터페이스 예제 Bank.java

Account.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package jse.alabama.bank;
 
import lombok.Data;
 
@Data
public class Account {
    protected int accountNo; 
    protected String ownerName; 
    private String password; 
    protected int restMoney; 
    protected static final String BANK_NAME = "한빛뱅크";
 
    
    public Account() {
        this.accountNo = (int) ((Math.random() * 999999+ 100000);
    }
 
    @Override
    public String toString() {
        return "[" + BANK_NAME + "] \n" 
                + "A Deposit Account \n" 
                + "Bank Account Number :" + accountNo + "\n" 
                + "Account Holder :" + ownerName + "\n" 
                + "Password : " + password + "\n"
                +" Balance :" + restMoney ;
    }
}
 
cs

IBank.java

1
2
3
4
5
6
7
8
9
package jse.alabama.bank;
 
public interface IBank {
    public String open(String accountName, String password);
    public String deposit(int inMoney); 
    public String withdraw(int outMoney);
    public String search();
}
 
cs

IAdmin.java

1
2
3
4
5
6
7
8
9
10
11
package jse.alabama.bank;
 
public interface IAdmin {
    
        public String openAccount(int accountNo, String name, String password, int restMoney);
        public Account searchAccountByAccountNo(int accountNo);
        public Account[] searchAccountsByName(String name);
        public String closeAccount(int accountNo);
        public int countAll();
 
}
cs

BankService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package jse.alabama.bank;
 
public class BankService implements IBank{
    private Account account = new Account(); 
 
 
    @Override 
    public String deposit(int inMoney) { 
        account.setRestMoney(inMoney + account.getRestMoney());
        return ("The balance is "+account.getRestMoney());
    
    }
    
    public String deposit(int inMoney,String str) { 
        account.setRestMoney(inMoney + account.getRestMoney());
        return ("The balance is "+account.getRestMoney());
    
    }
 
    @Override
    public String withdraw(int outMoney) { 
        String Massage = "";
 
        if (account.getRestMoney() - outMoney < 0) {
            Massage = "The balance is insufficient. Enter again.";
        }
 
        else {
            account.setRestMoney(account.getRestMoney()-outMoney);
            Massage = ("The balance is " + account.getRestMoney());
        }
        return Massage;
    }
 
    @Override
    public String search() {
        
        return "The balance is " +account.getRestMoney();
 
    }
 
    @Override
    public String open(String accountName, String password) { 
        
        int random = (int) ((Math.random()*999999)+100000);
        
        return  Account.BANK_NAME+"\n"
                +"bank account number : "+random+"\n"
                +"name : "+ ""+"\n"
                +"password : *******\n"
                +"balance : "+account.getRestMoney();
        
    }
}
 
cs

AdminService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package jse.alabama.bank;
 
import lombok.Data;
 
@Data
public class AdminService implements IAdmin{
    private Account[] accountList;
    private int count = 0;
 
    public AdminService() {
        this(0);
    }
 
    public AdminService(int count) {
        accountList = new Account[count];
    }
 
    @Override
    public String openAccount(int accountNo, String name, String password, int restMoney) {
        Account account = new Account();
        String msg = "";
        account.setAccountNo(accountNo);
        account.setOwnerName(name);
        account.setPassword(password);
        account.setRestMoney(restMoney);
        accountList[count] = account;
        count++;
        msg = account.toString();
        return msg;
    }
 
    @Override
    public Account searchAccountByAccountNo(int accountNo) {
        Account searchAccount = null;
        for (int i = 0; i < this.getCount(); i++) {
            if (accountList[i].getAccountNo() == accountNo) {
                searchAccount = accountList[i];
            }
        }
        return searchAccount;
    }
 
    @Override
    public Account[] searchAccountsByName(String name) {
        int tempCount = 0
        for (int i = 0; i < count; i++) {
            if (accountList[i].getOwnerName().equals(name)) {
                tempCount++;
            }
        }
        Account[] tempList = new Account[tempCount];
        tempCount = 0;
        for (int i = 0; i < count; i++) {
            if (accountList[i].getOwnerName().equals(name)) {
                tempList[tempCount] = accountList[i];
                tempCount++;
            }
 
        }
        return tempList;
    }
 
    @Override
    public String closeAccount(int accountNo) {
        String result = "Account to terminate does not exist";
        for (int i = 0; i < this.getCount(); i++) {
            if (accountList[i].getAccountNo() == accountNo) {
                accountList[i] = accountList[count - 1];
                accountList[count - 1= null;
                result = "The account you requested has been closed";
            }
        }
        count--;
        return result;
    }
 
    @Override
    public int countAll() {
        return this.getCount();
    }
}
 
cs

BankController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package jse.alabama.bank;
import java.util.Scanner;
 
public class BankController {
    public static void main(String[] args) {
        BankService service = new BankService();
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            System.out.println("Please decide what to do. : \n"
                    + "1. Account opening \n"
                    + "2. Deposit \n"
                    + "3. Withdrawal \n"
                    + "4. Account inquiry \n"
                    + "5. Termination");
            int key = scanner.nextInt();
            
            switch (key) {
            case 1:
                System.out.println("Please type your name.");
                String name = scanner.next();
                System.out.println("Please enter your password.");
                String password = scanner.next();
                System.out.println(service.open(name, password));
                break;
            case 2:
                System.out.println("Please enter the amount to deposit.");
                int inMoney = scanner.nextInt();
                System.out.println(service.deposit(inMoney));
                break;
            case 3:
                System.out.println("Please enter the amount to withdraw..");
                int outMoney = scanner.nextInt();
                System.out.println(service.withdraw(outMoney));
                
                break;
            case 4:
                System.out.println(service.search());
                break;
            case 5:
                return;
            default:
                System.out.println("Invalid input. Please re-enter the task you want");
                break;
 
            }
 
        }
    }
}
 
cs

AdminController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package jse.alabama.bank;
import java.util.Scanner;
public class AdminController {
    public static void main(String[] args) {
        IAdmin service = new AdminService(1000);
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            System.out.println("Please decide what to do. : \n"
                    + "1. Account opening \n"
                    + "2.Search (account number) \n"
                    + "3.Search (Name) \n"
                    + "4.Total account count \n"
                    + "5.Revocation \n"
                    + "6.Termination");
            int key = scanner.nextInt();
            
            switch (key) {
            case 1:
                System.out.println("Enter your account number.");
                int accountNo = scanner.nextInt();
                System.out.println("Type your name.");
                String name = scanner.next();
                System.out.println("Enter input amount");
                int restMoney = scanner.nextInt();
                System.out.println("Enter input password");
                String password = scanner.next();
                System.out.println(service.openAccount(accountNo, name, password, restMoney));
                break;
            
            case 2:
                System.out.println("Enter your account number.");
                int searchaccountNo = scanner.nextInt();
                System.out.println(service.searchAccountByAccountNo(searchaccountNo));
                break;
            case 3:
                System.out.println("Type your name.");
                String searchName = scanner.next();
                Account [] arr = service.searchAccountsByName(searchName);
                for (int i = 0; i < arr.length; i++) {
                    System.out.println(arr[i]);
                }
                
                break;
            case 4:
                System.out.println(service.countAll());
                
                break;
            case 5:
                System.out.println("Enter your account number.");
                int closeAccount = scanner.nextInt();
                System.out.println(service.closeAccount(closeAccount));
                break;
                
            case 6:
                return;
            default:
                System.out.println("Invalid input. Please re-enter the task you want");
                break;
 
            }
 
        }
    }
}
 
cs

End