天平,服务费和小数格式的输出错误

时间:2015-05-18 18:00:20

标签: java swing

余额应取初始余额并根据tcode减去或添加transamt(1表示取款,2表示存款)。

存款的服务费为0.10美元,支票为0.15美元。如果检查强制余额降至500.00美元以下,则会收取5.00美元的服务费,但这是第一次发生这种情况。只要余额低于$ 50.00,程序就会打印一条警告信息。如果支票导致负余额,则应评估10.00美元的额外服务费。

余额的十进制格式应为“00000。##”表示正数和“(00000。##)”。我不知道如何把第二个。

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Main {

    //global variables
    CheckingAccount c;
    public double balance;
    public double totalServiceCharge;


    public static void main (String[] args)
    {

        // defines local variables
        String iBalance, transATM, message, transCode;

        int tcode;

        double transatm, ibalance, currentservicecharge, fee = 0;

        // get initial balance from the user
        // perform in a loop until the trans code = 0
        // get the trans code from the user and process it with appropriate helper method
        // When loop ends show final balance to user.

         iBalance = JOptionPane.showInputDialog ("Enter the initial balance: ");

         ibalance = Double.parseDouble(iBalance);

         CheckingAccount c = new CheckingAccount(ibalance);

         DecimalFormat df = new DecimalFormat("#####.00");

         do 
       {  
         transCode = JOptionPane.showInputDialog ("Enter the trans code: ");

         tcode = Integer.parseInt(transCode);

         if(tcode == 1)
         {  
            transATM = JOptionPane.showInputDialog ("Enter the trans atm: ");

            transatm = Double.parseDouble(transATM);

            currentservicecharge = 0.15; 

            message = "Transaction: Check in amount of $" + transatm + "\n" +
                      "Current balance: " + c.getBalance() + "\n" +
                      "Service Charge: Check --- charge $ " + currentservicecharge + "\n" +
                      "Service Charge: " + fee + "\n" +
                      "Total Service Charge: " + c.getServiceCharge();
            JOptionPane.showMessageDialog (null, message + df);
         }
         else if(tcode == 2)
         {  
            transATM = JOptionPane.showInputDialog ("Enter the trans atm: ");

            transatm = Double.parseDouble(transATM);

            currentservicecharge = 0.10;  

            message = "Transaction: Deposit in amount of $" + transatm + "\n" +
                      "Current balance: " + c.getBalance() + "\n" +
                      "Service Charge: Check --- charge $ " + currentservicecharge + "\n" +
                      "Service Charge: " + fee + "\n" +
                      "Total Service Charge: " + c.getServiceCharge();
            JOptionPane.showMessageDialog (null, message + df);
         }
       }
       while (tcode != 0);


       message = "Transaction: End" + "\n" +
                 "Current Balance: " + ibalance + "\n" +
                 "Total Service Charge: " + c.getServiceCharge() + "\n" +
                 "Final Balance: " +  c.getBalance();

       JOptionPane.showMessageDialog (null, message + df);  

    }

        public int getTransCode(int tcode)
        {
            return tcode;
        }

        public double getTransAmt(double transatm)
        {
            return transatm;
        }

        public double processCheck(double currentservicecharge)
        {
            return currentservicecharge;
        }

        public double processDeposit(double currentservicecharge)
        {
            return currentservicecharge;
        }

        public double processFee(double fee)
        {
            return fee;
        }

           public void setServiceCharge(double currentServiceCharge)
        {
            totalServiceCharge += currentServiceCharge;            
        }
            public void penatlyCharge(double fee, double currentservicecharge, double transatm, String message, CheckingAccount c)
        {

            if(c.getBalance() < 500.00)
            {
                fee = 5.00;
                currentservicecharge += fee;
                message = "Service Charge: " + fee + "\n" +
                          "Warning : Balance below $500" ;
            }
            else if(c.getBalance() < 0.00)
            {
                fee = 10.00;
                currentservicecharge += fee;
                message = "Service Charge: " + fee + "\n" +
                          "Warning : Balance below $0";
            }
            else if(c.getBalance() < 50.00)
                message = "Service Charge: " + fee + "\n" +
                          "Warning : Balance below $50" ;

        }
}


public class CheckingAccount {

    private double balance;
    private double totalServiceCharge;

    public CheckingAccount()
    {
        balance = 0;
        totalServiceCharge = 0;
    }


    public CheckingAccount(double ibalance)
    {
        balance = ibalance;
    }

    public void setBalance(double transamt, int tcode)
    {
        if(tcode == 1)
        {
           double newBalance = balance - transamt; 
           balance = newBalance;
        }
        else if(tcode == 2)
        {
           double newBalance = balance + transamt; 
           balance = newBalance;
        }
    }

    public double getBalance()
    {  
       return balance;
    }

    public double getServiceCharge()
        {
            return totalServiceCharge;
        } 

}

1 个答案:

答案 0 :(得分:0)

你有没有试过像:

https://

这是简化的,但它可以解决这个问题。