I'm writing an animal shelter program, which keeps a database of different classes of animals (dog, cat, monkey). Functions that create the form and add the animals are very similar (difference is two questions and the rest is the same). Is it possible to divide this functions into parts so that the program code is not repeated?
You can see the differences between the functions here at Diffchecker. They have a lot in common.
private void admitDog() {
boolean lb = false;
boolean nw = false;
String[] options = {"OK"};
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
JLabel nameLabel = new JLabel("What is his/her name?");
JTextField nameField = new JTextField(10);
panel.add(nameLabel);
panel.add(nameField);
JLabel favLabel = new JLabel("What is his/her favourite food?");
JTextField favField = new JTextField(10);
panel.add(favLabel);
panel.add(favField);
JLabel numTimesLabel = new JLabel("How many times is he/she fed a day?"); //tu tylko cyfry
JTextField numTimesField = new JTextField(10);
panel.add(numTimesLabel);
panel.add(numTimesField);
JLabel needWalkLabel = new JLabel("Does he need walk?");
JRadioButton needWalkYes = new JRadioButton("Yes");
JRadioButton needWalkNo = new JRadioButton("No");
ButtonGroup needWalkGroup = new ButtonGroup();
needWalkGroup.add(needWalkYes);
needWalkGroup.add(needWalkNo);
needWalkYes.setSelected(true);
panel.add(needWalkLabel);
panel.add(needWalkYes);
panel.add(needWalkNo);
JLabel likeBonesLabel = new JLabel("Does he like bones?");
JRadioButton likeBonesYes = new JRadioButton("Yes");
JRadioButton likeBonesNo = new JRadioButton("No");
ButtonGroup likeBonesGroup = new ButtonGroup();
likeBonesGroup.add(likeBonesYes);
likeBonesGroup.add(likeBonesNo);
likeBonesYes.setSelected(true);
panel.add(likeBonesLabel);
panel.add(likeBonesYes);
panel.add(likeBonesNo);
int selectedOption = JOptionPane.showOptionDialog(null, panel,
"Enter informations", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options , options[0]); //dODAMY jeszcze cancel
if(selectedOption == 0)
{
String name = nameField.getText();
String fav = favField.getText();
int numTimes = Integer.parseInt(numTimesField.getText());
if(likeBonesYes.isSelected())
lb=true;
if(needWalkYes.isSelected())
nw=true;
ArrayList<Owner> owners = getOwners();
Dog newDog = new Dog(name, owners, lb, fav, numTimes, nw);
kennel.addAnimal(newDog);
}
}
private void admitCat() {
boolean sr = false;
String[] options = {"OK"};
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
JLabel nameLabel = new JLabel("What is his/her name?");
JTextField nameField = new JTextField(10);
panel.add(nameLabel);
panel.add(nameField);
JLabel favLabel = new JLabel("What is his/her favourite food?");
JTextField favField = new JTextField(10);
panel.add(favLabel);
panel.add(favField);
JLabel numTimesLabel = new JLabel("How many times is he/she fed a day?"); //tu tylko cyfry
JTextField numTimesField = new JTextField(10);
panel.add(numTimesLabel);
panel.add(numTimesField);
JLabel shareRunLabel = new JLabel("Does he share run?");
JRadioButton shareRunYes = new JRadioButton("Yes");
JRadioButton shareRunNo = new JRadioButton("No");
ButtonGroup shareRunGroup = new ButtonGroup();
shareRunGroup.add(shareRunYes);
shareRunGroup.add(shareRunNo);
shareRunYes.setSelected(true);
panel.add(shareRunLabel);
panel.add(shareRunYes);
panel.add(shareRunNo);
int selectedOption = JOptionPane.showOptionDialog(null, panel,
"Enter informations", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options , options[0]); //dODAMY jeszcze cancel
if(selectedOption == 0)
{
String name = nameField.getText();
String fav = favField.getText();
int numTimes = Integer.parseInt(numTimesField.getText());
if(shareRunYes.isSelected())
sr=true;
ArrayList<Owner> owners = getOwners();
Cat newCat = new Cat(name, owners, sr, fav, numTimes);
kennel.addAnimal(newCat);
}
}