I'm working on a program that first launches a frame with various text fields. After they are filled in, the information is submitted by simply clicking the submit button. I have the frame down, the text fields and the buttons, and I have the class where all my data and calculations lie.
I'm just having a hard time figuring out how to weave it all together, specifically on how to take that information that's input into the frame and send it all to the class where calculations are made, then return back the result to be displayed on the frame itself (i'll probably change from the printData method to toString method).
Any help would be awesome.
package prjPackage;
import javax.swing.SwingUtilities;
public class ProjectClass {
public static void main(String[] args)
{
Package p;
PostOffice po;
// create objects
po = new PostOffice();
//mail package
po.getPackage();
//launch the frame
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new PackageFrame();
}
});
// print out results
po.printData();
}// end main
}// end ProjectClass
//
//PostOffice
//
//The purpose of this class is to represent the Post Office
//rules for mailing a package first class.
//
package prjPackage;
public class PostOffice
{
private Package pkg;
private float cost;
private boolean canShipPackage;
private boolean exceedsWeightLimit;
private boolean exceedsCombinedLimit;
// constants used in determining the cost of shipping the package
// prices per pound
private static final float COSTRANGE1 = 0.75f;
private static final float COSTRANGE2 = 0.45f;
private static final float COSTRANGE3 = 0.40f;
private static final float COSTRANGE4 = 0.35f;
private static final float COSTRANGE5 = 0.30f; // greater weight than last range
// ranges of weights in pounds
private static final int MAXRANGE1 = 2; // up to 2 lbs
private static final int MAXRANGE2 = 10; // greater than 2 to up to 10 lbs
private static final int MAXRANGE3 = 40; // greater than 10 up to 40 lbs
private static final int MAXRANGE4 = 55; // greater than 40 up to 55 lbs
// limits
private static final int MAXWTTOSHIP = 70; // maximum weight allowed to ship
private static final int MAXLENGIRTHTOSHIP = 100; // maximum len + girth to ship
//
// PostOffice
//
// Constructor for the PostOffice class
//
// Input: none
// Return: none
//
public PostOffice()
{
pkg = new Package();
updateShipping();
} // end PostOffice
//
// setPackage
//
// This method modifies the package attribute
//
// Input: p the new package
// Return: none
//
public void setPackage(Package p)
{
pkg = p;
updateShipping();
} // end setPackage
//
// getPackage
//
// this method returns a copy of the package attribute
//
// Input: none
// Return: pkg
//
public Package getPackage()
{
return(pkg);
} // end getPackage
//
// getCost
//
// this method returns a copy of the cost to ship the package
//
// Input: none
// Return: cost
//
public float getCost()
{
return(cost);
} // end getCost
//
// getCanShipPackage
//
// This method returns a copy of the canShipPackage attribute
//
// Input: none
// Return: canShipPackage
//
public boolean getCanShipPackage()
{
return (canShipPackage);
} // end getCanShipPackage
//
// getExceedsCombinedLimit
//
// the purpose of this method is to return a copy of the exceedsCombinedLimit
// attribute
//
// Input: none
// Return: exceedsCombinedLimit
//
public boolean getExceedsCombinedLimit()
{
return(exceedsCombinedLimit);
}// end getExceedsCombinedLimit
//
// getExceedsWeightLimit
//
// the purpose of this method is to return a copy of the exceedsWeightLimit
// attribute
//
// Input: none
// Return: exceedsWeightLimit
//
public boolean getExceedsWeightLimit()
{
return(exceedsWeightLimit);
}// end getExceedsWeightLimit
//
// printData
//
// this method prints out the attributes for the class
//
// Input: none
// REturn: none
//
public void printData()
{
pkg.printData();
if (canShipPackage == true)
{
System.out.println("The package will cost $" + cost + " to ship.");
}
else
{
if (exceedsWeightLimit == true)
{
System.out.print("The package cannot be shipped. ");
System.out.println("It exceeds weight restrictions");
}
if (exceedsCombinedLimit == true)
{
System.out.print("The package cannot be shipped. ");
System.out.println("It exceeds the combined length restrictions");
}
}
} // end printData
//
// sendPackage
//
// This method allows a package to be checked for shipping cost to send.
//
// Input: p the package to ship
// Return: none
//
public void sendPackage(Package p)
{
pkg = p;
// update the shipping information for this new package
updateShipping();
} // end sendPackage
//
// updateShipping
//
// The purpose of this method is to update the shipping information
// for the package
//
// Input: none
// Return: none
//
public void updateShipping( )
{
// update the weight status
checkWeightLimit( );
// update the combined status
checkCombinedLimit( );
// update the overall shipping status
if ((exceedsWeightLimit == true) || (exceedsCombinedLimit == true))
{
canShipPackage = false;
cost = 0.0f;
}
else
{
canShipPackage = true;
// update the shipping cost
calcCost();
}
}// end updateShipping
//
// checkWeightLimit
//
// The purpose of this method is to check the weight limit of a package
//
// Input: none
// Return: none
//
public void checkWeightLimit( )
{
int wt;
wt = pkg.getWeight();
if (wt > MAXWTTOSHIP)
{
exceedsWeightLimit = true;
}
else
{
exceedsWeightLimit = false;
}
}// end checkWeightLimit
//
// checkCombinedLimit
//
// The purpose of this method is to check the combined limit of a package
//
// Input: none
// Return: none
//
public void checkCombinedLimit( )
{
int len;
int g;
int combinedValue;
// calculate the combined value
len = pkg.getLength();
g = pkg.getGirth();
combinedValue = len + g;
if (combinedValue > MAXLENGIRTHTOSHIP)
{
exceedsCombinedLimit = true;
}
else
{
exceedsCombinedLimit = false;
}
}// end checkCombinedLimit
//
// calcCost
//
// this method determines the cost for a package to be shipped.
//
// Input: none
// REturn: none
//
private void calcCost()
{
int wt;
// get the package attributes
wt = pkg.getWeight();
// okay to ship the package so calculate cost
if (wt <= MAXRANGE1)
{
cost = calcRange1Cost(wt);
}
else if (wt <= MAXRANGE2)
{
cost = calcRange2Cost(wt);
}
else if (wt <= MAXRANGE3)
{
cost = calcRange3Cost(wt);
}
else if (wt <= MAXRANGE4)
{
cost = calcRange4Cost(wt);
}
else
{
cost = calcRange5Cost(wt);
}
}// end calcCost
//
// calcRange1Cost
//
// This method calculates the cost for range 1
//
// Input: wt weight in lbs used to calculate cost
// Return: rangeCost
//
private float calcRange1Cost(int wt)
{
float rangeCost;
rangeCost = (float)wt * (float)COSTRANGE1;
return(rangeCost);
}
//
// calcRange2Cost
//
// This method calculates the cost for range 2
//
// Input: wt weight in labs used to calculate cost
// Return: rangeCost
//
private float calcRange2Cost(int wt)
{
int overRange1;
float rangeCost;
// calculate the price for the weight up to Range1
rangeCost = calcRange1Cost(MAXRANGE1);
// determine the weight over the Range1 limit
overRange1 = wt - MAXRANGE1;
// calculate the remaining cost
rangeCost += ((float)overRange1 * COSTRANGE2);
return(rangeCost);
} // end calcRange2Cost
//
// calcRange3Cost
//
// This method calculates the cost for range 3
//
// Input: wt weight in labs used to calculate cost
// Return: rangeCost
//
private float calcRange3Cost(int wt)
{
int overRange2;
float rangeCost;
// calculate the price for the weight up to Range2
rangeCost = calcRange2Cost(MAXRANGE2);
// determine the weight over the Range2 limit
overRange2 = wt - MAXRANGE2;
// calculate the remaining cost
rangeCost += ((float)overRange2 * COSTRANGE3);
return(rangeCost);
} // end calcRange3Cost
//
// calcRange4Cost
//
// This method calculates the cost for range 4
//
// Input: wt weight in labs used to calculate cost
// Return: rangeCost
//
private float calcRange4Cost(int wt)
{
int overRange3;
float rangeCost;
// calculate the price for the weight up to Range3
rangeCost = calcRange3Cost(MAXRANGE3);
// determine the weight over the Range3limit
overRange3 = wt - MAXRANGE3;
// calculate the remaining cost
rangeCost += ((float)overRange3 * COSTRANGE4);
return(rangeCost);
} // end calcRange4Cost
//
// calcRange5Cost
//
// This method calculates the cost for range 5
//
// Input: wt weight in labs used to calculate cost
// Return: rangeCost
//
private float calcRange5Cost(int wt)
{
int overRange4;
float rangeCost;
// calculate the price for the weight up to Range4
rangeCost = calcRange4Cost(MAXRANGE4);
// determine the weight over the Range1 limit
overRange4 = wt - MAXRANGE4;
// calculate the remaining cost
rangeCost += ((float)overRange4 * COSTRANGE5);
return(rangeCost);
} // end calcRange5Cost
}// end PostOffice
package prjPackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PackageFrame implements ActionListener
{
JTextField height;
JTextField weight;
JTextField girth;
JTextField length;
JTextField width;
PackageFrame()
{
JFrame jfrm = new JFrame("Text Fields");
jfrm.setLayout(new BorderLayout());
jfrm.setSize(400, 400);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
height = new JTextField(10);
weight = new JTextField(10);
girth = new JTextField(10);
length = new JTextField(10);
width = new JTextField(10);
//commented this out because i'm unable to reach my final result
//cost.setEditable(false);
// Set first panel to retrieve data from user
JPanel inFieldPane = new JPanel();
inFieldPane.setLayout(new GridLayout(2,2));
inFieldPane.add(new JLabel("Height"));
inFieldPane.add(height);
height.addActionListener(this);
inFieldPane.add(new JLabel("Weight"));
inFieldPane.add(weight);
weight.addActionListener(this);
jfrm.add(inFieldPane,BorderLayout.NORTH);
inFieldPane.add(new JLabel("Girth"));
inFieldPane.add(girth);
girth.addActionListener(this);
jfrm.add(inFieldPane,BorderLayout.NORTH);
inFieldPane.add(new JLabel("Length"));
inFieldPane.add(length);
length.addActionListener(this);
jfrm.add(inFieldPane,BorderLayout.NORTH);
inFieldPane.add(new JLabel("Width"));
inFieldPane.add(width);
width.addActionListener(this);
jfrm.add(inFieldPane,BorderLayout.NORTH);
//Set second panel to submit data for processing
JPanel submitPane = new JPanel();
submitPane.setLayout(new FlowLayout());
submitPane.add(new JLabel("Package Information"));
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(this);
submitPane.add(submitButton);
jfrm.add(submitPane,BorderLayout.CENTER);
// Set third panel to display processed data
JPanel outFieldPane= new JPanel();
outFieldPane.setLayout(new GridLayout(1,2));
outFieldPane.add(new JLabel("Cost To Ship"));
//commented this out but i know it should go there
//outFieldPane.add(cost);
jfrm.add(outFieldPane,BorderLayout.SOUTH);
//make the frame visible
jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Submit"))
{
//make the calculations and return the total
//This is where I get stuck. I need to figure out what the cost to ship will be and since I'm not sure on how to run my inputs into the PostOffice class, get the calculations done, and run it back to be displayed on the frame, I can't get further.
}
}
}