Question

In: Computer Science

Make a JAVA program: java.applet java.awt java.awt.evet java.swing.JOptionpane decisional structure (IF, ELSE IF), message box, checkbox,...

Make a JAVA program:
java.applet
java.awt
java.awt.evet
java.swing.JOptionpane

decisional structure (IF, ELSE IF), message box, checkbox, number format.
Data entry:
Customer name
Client number
Discount: New Customer 10% Existing Customer 5%
Registration: Daily $ 5 Weekly $ 20 Monthly $ 40 Yearly $ 80
Calculate the registration cost in a function according to the discount and type of registration
It should show the cost and a message box showing the selected alternatives; Result boxes must be visible when the button is pressed and cannot be altered. You must contemplate the different errors, use "try and catch". Create color and apply to shape.
Add button to clear text boxes and variables.

Make a JAVA program decisional structure, message box, matching box, number format.
Entry of Data:
Customer Name
Customer Number
Discount: New Customer 10% Existing Customer 5%
Registration: Daily $5 Weekly $20 Monthly $40 Annual $80
Calculate in a function the cost of registration according to the discount and type of registration
You must display the cost and a message box showing the selected alternatives; the result boxes must be visible when the button is pressed and cannot be altered. You must contemplate the different errors, use " try and catch". Create color and apply to the form.
Add button to clear text boxes and variables.

Solutions

Expert Solution

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

Sample Output:


Related Solutions

java.applet java.awt java.awt.event java.swimg.JOptionPane Make a JAVA program decisional structure, message box, matching box, number format....
java.applet java.awt java.awt.event java.swimg.JOptionPane Make a JAVA program decisional structure, message box, matching box, number format. Entry of Data: Customer Name Customer Number Discount: New Customer 10% Existing Customer 5% Registration: Daily $5 Weekly $20 Monthly $40 Annual $80 Calculate in a function the cost of registration according to the discount and type of registration You must display the cost and a message box showing the selected alternatives; the result boxes must be visible when the button is pressed and...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if -...
Write one Java program and satisfy the following requirements: Rewrite the following if -else if - else statement as an equivalent switch statement.   if (letter == 'A' | | letter == 'a')         System.out.println("Excellent");         else if (letter == 'B' | | letter == 'b')                System.out.println("You can do better");         else if (letter == 'C' | | letter == 'c')                System.out.println("Try harder");         else if (letter == 'D' | | letter == 'd')                System.out.println("Try much harder");        ...
A Java program that deciphers each message by building the string. Each line of the text...
A Java program that deciphers each message by building the string. Each line of the text file contains either a word or a command followed by a forward slash and the requirements of the command. A Stack Implementation is needed as. As you read the data from the text file, you need to know if it is a new phrase or command. The commands are: i – an insert followed by the letters, numbers, or symbols that need to be...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on page 108.  Use the format specified earlier (initial block of comments with TCC logo, name, etc) Display instructions so that the user understands the purpose of the program and what to enter. Display the results in increasing (non-decreasing) order. Run the program for the following 6 test cases.Turn in a printout of the program and printouts of the 6 test cases.(The result should...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT