1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
       Project: Chapter 4
       Author: Shady Tyrant
       Date: 4/28/09
*/
 
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
 
public class Commission
{
       public static void main(String[] args)
       {
              //declare class varables
              double dollars, answer;
              int empCode;
 
              //call methods()
              dollars = getSales();
              empCode = getCode();
              answer = getComm(dollars,empCode);
              output(answer, dollars);
              finish();
       }
 
       //The getSales() method asks the user to input a dollar amount and validates it
       public static double getSales()
       {
              //declare method variables
              double sales = 0.0;
              boolean done = false;
 
              //loop while not done
              while(!done)
              {
                     String answer = JOptionPane.showInputDialog(null"Enter the sales amount \n(do not use commas or dollar signs)\n or click Cancel to exit:");
 
                     if (answer == null) finish();
 
                     try
                     {
                            sales = Double.parseDouble(answer);
                            if (sales <= 0) throw new NumberFormatException();
                            else done = true;
                     }
                     catch(NumberFormatException e)
                     {
                            JOptionPane.showMessageDialog(null,"Your entry was not in the proper format.",
                            "Error",JOptionPane.INFORMATION_MESSAGE);
                     }
 
                     sales = Double.parseDouble(answer);
              }
              return sales;
       }
 
       //The getCode() method retrieves a code from the user and validates it.
       public static int getCode()
       {
              //declare method variables
              int code = 0;
              boolean done = false;
 
              //loop while not done
              while(!done)
              {
                     try
                     {
                            String message = "Enter the commission code:" +
                            "\n\n1) Telephone Sales\n2) In-Store Sales\n3) Outside Sales\n\n";
 
                            code = Integer.parseInt(JOptionPane.showInputDialog(null,message));
 
                            //test for valid codes 1, 2, or 3
                            if (code<1 || code>3) throw new NumberFormatException();
                            else done = true;
                     }
                     catch(NumberFormatException e)
                     {
                            JOptionPane.showMessageDialog(null,"Please enter a 1, 2, or 3",
                            "Error",JOptionPane.INFORMATION_MESSAGE);
                     }
              }
              return code;
       }
 
       public static double getComm(double employeeSales, int employeeCode)
       {
              double commission = 0.0;
 
              switch(employeeCode)
              {
                     case 1:
                            commission = .10 * employeeSales;
                            break;
 
                     case 2:
                            commission = .14 * employeeSales;
                            break;
 
                     case 3:
                            commission = .18 * employeeSales;
                            break;
              }
              return commission;
       }
 
       //the output method displays the commission and sales.
       public static void output(double commission, double sales)
       {
              DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
 
              JOptionPane.showMessageDialog(null,"Your commission on sales of "+ twoDigits.format(sales) + " is " + twoDigits.format(commission),"Commission Totals",JOptionPane.INFORMATION_MESSAGE);
       }
 
       //finish() method ends the program
       public static void finish()
       {
              System.exit(0);
       }
}