Reputation: 3
I have a class and in the class I have three methods which do the same thing but provide different inputs, So I was wondering if there is any way to make this called smaller.
My code;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
public class test {
public void methodclassA() {
int result = JOptionPane
.showOptionDialog(
null,
"How would you like you insert your data, manually or from a file? ",
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] {
"Manual", "From a File" },
JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Going to call methodA from another class
}
if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = { "Path location:", NameField };
int result2 = JOptionPane.showOptionDialog(null, message,
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
"Locate the file" }, JOptionPane.NO_OPTION);
}
}
public void methodclassB() {
int result = JOptionPane
.showOptionDialog(
null,
"How would you like you insert your data, manually or from a file? ",
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] {
"Manual", "From a File" },
JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Going to call methodB from another class
}
if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = { "Path location:", NameField };
int result2 = JOptionPane.showOptionDialog(null, message,
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
"Locate the file" }, JOptionPane.NO_OPTION);
}
}
public void methodclassC() {
int result = JOptionPane
.showOptionDialog(
null,
"How would you like you insert your data, manually or from a file? ",
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] {
"Manual", "From a File" },
JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Going to call methodB from another class
}
if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = { "Path location:", NameField };
int result2 = JOptionPane.showOptionDialog(null, message,
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
"Locate the file" }, JOptionPane.NO_OPTION);
}
}
}
For example, my three methods in the class are; methodclassA,methodclassB, methodclassC, all of these ask the user for the same input but however each method will call a different method from a different class.
Thanks in advance and I hope I have explained myself clearly.
edit: I forgot to mention before, I have three buttons in my main class which calls each of these three methods. for example my buttonA calls the methodclassA, buttonB calls methodclassB and buttonC calls the methodclassC.
Upvotes: 0
Views: 111
Reputation: 10959
You could just provide an input switch into the method so it would be something like
public void methodCaller(char aSwitcher) {
int result = JOptionPane.showOptionDialog(null, "How would you like you insert your data, manually or from a file? ", "Inserting data",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Manual", "From a File" }, JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
switch(aSwitcher)
{
case 'A':
//Going to call methodA from another class
break;
case 'B':
//Going to call methodB from another class
break;
case 'C':
//Going to call methodC from another class
break;
default:
throw new IllegalArgumentExcpetion("No method defined for option" + aSwitcher);
}
}
else if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = {"Path location:", NameField};
int result2 = JOptionPane.showOptionDialog(null, message, "Inserting data",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok", "Locate the file" }, JOptionPane.NO_OPTION);
}
}
Possibly a better way to do it but this at least will save all the code duplication.
Then to call just make these replacements
methodclassA(); -> methodCaller('A');
methodclassB(); -> methodCaller('B');
methodclassC(); -> methodCaller('C');
Also advantage of this is that you can add 'D', 'E', 'F' and all you have to modify is to add those cases to the switch.
Upvotes: 3
Reputation: 2534
I know they are all in the same class now, but depending on how they are called you could refactor and use the Template Method Design Pattern.
Upvotes: 0