자바 질문

import java.text.DecimalFormat;
import java.util.Scanner;

class BankAccount 
{
    private int balance;

    public BankAccount(int initial_amount) {
         if (initial_amount >= 0)
             balance = initial_amount;
         else
             balance = 0;
    }

    public int getBalance() {
        return balance;
    }

    public boolean deposit(int amount) {
        boolean result = false;
        if (amount < 0)
             System.out.println("invalid deposit amount");
         else {
             balance = balance + amount;
             result = true;
         }
         return result;
    }

    public boolean withdraw(int amount) {
        boolean result = false;
        if (amount < 0)
            System.out.println("invalid withdraw amount");
        else if (amount > balance)
            System.out.println("not enough balance");
        else {
            balance = balance - amount;
            result = true;
        }
        return result;
    }
}

class BankReader 
{
    private String input_line = "";

    public BankReader() {
        scan = new Scanner(System.in);  // Reading from System.in
    } 
    public char readCommand(String message) {
        System.out.print(message);
        input_line = scan.nextLine().toUpperCase();
        return input_line.charAt(0);
    }

    public int readAmount()
    {
        int answer = 0;
        String s = input_line.substring(1, input_line.length());

        if(s.length() > 0) {
            double dollars_cents = new Double(s).doubleValue();
            answer = (int)(dollars_cents*100);
        } else
            System.out.println("invalid command - input amount: 0");
        return answer;
    }
}

class BankWriter 
{
    private BankAccount bank;
    private String last_transaction = "";

    public BankWriter(BankAccount b) {
        bank = b;
    }

    public String unconvert(int i) {
        return new DecimalFormat("0.00").format(i/100.0);
    }

    public void setTransaction(String message, int amount)
    { 
        last_transaction = message + " " + unconvert(amount);
        System.out.println("transaction: " + last_transaction);
    }

    public void setTransaction(String message)
    { 
        last_transaction = message;
        System.out.println("transaction: " + last_transaction);
    }

}

class AccountController
{
    private BankReader reader; // input view
    private BankAccount primary_account, secondary_account, account;
    private BankWriter primary_writer, secondary_writer, writer;

    public AccountController (BankReader r, BankAccount a1,BankWriter w1, BankAccount a2, BankWriter w2)
    {
        reader = r;
        primary_account = a1;
        primary_writer = w1;
        secondary_account = a2;
        secondary_writer = w2;
        account = primary_account;
        writer = primary_writer;
    }

    public void resetAccount (BankAccount new_account,BankWriter new_writer) {
        account = new_account;
        writer = new_writer;
    }

    public void processTransactions()
    {
        char command = reader.readCommand("Commands (P/S/D/W/T/I/Q): ");

        switch (command) {
            case 'P':
                resetAccount(primary_account, primary_writer);
                break;
            case 'S':
                resetAccount(secondary_account, secondary_writer);
                break;
            case 'Q':
                System.out.println("Quit");
                return;
            case 'D':
                { 
                    int amount = reader.readAmount();
                    if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
                    else writer.setTransaction("deposit error ", amount);
                    break;
                }
            case 'W':
                { 
                    int amount = reader.readAmount();
                    if (account.withdraw(amount)) writer.setTransaction("withdraw $", amount);
                    else writer.setTransaction("withdraw error ", amount);
                    break;
                }
            case 'T':
                // 'T 금액', 활성 계좌에서 비활성 계좌로 금액만큼 이체
            	{
            		break;
            	}
            case 'I':
                // 'I 이율', 활성 계좌의 금액을 이율만큼 증가
        		{
            		break;
            }
            default:
                writer.setTransaction("invalid commpand: " + command);
        }
        // 모든 계좌의 정보를 출력합니다.
        System.out.println("=======================");
        System.out.println("== currently active : " + ((account == primary_account) ? "primary" : "secondary"));
        System.out.println("== primary account : " + primary_writer.unconvert(primary_account.getBalance()));
        System.out.println("== secondary account : " + secondary_writer.unconvert(secondary_account.getBalance()));
        System.out.println("=======================");

        this.processTransactions();
    }
}


public class Main {
  public static void main(String[] args) {
    BankReader reader = new BankReader();
    BankAccount primary_account = new BankAccount(0);
    BankWriter primary_writer = new BankWriter(primary_account);
    BankAccount secondary_account = new BankAccount(0);
    BankWriter secondary_writer = new BankWriter(secondary_account);
    AccountController controller = new AccountController(reader,
            primary_account, primary_writer, secondary_account,
            secondary_writer);
    controller.processTransactions();
  }
}

주석있는 부분 어떻게 코드를 짜야할지 모르겠어요 도와주실수 있나요?

import java.text.DecimalFormat;
import java.util.Scanner;

class BankAccount 
{
    private int balance;

    public BankAccount(int initial_amount) {
         if (initial_amount >= 0)
             balance = initial_amount;
         else
             balance = 0;
    }

    public int getBalance() {
        return balance;
    }

    public boolean deposit(int amount) {
        boolean result = false;
        if (amount < 0)
             System.out.println("invalid deposit amount");
         else {
             balance = balance + amount;
             result = true;
         }
         return result;
    }

    public boolean withdraw(int amount) {
        boolean result = false;
        if (amount < 0)
            System.out.println("invalid withdraw amount");
        else if (amount > balance)
            System.out.println("not enough balance");
        else {
            balance = balance - amount;
            result = true;
        }
        return result;
    }
}

class BankReader 
{
    private String input_line = "";
    private Scanner scan;

    public BankReader() {
        scan = new Scanner(System.in);
    } 
    public char readCommand(String message) {
        System.out.print(message);
        input_line = scan.nextLine().toUpperCase();
        return input_line.charAt(0);
    }

    public int readAmount()
    {
        int answer = 0;
        String s = input_line.substring(1, input_line.length());

        if(s.length() > 0) {
            double dollars_cents = new Double(s).doubleValue();
            answer = (int)(dollars_cents*100);
        } else
            System.out.println("invalid command - input amount: 0");
        return answer;
    }
}

class BankWriter 
{
    private BankAccount bank;
    private String last_transaction = "";

    public BankWriter(BankAccount b) {
        bank = b;
    }

    public String unconvert(int i) {
        return new DecimalFormat("0.00").format(i/100.0);
    }

    public void setTransaction(String message, int amount)
    { 
        last_transaction = message + " " + unconvert(amount);
        System.out.println("transaction: " + last_transaction);
    }

    public void setTransaction(String message)
    { 
        last_transaction = message;
        System.out.println("transaction: " + last_transaction);
    }

}

class AccountController
{
    private BankReader reader;
    private BankAccount primary_account, secondary_account, account;
    private BankWriter primary_writer, secondary_writer, writer;

    public AccountController (BankReader r, BankAccount a1,BankWriter w1, BankAccount a2, BankWriter w2)
    {
        reader = r;
        primary_account = a1;
        primary_writer = w1;
        secondary_account = a2;
        secondary_writer = w2;
        account = primary_account;
        writer = primary_writer;
    }

    public void resetAccount (BankAccount new_account,BankWriter new_writer) {
        account = new_account;
        writer = new_writer;
    }

    public void processTransactions()
    {
        char command = reader.readCommand("Commands (P/S/D/W/T/I/Q): ");

        switch (command) {
            case 'P':
                resetAccount(primary_account, primary_writer);
                break;
            case 'S':
                resetAccount(secondary_account, secondary_writer);
                break;
            case 'Q':
                System.out.println("Quit");
                return;
            case 'D':
                { 
                    int amount = reader.readAmount();
                    if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
                    else writer.setTransaction("deposit error ", amount);
                    break;
                }
            case 'W':
                { 
                    int amount = reader.readAmount();
                    if (account.withdraw(amount)) writer.setTransaction("withdraw $", amount);
                    else writer.setTransaction("withdraw error ", amount);
                    break;
                }
            case 'T':
            	{
                    int amount = reader.readAmount();
                    if (account.withdraw(amount)) 
                    { 
                        writer.setTransaction("withdraw $", amount);
                        if(account == primary_account) resetAccount(secondary_account, secondary_writer); 
                        else resetAccount(primary_account, primary_writer);
                        if(account.deposit(amount))
                        {
                            writer.setTransaction("deposit $", amount);
                        }
                        else
                        {
                            writer.setTransaction("deposit error ", amount);
                        }
                    }
            		else 
                    { 
                        writer.setTransaction("withdraw error ", amount);
                    }
                    if(account == primary_account) resetAccount(secondary_account, secondary_writer); 
                    else resetAccount(primary_account, primary_writer);
                    break;
            	}
            case 'I':
        	{
                int amount = reader.readAmount() / 100 * account.getBalance() / 100;
                if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
                else writer.setTransaction("deposit error ", amount);
            	break;
            }
            default:
                writer.setTransaction("invalid commpand: " + command);
        }
        System.out.println("=======================");
        System.out.println("== currently active : " + ((account == primary_account) ? "primary" : "secondary"));
        System.out.println("== primary account : " + primary_writer.unconvert(primary_account.getBalance()));
        System.out.println("== secondary account : " + secondary_writer.unconvert(secondary_account.getBalance()));
        System.out.println("=======================");

        this.processTransactions();
    }
}


public class Main {
  public static void main(String[] args) {
    BankReader reader = new BankReader();
    BankAccount primary_account = new BankAccount(0);
    BankWriter primary_writer = new BankWriter(primary_account);
    BankAccount secondary_account = new BankAccount(0);
    BankWriter secondary_writer = new BankWriter(secondary_account);
    AccountController controller = new AccountController(reader,
            primary_account, primary_writer, secondary_account,
            secondary_writer);
    controller.processTransactions();
  }
}

이 코드 사용하시면 될 거 같고, 이동 중에 폰으로 ideone 접속해서 작성한 코드라서 잘못된 부분이 있을 수 있습니다. 입력 받는 부분 에러 나길래 수정해뒀습니다.

코드 실행 결과는 다음과 같습니다. 잘못된 부분 있을 수 있으니 검토 해보세요.

import java.text.DecimalFormat;
import java.util.Scanner;
import java.math.BigDecimal;
import java.math.MathContext;

class BankAccount 
{
    private double balance;

    public BankAccount(double initial_amount) {
         if (initial_amount >= 0.01)
             balance = initial_amount;
         else
             balance = 0;
    }

    public double getBalance() {
        return balance;
    }

    public boolean deposit(double amount) {
        boolean result = false;
        if (amount < 0.01)
             System.out.println("invalid deposit amount");
         else {
             balance = (double)((int)(balance * 100) + (int)(amount * 100)) / 100;
             result = true;
         }
         return result;
    }

    public boolean withdraw(double amount) {
        boolean result = false;
        if (amount < 0.01)
            System.out.println("invalid withdraw amount");
        else if (amount > balance)
            System.out.println("not enough balance");
        else {
             balance = (double)((int)(balance * 100) - (int)(amount * 100)) / 100;
            result = true;
        }
        return result;
    }
}

class BankReader 
{
    private String input_line = "";
    private Scanner scan;

    public BankReader() {
        scan = new Scanner(System.in);
    } 
    public char readCommand(String message) {
        System.out.print(message);
        input_line = scan.nextLine().toUpperCase();
        return input_line.charAt(0);
    }

    public double readAmount()
    {
        double answer = 0;
        String s = input_line.substring(1, input_line.length());

        if(s.length() > 0) {
            answer = new Double(s).doubleValue();
        } else
            System.out.println("invalid command - input amount: 0");
        return answer;
    }
}

class BankWriter 
{
    private BankAccount bank;
    private String last_transaction = "";

    public BankWriter(BankAccount b) {
        bank = b;
    }

    public void setTransaction(String message, double amount)
    {
        last_transaction = message + " " + BigDecimal.valueOf((double)((int)(amount * 100)) / 100).movePointRight(2).round(MathContext.UNLIMITED).movePointLeft(2);
        System.out.println("transaction: " + last_transaction);
    }

    public void setTransaction(String message)
    { 
        last_transaction = message;
        System.out.println("transaction: " + last_transaction);
    }

}

class AccountController
{
    private BankReader reader;
    private BankAccount primary_account, secondary_account, account;
    private BankWriter primary_writer, secondary_writer, writer;

    public AccountController (BankReader r, BankAccount a1,BankWriter w1, BankAccount a2, BankWriter w2)
    {
        reader = r;
        primary_account = a1;
        primary_writer = w1;
        secondary_account = a2;
        secondary_writer = w2;
        account = primary_account;
        writer = primary_writer;
    }

    public void resetAccount (BankAccount new_account,BankWriter new_writer) {
        account = new_account;
        writer = new_writer;
    }

    public void processTransactions()
    {
        char command = reader.readCommand("Commands (P/S/D/W/T/I/Q): ");

        switch (command) {
            case 'P':
                resetAccount(primary_account, primary_writer);
                break;
            case 'S':
                resetAccount(secondary_account, secondary_writer);
                break;
            case 'Q':
                System.out.println("Quit");
                return;
            case 'D':
                { 
                    double amount = reader.readAmount();
                    if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
                    else writer.setTransaction("deposit error ", amount);
                    break;
                }
            case 'W':
                { 
                    double amount = reader.readAmount();
                    if (account.withdraw(amount)) writer.setTransaction("withdraw $", amount);
                    else writer.setTransaction("withdraw error ", amount);
                    break;
                }
            case 'T':
            	{
                    double amount = reader.readAmount();
                    if (account.withdraw(amount)) 
                    { 
                        writer.setTransaction("withdraw $", amount);
                        if(account == primary_account) resetAccount(secondary_account, secondary_writer); 
                        else resetAccount(primary_account, primary_writer);
                        if(account.deposit(amount))
                        {
                            writer.setTransaction("deposit $", amount);
                        }
                        else
                        {
                            writer.setTransaction("deposit error ", amount);
                        }
                    }
            		else 
                    { 
                        writer.setTransaction("withdraw error ", amount);
                    }
                    if(account == primary_account) resetAccount(secondary_account, secondary_writer); 
                    else resetAccount(primary_account, primary_writer);
                    break;
            	}
            case 'I':
        	{
                double amount = reader.readAmount() / 100 * account.getBalance();
                if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
                else writer.setTransaction("deposit error ", amount);
            	break;
            }
            default:
                writer.setTransaction("invalid commpand: " + command);
        }
        System.out.println("=======================");
        System.out.println("== currently active : " + ((account == primary_account) ? "primary" : "secondary"));
        System.out.println("== primary account : " + BigDecimal.valueOf(primary_account.getBalance()).movePointRight(2).round(MathContext.UNLIMITED).movePointLeft(2));
        System.out.println("== secondary account : " + BigDecimal.valueOf(secondary_account.getBalance()).movePointRight(2).round(MathContext.UNLIMITED).movePointLeft(2));
        System.out.println("=======================");

        this.processTransactions();
    }
}


public class Main {
  public static void main(String[] args) {
    BankReader reader = new BankReader();
    BankAccount primary_account = new BankAccount(0);
    BankWriter primary_writer = new BankWriter(primary_account);
    BankAccount secondary_account = new BankAccount(0);
    BankWriter secondary_writer = new BankWriter(secondary_account);
    AccountController controller = new AccountController(reader,
            primary_account, primary_writer, secondary_account,
            secondary_writer);
    controller.processTransactions();
  }
}

첫 답변의 경우 이율 계산에 있어 소수점 아래의 이율을 계산하지 못합니다. ( 입력이 i 0.1 처럼 들어올 경우 ) 관련 문제를 수정한 코드입니다.

입력에 따라 처음 답변 드린 코드를 사용하셔도 상관 없습니다.

PS. 들어오는 입력 데이터를 명확히 해주시면 더 자세한 답변이 가능합니다.

실행 결과는 다음과 같습니다.

감사합니다.