简体   繁体   中英

How to take values from gui and apply suitable constructor or method

I am newbie programmer learning the use of object oriented programming concepts (inheritance and overloading constructors methods )

Now i have created (mostly copied) a code that will take 5 inputs from user and display a output. Now using method overloading it can also take 3 inputs and display they same output.

Now how do implement that in a Swing GUI . the GUI is this

GUI图像

User can give all the values or give only one from width, height, and depth.

the main code is

`

class box
{
    private double width;
    private double height;
    private double depth;
    box(box ob)
    {
        width =ob.width;
        height=ob.height;
        depth =ob.depth;
    }
    box(double w,double h,double d)

    {
        width=w;
        height=h;
        depth=d;
    }
    box()
    {
        width=-1;
        height=-1;
        depth=-1;
    }
    box(double len)
    {
        width=height=depth=len;
    }
    double volume()
    {
        return width*depth*height;
    }

 }

class boxweight extends box
{
    private double weight;
    boxweight(boxweight ob)
    {
        super(ob);
        weight= ob.weight;
    }
    boxweight(double w,double h,double d,double m)
    {
        super(w,h,d);
        weight=m;
    }

    boxweight(double len,double m)
    {
        super(len);
        weight=m;

    }

    boxweight() 
    {
        super();
        weight=-1;
    }

    double weightcal()
    {
        return (volume()*weight);
    }


}

class shipment extends boxweight
{
    private double cost;

    shipment(shipment ob) 
    {
        super(ob);
        cost=ob.cost;

    }

    shipment(double w,double h,double d,double m,double c) 
    {
        super(w,h,d,m);
        cost=c;
    }
    shipment (double len,double m,double c)
    {
        super(len,m);
        cost=c;

    }

    shipment() 
    {
        super();
        cost=-1;
    }
    double costcal()
    {
        return(weightcal()*cost);
    }


}


public class Inheritanceallhere {

    public static void main(String[] args) {

        shipment o1=new shipment(10,20,30,40,50);
        shipment o3=new shipment(18.17122,40,50);
        double vol;
        vol = o1.volume();
        System.out.println("Volume of shipment1 is " + vol);
        System.out.println("Weight of shipment1 is "+ o1.weightcal());
        System.out.println("Shipping cost: $" + o1.costcal());
        System.out.println();
        vol =o3.volume();
        System.out.println("Volume of shipment2 is " + vol);
        System.out.println("Weight of shipment2 is "+ o3.weightcal());
        System.out.println("Shipping cost: $" + o3.costcal());

    }
}

`

Now what i want when i press the calculate button it will take the available values and assign appropriate method to it

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       double a,b,c,d,e;
       firstsubclass o2=new firstsubclass();
       a=Double.parseDouble(jTextField1.getText());
       b=Double.parseDouble(jTextField2.getText());
       c=Double.parseDouble(jTextField4.getText());
       d=Double.parseDouble(jTextField5.getText());
       e=Double.parseDouble(jTextField6.getText());
       shipment o3=new shipment(a,b,c,d,e);




       jTextField3.setText(String.valueOf(o3.costcal()));

}

i did this code to take all the five inputs and then calculate. but i cant do the same thing for three values(where any one of the width height or depth field will be available). I know i can use some control statement to make sure which method to work but i wanted to know if there is any dynamic control in java so that i can do that directly without any additional code.

NB: Both code are in the same package

I suggest that you look at the Java Collections Framework, specifically ArrayList as a good collection to use for general purposes.

http://docs.oracle.com/javase/7/docs/technotes/guides/collections/index.html

If your program has n things, but you don't know at "compile time" how many things, you can write your logic to expect a list of things, then loop over the list.

How can i make a program that can understand how many inputs are available and does the calculation based on that?

You test each field individually.

if (!jTextField1.getText().trim().equals("")) {
    a = Double.parseDouble(jTextField1.getText().trim());
}

You then test the Double values to see which one(s) have been set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM