2015-05-18 26 views
-1

餘額應取初始餘額並根據tcode減去或添加transamt(1是取款,2是存款)。餘額,服務費用和小數格式的輸出錯誤

服務費爲存款0.10美元,支票0.15美元。如果支票強制將餘額降至500美元以下,則會收取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; 
     } 

} 

回答

0

你有沒有嘗試過這樣的:

if (amount >= 0.0) { 
    showMessageDialog(amount); 
} else { 
    showMessageDialog('(' + Math.abs(amount) + ')'); 
} 

即簡化,但它跨越有想法。