자바 코드 질문

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

  • “T 금액” 을 명령어로 입력받아서 현재 활성화 된 계좌에서 비활성 계좌로 금액만큼 이체 (transfer)

  • “I 이율” 을 명령어로 입력받아서, 현재 활성화 된 계좌의 금액을 이율(interest)만큼 증가

  • 이율은 0~1 범위의 실수이다.

  • 이율은 소수점 두자리까지만 인정한다. 예, 0.055 => 0.05

  • 이율만큼 증가한다는 것은 (금액 * 이율) 만큼 증가시킨다는 뜻이다.

다음의 명령문들을 사용하여 성공/실패할 경우 메세지를 출력한다.

  • 이체에 성공할 경우: writer.setTransaction(“transfer $”, 금액);

  • 이체에 실패할 경우: writer.setTransaction(“transfer error”);

  • 이율증가에 성공할 경우: writer.setTransaction(“interest $”, 금액);

  • 이율증가에 실패할 경우: writer.setTransaction(“interest error”);
    이 기능을 주석문 쪽에 짜야하는데 잘 모르겠어요 도와주세요 ㅠ

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;
    }

    public double readInterest()
    {
        double answer = 0;
        String s = input_line.substring(1, input_line.length());
        if(s.length() > 0)
        {
            int check = (int)(new Double(s).doubleValue() * 100);
            if(check >= 0 && check <= 100)
            {
                answer = check / 100.0;
            }
            else
            {
                System.out.println("invalid interest value");
                answer = -1;
            }
        }
        else
        {
            System.out.println("invalid command - input interest: 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':
            {
                BankAccount inactive_account;
                if(account == primary_account) inactive_account = secondary_account;
                else inactive_account = primary_account;

                int amount = reader.readAmount();
                if (account.withdraw(amount)) 
                {
                    inactive_account.deposit(amount);
                    writer.setTransaction("transfer $", amount);
                }
                else 
                {
                    writer.setTransaction("transfer error");
                }
                break;
            }
            case 'I':
        	{
                double b = account.getBalance() / 100.0;
                double i = reader.readInterest();
                int amount = (int)(b * i * 100);

                if(account.deposit(amount))
                {
                    writer.setTransaction("interest $", amount);
                }
                else
                {
                    writer.setTransaction("interest error");
                }
            	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();
  }
}

요 근래 문제랑 코드만 툭 던져놓고 도와주세요 ㅠ 식으로 질문을 많이 하시는데요,

기본적으로 도움을 얻고자 한다면 상대에 대한 존중이 있어야 하지 않을까요.

길게 쓴다고 뭐 원하는 내용이 아니니까 흘려버릴테니 간단하게만 얘기하겠습니다.

여기분들 호구 아닙니다.

2 Likes

4 Likes

앗! 유명인이다!!!

:smirk: :smile:

죄송해요 다른 문제들은 제가 짜보고 모르는 부분을 여쭤봤는데 이거는 너무 어렵네요

처음 답변 드린 코드 그대로 사용하시면 됩니다.

처음에 질문 하신 글을 보면, 과제 코드를 한번도 안 돌려보고 바로 올리신 거 같은데 다음에 질문 올리실 때는 최소한 한번 정도는 돌려보고 과제에 대해 고민하신 다음에 올려주세요.

츄라이

2 Likes

알겠습니다 최대한 트라이 하고 여쭤볼게요

1 Like