Write the following code in the class:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
public class Imei {
JFrame frame;
JButton button;
JTextField field;
JLabel label;
JLabel warninglabel;
Box panel;
public static void main(String[] args) {
Imei hl=new Imei();
hl.gui();
}
public void gui(){
panel = Box.createVerticalBox();
frame=new JFrame();
button=new JButton("Click");
field=new JTextField(15);
field.putClientProperty("JComponent.sizeVariant", "mini");
label=new JLabel("Enter the IMEI Number");
warninglabel=new JLabel("");
//adding contents to frame
panel.add(label);
panel.add(field);
panel.add(warninglabel);
panel.add(button);
frame.getContentPane().add(BorderLayout.NORTH,panel);
frame.setVisible(true);
frame.setSize(300,300);
button.addActionListener(new buttonAction());
}
public class buttonAction implements ActionListener{
public void actionPerformed(ActionEvent ev) {
int sum=0;
String ImeiNo=field.getText();
if (ImeiNo.length()!=15){
warninglabel.setText("IMEI Number should contain 15 characters");
}else
{
boolean errorflag = false;
for(int i=0;i<=14;i++){
//getting ascii value for each character
char c=ImeiNo.charAt(i);
int number=c;
//Assigning number values to corrsponding Ascii value
if (number<48 || number>57){
warninglabel.setText("Enter only numerals");
errorflag = true;
break;
}else
{
switch(number){
case 48: number=0;break;
case 49: number=1;break;
case 50: number=2;break;
case 51: number=3;break;
case 52: number =4;break;
case 53:number =5;break;
case 54:number=6;break;
case 55:number=7;break;
case 56:number=8;break;
case 57:number=9;break;
}
//Double the even number and divide it by 10. add quotient and remainder
if ((i+1)%2==0){
number=number*2;
number=number/10+number%10;
}
sum=sum+number;
}
}
// Check the error flag to avoid overWriting of Warning Lable
if(!errorflag){
if(sum%10==0){
warninglabel.setText("Valid");
}
else
{
warninglabel.setText("Invalid");
}
}
}
}
}
}