In: Computer Science
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Defines class Customer
class Customer
{
// Instance variables to store customer information
String customerName;
String clientNumber;
// Parameterized constructor
Customer(String cna, String cn)
{
customerName = cna;
clientNumber = cn;
}
// Method to return customer information
public String toString()
{
return "\n Name: " + customerName + "\n Client Number: " + clientNumber;
}
}// End of class Customer
// Defines class Discount derived from super class Applet
// implements interface ActionListener
public class Discount extends JApplet implements ActionListener
{
// Declares component objects
JLabel cnaL, cnL, regL;
JTextField cnaT, cnT;
JCheckBox daily, weekly, monthly, yearly;
ButtonGroup bg;
JButton addB;
// Declares JPanel objects
JPanel jp1, jp2, jp3, jp4, mainJP;
// Declares an array of object of class Customer
Customer []customers;
// To store number of customers
int numberOfCustomer;
// Overrides init() method of Applet
public void init()
{
// Creates the array of object of type Customer
customers = new Customer[10];
// Initializes number of objects
numberOfCustomer = 0;
// Creates panels
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
mainJP = new JPanel();
// Creates labels
cnaL = new JLabel("Enter customer name: ");
cnL = new JLabel("Enter customer number: ");
regL = new JLabel("Registration: ");
// Creates text fields
cnaT = new JTextField(20);
cnT = new JTextField(20);
// Creates radio buttons
daily = new JCheckBox("Daily $5");
weekly = new JCheckBox("Weekly $ 20");
monthly = new JCheckBox("Monthly $ 40");
yearly = new JCheckBox("Yearly $ 80");
// Creates button group
bg = new ButtonGroup();
// Adds radio buttons to button group
bg.add(daily);
bg.add(weekly);
bg.add(monthly);
bg.add(yearly);
// Creates the button
addB = new JButton("Add");
// Registers the button for event handling
addB.addActionListener(this);
// Adds the label and text field to panel 1
jp1.add(cnaL);
jp1.add(cnaT);
jp1.add(cnL);
jp1.add(cnT);
// Sets the layout to 2 rows and 2 columns
jp1.setLayout(new GridLayout(2, 2));
// Adds the registration label to panel 2
jp2.add(regL);
// Adds the radio buttons to panel 3
jp3.add(daily);
jp3.add(weekly);
jp3.add(monthly);
jp3.add(yearly);
// Sets the layout to 2 rows and 2 columns
jp3.setLayout(new GridLayout(2, 2));
// Adds button to panel 4
jp4.add(addB);
// Adds panels to main panel
mainJP.add(jp1);
mainJP.add(jp2);
mainJP.add(jp3);
mainJP.add(jp4);
// Sets the layout to 4 rows and one column
mainJP.setLayout(new GridLayout(4, 1));
// Adds main panel to applet
add(mainJP);
// Sets the size of the Applet
setSize(350, 150);
}// End of method
// Method to return true if parameter number is available in
// Array of object customers (Existing customer)
// Otherwise returns false (New customer)
boolean isAvailable(String name, String number)
{
// Loops till number of customers
for(int c = 0; c < numberOfCustomer; c++)
// Checks if current client number is equals to parameter number
if(customers[c].clientNumber.equalsIgnoreCase(number) &&
customers[c].customerName.equalsIgnoreCase(name))
// Returns true for Existing customer
return true;
// Returns false for New customer
return false;
}// End of method
// Override actionPerformed of ActionListener interface
public void actionPerformed(ActionEvent e)
{
double amount = 0;
double discount;
String type = "";
String customer = "";
// try block begins
try
{
// Extracts the customer name
String name = cnaT.getText();
// Checks if name is blank then throws an exception
if(name.isEmpty())
throw new NullPointerException("Please enter Customer Name.");
// Extracts the customer number
String no = cnT.getText();
// Checks if number is blank then throws an exception
if(no.isEmpty())
throw new NullPointerException("Please enter Customer Number.");
// Checks if all three radio button not selected then throws an exception
if(!daily.isSelected() && !weekly.isSelected() &&
!monthly.isSelected() && !yearly.isSelected())
throw new NullPointerException("Please select Registration Type.");
// Checks if daily radio button selected
// then sets the amount and type
if(daily.isSelected())
{
amount = 5;
type = "Daily";
}
// Otherwise checks if weekly radio button selected
// then sets the amount and type
else if(weekly.isSelected())
{
amount = 20;
type = "Weekly";
}
// Otherwise checks if monthly radio button selected
// then sets the amount and type
else if(monthly.isSelected())
{
amount = 40;
type = "Monthly";
}
// Otherwise yearly radio button selected
// then sets the amount and type
else
{
amount = 80;
type = "Yearly";
}
// Calls the method to check for existing customer or new customer
// If existing customer then sets the discount and customer type
if(isAvailable(name, no))
{
discount = 10;
customer = "Existing Customer";
}
// Otherwise new customer then sets the discount and customer type
else
{
discount = 5;
customer = "New Customer";
}
// Subtracts the discount
amount -= (amount * discount) / 100.0;
// Checks if number of customer is equals to maximum length
// then throws exception
if(numberOfCustomer == customers.length)
throw new IndexOutOfBoundsException("Cannot add more customer. " +
"Reached maximum.");
// Otherwise creates an object of Customer using parameterized
// constructor and assigns it to number of customer index position
else
customers[numberOfCustomer++] = new Customer(name, no);
// Displays information
JOptionPane.showMessageDialog(null, customers[numberOfCustomer-1] +
"\n Customer Type: " + customer + "\n Registration Type: " + type +
"\n Discount: " + discount + "% \n Amount: $" + amount,
"Payment", JOptionPane.INFORMATION_MESSAGE);
cnaT.setText("");
cnT.setText("");
daily.setSelected(false);
weekly.setSelected(false);
monthly.setSelected(false);
yearly.setSelected(false);
}
catch(NullPointerException ne)
{
JOptionPane.showMessageDialog(null, ne.getMessage());
}
catch(IndexOutOfBoundsException ie)
{
JOptionPane.showMessageDialog(null, ie.getMessage());
}
catch(RuntimeException re)
{
JOptionPane.showMessageDialog(null, re.getMessage());
}
}// End of method
}// End of class