Answer to Question #196734 in Java | JSP | JSF for Bernice

Question #196734

https://www.chegg.com/homework-help/questions-and-answers/question-2-marks-20-organisation-work-asked-create-interactive-application-assist-allocati-q74111994


1
Expert's answer
2021-05-23T12:19:18-0400
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Random;

public class PhoneNumber extends JFrame {

    public PhoneNumber() {
        String[] providers = {"Vodacom", "MTN", "Cell C"};
        String[] start = {"072", "083", "084"};
        String[] ordinals = {"first", "second", "third"};
        Random random = new Random();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JLabel[] labels = new JLabel[3];
        JTextField[] textFields = new JTextField[3];
        for (int i = 0; i < 3; i++) {
            labels[i] = new JLabel("Enter " + ordinals[i] + " name:");
            textFields[i] = new JTextField();
            panel.add(labels[i]);
            panel.add(textFields[i]);
        }
        JButton button = new JButton("Generate");
        button.addActionListener((ActionEvent ae) -> {
            String zeroO = "000";
            String zeroT = "0000";
            String one;
            String two;
            StringBuilder message = new StringBuilder("CELL PHONE NUMBER GENERATOR\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
            int[] indexes = new int[textFields.length];
            boolean isValid;
            for (int i = 0; i < indexes.length; i++) {
                do {
                    isValid = true;
                    indexes[i] = random.nextInt(indexes.length);
                    for (int j = i - 1; j >= 0; j--) {
                        if (indexes[i] == indexes[j]) {
                            isValid = false;
                            break;
                        }
                    }
                } while (!isValid);
                one = Integer.toString(random.nextInt(1000));
                two = Integer.toString(random.nextInt(10000));
                message.append(textFields[i].getText()).append(" will be on the ").append(providers[indexes[i]]);
                message.append(" network with phone number ").append(start[indexes[i]]).append(" ");
                message.append(zeroO.substring(one.length())).append(one).append(" - ").append("(");
                message.append(zeroT.substring(two.length())).append(two).append(")\n");
            }
            JOptionPane.showMessageDialog(this, message.toString(), "Network Provider Assignment",
                    JOptionPane.INFORMATION_MESSAGE);
        });
        panel.add(button);
        add(panel);
        setSize(340, 200);
        setTitle("Phone Number Generator");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new PhoneNumber();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS