Mengenal Perintah Java (Scanner dan Library)

USING DIALOG BOX

Entering Input and Showing Output in a Dialog Box 

The programs presented thus far display output in the command window. Many applications use windows or dialog boxes to display output. Web browsers such as Firefox, Internet Explorer, Chrome and Safari display web pages in their own windows. E-mail programs allow you to type and read messages in a window. Typically, dialog boxes are windows in which programs display importantmessages to users.

This program contains a class named Account that maintains the balance of a bank account. A typical bank services many accounts, each with its own balance.

Source Code
public class Account
{
    private double balance;
    
    public Account (double initialBalance)
    {
        if (initialBalance > 0.0)
            balance = initialBalance;
    }
    
    public void credit (double amount)
    {
        balance = balance + amount;
    }
    
    public double getBalance()
    {
        return balance;
    }
}


Without Dialog Box

Source Code 
import java.util.Scanner;

public class AccountTest
{
    public static void main (String[] args)
    {
        Account account1 = new Account (50.00);
        Account account2 = new Account (-7.53);
        
        System.out.printf ("account1 balance : $%.2f\n", account1.getBalance());
        System.out.printf ("account2 balance : $%.2f\n\n", account2.getBalance());
        
        Scanner input = new Scanner (System.in);
        double depositAmount;
        
        System.out.print ("Enter deposit amount for account1 : ");
        depositAmount = input.nextDouble();
        System.out.printf ("\nadding %.2f to accout1 balance\n\n", depositAmount);
        account1.credit (depositAmount);
        
        System.out.printf ("account1 balance : $%.2f\n", account1.getBalance());
        System.out.printf ("account2 balance : $%.2f\n\n", account2.getBalance());
        
        System.out.printf ("Enter dposit amount for account2 : ");
        depositAmount = input.nextDouble();
        System.out.printf ("\nadding %.2f to account2 balance\n\n", depositAmount);
        account2.credit (depositAmount);
        
        System.out.printf ("account1 balance : $%.2f\n", account1.getBalance());
        System.out.printf ("account2 balance : $%.2f\n", account2.getBalance());
    }
}

Output


With Dialog Box

 Source Code
 
import javax.swing.JOptionPane;

public class AccountTestDialogBox
{
    public static void main (String[] args)
    {
        Account account1 = new Account (50.00);
        Account account2 = new Account (-7.53);
        
        String output1 = String.format ("account1 balance : $%.2f\naccount2 balance : $%.2f\n\n", account1.getBalance(), account2.getBalance());
        JOptionPane.showMessageDialog (null, output1);
        
        String input1 = JOptionPane.showInputDialog ("Enter deposit amount for account1 : ");
        Double input1a = Double.parseDouble (input1);
        account1.credit (input1a); 
        String message1 = String.format ("\nadding %s to account1 balance\n\n", input1);
        JOptionPane.showMessageDialog (null, message1);
        
        String output2 = String.format ("account1 balance : $%.2f\naccount2 balance : $%.2f\n\n", account1.getBalance(), account2.getBalance());
        JOptionPane.showMessageDialog (null, output2);
        
        String input2 = JOptionPane.showInputDialog ("Enter deposit amount for account2 : ");
        Double input2a = Double.parseDouble (input2);
        account2.credit (input2a);
        String message2 = String.format ("\nadding %s to account2 balance\n\n", input2);
        JOptionPane.showMessageDialog (null, message2);
        
        String output3 = String.format ("account1 balance : $%.2f\naccount2 balance : $%.2f\n", account1.getBalance(), account2.getBalance());
        JOptionPane.showMessageDialog (null, output3);
    }
}


Output






Komentar

Postingan Populer