본문 바로가기

JavaCode(review)

Bank Application - Autoboxing & Unboxing Challenge

반응형

<class Customers> 코드

package com.company;

import java.util.ArrayList;

public class Customers {        //have arrayList of Doubles(transactions)
                                    //name and ArrayList of doubles
    private ArrayList<Double> transactions;
    private String name;

    //constructor
    public Customers(String name, double initialization) {
        this.name = name;
        this.transactions = new ArrayList<Double>();    //constructor에 initailization장단점 있음 추후에
        transactions.add(initialization);
    }
    //add transaction
    public void  addTransaction(double amount){     //deposit or balance
        this.transactions.add(amount);
    }
    //getter

    public ArrayList<Double> getTransactions() {
        return transactions;
    }

    public String getName() {
        return name;
    }

    //calling constructor
    public static Customers creatCustomers(String name, double transaction){
        return new Customers(name, transaction);
    }
}

 

<Class Branch> code

package com.company;

import java.util.ArrayList;

public class Branches {         //have arrayList of Customers.
                       //add a new customer, initial transaction-add additional amount

    private ArrayList<Customers> Mycustomers;           //object myCustomers
    private String branchName ;

    //constructor
    public Branches(String branchName){
        this.Mycustomers = new ArrayList<Customers>();
        this.branchName = branchName;
    }

    //getter

    public ArrayList<Customers> getMycustomers() {
        return Mycustomers;
    }

    public String getBranchName() {
        return branchName;
    }

    //add customer
    public boolean addNewCustomer(String newCustomer, double initialAmount){
        if(CustomerExists(newCustomer) != null) {
            System.out.println("already on the list");
            return false;
        }                                                   //arrayList의 add기능 이용/Customer setting.
        this.Mycustomers.add(new Customers(newCustomer,initialAmount));      
        return true;
    }

    //add amount
    public boolean addNewTransaction(String name, double amount){
        Customers currentName = CustomerExists(name);
        if(currentName != null) {
            currentName.addTransaction(amount);         //class cutomer의 method
            return true;              //addTransaction 변화시키고 싶으면 class customer가서 변화시킴 된다.
        }
        return false;
    }
    //private int (Customers customers){           //index 여부 찾기
      //  return this.Mycustomers.indexOf(customers);
   // }
    private Customers CustomerExists(String name){                  //i=index
        for(int i=0; i<this.Mycustomers.size(); i++){
            Customers checkCustomers= this.Mycustomers.get(i);   //ArrayList에서 object checkCustomers로 전환
            if(checkCustomers.getName().equals(name)){
                return checkCustomers;                          //class branch에 customer저장.
            }
        }
        return null;           //dataType에 int findcutomer보다 Customers findecustomer쓰면
    }                           // Customer Class의 field에 해당하는 dataType거 암거나 가능.

}

<class bank>

package com.company;

import java.util.ArrayList;

public class Bank {     //have arrayList of Branches
                            //add a new branch, add a customer to that branch
                            //add a transaction to that branch(existing customer)
                            //show a list of customer of that branch, list of their transaction(option)

    private ArrayList<Branches> branches;               //arrayList define
    private String bankName;

    //constructor
    public Bank(String bankName) {
        this.branches = new ArrayList<Branches>();  // arrayList 공간설정
        this.bankName = bankName;
    }

    //getter
    public ArrayList<Branches> getBranches() {
        return branches;
    }
    public String getBankName() {
        return bankName;
    }
    //methods
    //add a new branch
    public boolean addNewBranch(String newBranch){
        if(BranchExists(newBranch) !=null){
            System.out.println("already on the list");
            return false;
        }
        this.branches.add(new Branches(newBranch));     //class branch setting.
        return true;
    }
    //add customer to that branch   //customer walks in and open an account
    public boolean addCustomerToBranch(String newBranch, String newCustomer,double initialAmount){
        Branches branch = BranchExists(newBranch)  ;        //이거 object 정의 생각못해서 막힘.
        if(branch !=null){
            return branch.addNewCustomer(newCustomer,initialAmount);
        }
        return false;
    }
    //add cutomer transaction
    public boolean addCustomerTranaction(String newBranch, String newCustomer,double amount){
        Branches branch = BranchExists(newBranch)  ;        //이거 object정의 생각못해서 막힘.
        if(branch !=null){
            return branch.addNewTransaction(newCustomer, amount);
        }
        return false;
    }

    private Branches BranchExists(String name){     //i=index
        for(int i=0; i<this.branches.size(); i++){
            Branches checkBranch = this.branches.get(i);
            if(checkBranch.getBranchName().equals(name)){
                return checkBranch;         //존재한다
            }
        }
        return null;            //존재안한다.
    }

    //list
    public boolean listCustomer(String branchName, boolean showTransaction){
        Branches branch = BranchExists(branchName);
        if(branch != null){
            System.out.println("Customers for branch"+branch.getBranchName());

            ArrayList<Customers> branchCustomers = branch.getMycustomers();
            for(int i=0; i<branchCustomers.size(); i++){
                Customers branchCustomer = branchCustomers.get(i);
                System.out.println("Customer: "+branchCustomer.getName()+"["+(i+1)+"]");
                if(showTransaction){
                    System.out.println("Transactions");
                    ArrayList<Double> transactions = branchCustomer.getTransactions();
                    for(int j=0; j<transactions.size(); j++){
                        System.out.println("["+(j+1)+"] Amount "+transactions.get(j));
                    }
                }
            }return true;
        }else{
        return false;
        }
    }
}

<class main>

package com.company;

public class Main {         //unboxing challenge  - Bank application

    public static void main(String[] args) {
	// write your code here
        Bank bank = new Bank("National Korean Bank"); // object설정/call constructor

        bank.addNewBranch("GangNam");       //new branch and new customers

        bank.addCustomerToBranch("GangNam","Tim",50.05);
        bank.addCustomerToBranch("GangNam","JAY",45);

        bank.addNewBranch("SinDorim");      //new branch and new customers

        bank.addCustomerToBranch("SinDorim","Kay",34);

        bank.listCustomer("GangNam",false);
        
    }
}

<output>

D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=50840:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\unboxingChallenge\out\production\unboxingChallenge com.company.Main
Customers for branchGangNam
Customer: Tim[1]
Customer: JAY[2]

Process finished with exit code 0

 

<another main>

package com.company;

public class Main {         //unboxing challenge  - Bank application

    public static void main(String[] args) {
	// write your code here
        Bank bank = new Bank("National Korean Bank"); // object설정/call constructor

        bank.addNewBranch("GangNam");       //new branch and new customers

        bank.addCustomerToBranch("GangNam","Tim",50.05);
        bank.addCustomerToBranch("GangNam","JAY",45);

        bank.addNewBranch("SinDorim");      //new branch and new customers

        bank.addCustomerToBranch("SinDorim","Kay",34);

        bank.addCustomerTranaction("GangNam","Tim",24);
        bank.listCustomer("GangNam",true);

    }
}

<output>

D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=50865:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\NewProject\unboxingChallenge\out\production\unboxingChallenge com.company.Main
Customers for branchGangNam
Customer: Tim[1]
Transactions
[1] Amount 50.05
[2] Amount 24.0
Customer: JAY[2]
Transactions
[1] Amount 45.0

Process finished with exit code 0

 

Q.