0

Im really not sure as how to word this question. But im gonna try my best here. Bear with me if you could :)

I have a database with 3 tables (that i am dealing with right now). Fortunately they all have the same amount of columns. Im trying to input values into them using a "popup" form. (Not sure how to do that, but im using this link here as a guideline, and hoping it works)

Here is the code i have written for that method so far..

public form(int option, String val1, String val2, String val3, String val4, String val5)
{
    val1 = null;
    val2 = null;
    val3 = null;
    val4 = null;
    val5 = null;

    JTextField val1Field = new JTextField(20);
    JTextField val2Field = new JTextField(20);
    JTextField val3Field = new JTextField(20);
    JTextField val4Field = new JTextField(20);
    JTextField val5Field = new JTextField(20);

    String name;
    String lbl1 = null;
    String lbl2 = null;
    String lbl3 = null; 
    String lbl4 = null;
    String lbl5 = null;

    switch(option)
    {
    case 1: //if customer
        name = "Customer Information";
        lbl1 = "Customer No:";
        lbl2 = "Customer Name:";
        lbl3 = "Company Name:";
        lbl4 = "Contact Number: ";
        lbl5 = "Discount Rate:";

    case 2: //if item
        name = "Item Information";
        lbl1 = "Item No:";
        lbl2 = "Item Name:";
        lbl3 = "Cost Price:";
        lbl4 = "Selling Price: ";
        lbl5 = "Stock:";

    case 3: //if user
        name = "Staff Information";
        lbl1 = "Staff ID:";
        lbl2 = "Full Name:";
        lbl3 = "Username:";
        lbl4 = "Password: ";
        lbl5 = "adminusercheck:";

    default:
        JOptionPane.showMessageDialog(alphaPOS,
                "Something went wrong! Try again!",
                "ERROR",
                JOptionPane.ERROR_MESSAGE);

    }

    JPanel formPanel = new JPanel();

    formPanel.add(new JLabel(lbl1));
    formPanel.add(val1Field);
    formPanel.add(Box.createHorizontalStrut(15)); // a spacer
    formPanel.add(new JLabel(lbl2));
    formPanel.add(val2Field);
    formPanel.add(Box.createHorizontalStrut(15)); // a spacer
    formPanel.add(new JLabel(lbl3));
    formPanel.add(val3Field);
    formPanel.add(Box.createHorizontalStrut(15)); // a spacer
    formPanel.add(new JLabel(lbl4));
    formPanel.add(val4Field);
    formPanel.add(Box.createHorizontalStrut(15)); // a spacer
    formPanel.add(new JLabel(lbl5));
    formPanel.add(val5Field);
    formPanel.add(Box.createHorizontalStrut(15)); // a spacer

    int result = JOptionPane.showConfirmDialog(null, formPanel, 
            name, JOptionPane.OK_CANCEL_OPTION);

    if (result == JOptionPane.OK_OPTION) 
    {
        val1 = val1Field.getText();
        val2 = val2Field.getText();
        val3 = val3Field.getText();
        val4 = val4Field.getText();
        val5 = val5Field.getText();

    }

    return(option, val1, val2, val3, val4, val5);
}

Now.. it took me a while to realize that i cannot return values like that, and that i could instead return the object instead. I have a class made for each of these "tables" (Item, Customer and Staff). But.. the thing is in the method above i need to use a switch so that i can have the labels made according to the type of Table.

So my question is, is there a way to pass the object and its name into the method? Or do i have it all wrong?

Any help is much appreciated.

Community
  • 1
  • 1
Tsar
  • 170
  • 1
  • 6
  • 20
  • 1
    Is this meant to be a method or is it a constructor? – Elliott Frisch Oct 28 '14 at 15:55
  • Method. Constructor was written elsewhere. – Tsar Oct 28 '14 at 15:57
  • Java does not have "output" parameters. So if your intent that the caller will pass `String` variables like `val1`, `val2`, etc., and the method will fill in their values, you can't do that in Java the way you can in some other languages (like C++ or PHP or Ada). – ajb Oct 28 '14 at 15:58

2 Answers2

0

It looks to me like you don't really need to return option since you never change it. Also, it looks like you were trying to have the caller pass variables into the method and let the method fill in the values of those variables. But Java doesn't have "output" or "reference" parameters the way C++ and PHP do, so this doesn't work. (You can pass in a reference to a mutable object and have a method set a field in that object, but you can't do that with String since it's immutable.)

If that's the case, then since the 5 things you want to return are all the same type, you could just make your method return a String[]:

public String[] form(int option)
{
    String[] values = new String[] (5);
    //values[0] = null;  // not necessary since the array elements will already be null
    //values[1] = null;  ...

    ...


    if (result == JOptionPane.OK_OPTION) 
    {
        values[0] = val1Field.getText();
        values[1] = val2Field.getText();
        values[2] = val3Field.getText();
        values[3] = val4Field.getText();
        values[4] = val5Field.getText();

    }

    return values;
}

P.S. I recommend using arrays or ArrayList instead of separate variables like val1Field, val2Field, etc., so that you don't have to repeat code like this.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • As i explained above.. i have multiple tables.. i need to insert values into them, to do that i first tried separately using joptionpane for inserting values.. but that seemed too tedious. So i want to make a form, and since all tables have 5 fields, i thought of using one method for all of them. Thats why i need a "option" to identify which one it is (customer, item or staff) to make sure labels are accordingly set. If i set option as an argument, i need to return it aswell.. any thoughts on that? – Tsar Oct 28 '14 at 16:24
  • @ShifaTsar I don't understand why you need to return `option`. The caller has the option already, it doesn't need the `form` method to compute it. You may have some basic misunderstanding about what it means to **return** something. Generally, you write a method that _returns_ a value when the purpose of the method is to compute something, or retrieve some data, and give the result back to the caller. Your description makes it look like a `form` should be an object (of a class), not a method. – ajb Oct 28 '14 at 16:29
  • thanks for the option part advice. I read through the documentation and managed to return the object only and it worked :) – Tsar Oct 29 '14 at 10:05
0

While there is a way to kind of do this, I would like to point out that this is not a good design! Don't generalize methods! I think that you need to try to redesign your code instead. If one of your classes changes, you will end up breaking them up anyway. Create a handler for each form type. You can do this using Factory pattern and then get the appropriate form, based on that. Might be a bit more work, but you will thank me later :)

If you need help on design, then that is something that we can definitely discuss.

lupus137
  • 383
  • 3
  • 10