You can create a lookup map for all the customers. You can even extend this to add and remove customers.
String username = textField.getText().toLowerCase();
if (customerMap.containsKey(username)) {
output.setText(customerMap.get(username).toString());
} else {
output.setText("Not found!");
}
Example
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class App implements Runnable {
private static class Customer {
private String userName;
private boolean regular;
private boolean payAhead;
private boolean loyal;
private double amountOfStorage;
public Customer(String userName, boolean regular, boolean payAhead, boolean loyal, double amountOfStorage) {
this.userName = userName;
this.regular = regular;
this.payAhead = payAhead;
this.loyal = loyal;
this.amountOfStorage = amountOfStorage;
}
@Override
public String toString() {
return String.format("{ userName: %s, regular: %s, payAhead: %s, loyal: %s, amountOfStorage: %s }",
userName, regular, payAhead, loyal, amountOfStorage);
}
}
private static class MainPanel extends JPanel {
private static final long serialVersionUID = -1911007418116659180L;
private static Map<String, Customer> customerMap;
static {
customerMap = new HashMap<String, Customer>();
customerMap.put("kyle", new Customer("Kyle", true, false, false, 1000.00));
customerMap.put("andrew", new Customer("Andrew", false, true, false, 0.00));
customerMap.put("connor", new Customer("Connor", false, false, true, 5000.00));
}
public MainPanel() {
super(new GridBagLayout());
JTextField textField = new JTextField("", 16);
JButton button = new JButton("Check");
JTextArea output = new JTextArea(5, 16);
button.addActionListener(new AbstractAction() {
private static final long serialVersionUID = -2374104066752886240L;
@Override
public void actionPerformed(ActionEvent e) {
String username = textField.getText().toLowerCase();
if (customerMap.containsKey(username)) {
output.setText(customerMap.get(username).toString());
} else {
output.setText("Not found!");
}
}
});
output.setLineWrap(true);
addComponent(this, textField, 0, 0, 1, 1);
addComponent(this, button, 1, 0, 1, 1);
addComponent(this, output, 0, 1, 1, 2);
}
}
protected static void addComponent(Container container, JComponent component, int x, int y, int cols, int rows) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = cols;
constraints.gridwidth = rows;
container.add(component, constraints);
}
@Override
public void run() {
JFrame frame = new JFrame();
MainPanel panel = new MainPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new App());
}
}