Question

In: Computer Science

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 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


Related Solutions

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...
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...
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...
java program plz number = 308.2 number = 14.9 number = 7.4 number = 2.8 number...
java program plz number = 308.2 number = 14.9 number = 7.4 number = 2.8 number = 3.9 number = 4.7 number = -15.4 number = 2.8 Sum = 329.3
Java Counter Program I can't get my program to add the number of times a number...
Java Counter Program I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2. My Code: import java.util.Scanner; import java.util.Random;    import java.lang.*;       public class Dice    {               public static void main(String[] args)        {            Scanner in = new Scanner(System.in);            int Continue = 1;            //randomnum = new Random();           ...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message 1.Caller dials main number of voice mail system 2.System speaks prompt 3.User types extension number 4.System speaks 5.Caller speaks message Variation #1 3.1 In step 3, user enters invalid extension number 3.2 Voice mail system speaks 3.3 Continue with step 2. What the code Does: adds a new regular task, delete a task , show all tasks, and show regular tasks. my code: import...
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.
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT