Please use java language: Implement a class SubmitInfo that simulates a form to
ID: 3566349 • Letter: P
Question
Please use java language:
Implement a class SubmitInfo that simulates a form to receive information about a
commodity.
Supply methods
public void enter(String text)
public void submit(String button)
public boolean registered()
The first input is the name, the second input is the code. The submit method can be called with
arguments "Register" and "Clear". When a user has successfully provided the information and
clicked the register button, the registered method returns true and further input has no effect.
When a user tries to register with an invalid name and code, the form is cleared. Use a
constructor with the expected name and code.
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author NAGLENOVO
*/
public class SubmitInfo
{
private JFrame mainFrame;
private JPanel controlPanel;
private JTextField tf1,tf2;
private JLabel lb1,lb2;
private JButton register,clear;
private String names[] = new String[50];
private int codes[] = new int[50];
private static int index = 0;
public SubmitInfo()
{
prepareGUI();
}
public static void main(String args[])
{
SubmitInfo cc = new SubmitInfo();
cc.showCalculations();
}
public void prepareGUI()
{
mainFrame = new JFrame("Form commodity Using Java Swings");
mainFrame.setSize(400,400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new BorderLayout());
controlPanel = new JPanel(new GridLayout(3,2));
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
public void showCalculations()
{
lb1 = new JLabel("Enter Name ",JLabel.CENTER);
lb2 = new JLabel("Enter Code ",JLabel.CENTER);
tf1 = new JTextField(20);
tf2 = new JTextField(20);
register = new JButton("Register");
register.addActionListener(new MyListener());
clear = new JButton("Clear");
clear.addActionListener(new MyListener());
controlPanel.add(lb1);
controlPanel.add(tf1);
controlPanel.add(lb2);
controlPanel.add(tf2);
controlPanel.add(register);
controlPanel.add(clear);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
public void enter(String text, int code)
{
names[index] = text;
codes[index] = code;
index++;
}
public boolean registred(String name)
{
for(int i = 0; i < names.length; i++)
{
if(names[i].equals(name))
return false;
}
return true;
}
class MyListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String name = tf1.getText();
int code = Integer.parseInt(tf2.getText());
if(e.getSource() == register)
{
if(registred(name))
{
enter(name, code);
JOptionPane.showMessageDialog(controlPanel, "Name and Code is Saved");
}
else
{
JOptionPane.showMessageDialog(controlPanel, "Name or Code is Invalid");
}
}
if(e.getSource() == clear)
{
tf1.setText("");
tf2.setText("");
}
}
}
}