Search code examples
javaswingapplet

Java swing applet freezes when button run


This is code I wrote for an applet using swing. The program lets the user enter any extra items (peripherals), which shipping method they want to use, and then allows them to push buttons that calculate their total costs either with or without shipping depending on the button they push. The problem is, when I run it, it freezes and doesn't do anything after I click either of the calculate buttons. Does anyone see anything wrong with my code that could be causing this? Thank you.

 package selling;


public class midtermComputers extends javax.swing.JApplet {
public double calculateCost(int quantity){

     double discountedPrice=getDiscountPercentage(quantity);
       double finalCost=0;
     if (isShipping()&(groundRadioButton.isSelected())&quantity<100){
     finalCost= ((discountedPrice*.05)+discountedPrice)*quantity;
     }
        if (isShipping()&(groundRadioButton.isSelected())&quantity>100){
     finalCost=discountedPrice*quantity;
     }     
         if (isShipping()&(airRadioButton.isSelected())){
     finalCost= discountedPrice*.1*quantity;
     }
         if (isShipping()&(fedExRadioButton.isSelected())){
     finalCost= discountedPrice*.2*quantity;
}
         if (!isShipping()){
     finalCost= discountedPrice*quantity;
 }
         return finalCost;
 }

private void calcWithShippingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    int quantity=1;
    double finalCost=calculateCost(quantity);
outputTextPane.setText(outputTextPane.getText() + quantity + "\t" + finalCost  + "\n" );
     quantity=100;
 while(quantity<=1000){
outputTextPane.setText(outputTextPane.getText() + quantity + "\t" + finalCost  + "\n" );
quantity=quantity+100;
    }

    }                                                      

private void calcWithoutShippingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                          
   int quantity=1;
    double finalCost=calculateCost(quantity);
outputTextPane.setText(outputTextPane.getText() + quantity + "\t" + finalCost  + "\n" );
 quantity=100;
 while(quantity<=1000){
outputTextPane.setText(outputTextPane.getText() + quantity + "\t" + finalCost  + "\n" );
quantity=quantity+100;
}
}                                                         

public double calculateCost(){
double base=399.00;
double costOneUnit=0.0;
          double speakerPeripheral=0.0;
          double printerPeripheral=0.0;
          double scannerPeripheral=0.0;
          double microphonePeripheral=0.0;
          double projectorPeripheral=0.0;
          double plotterPeripheral=0.0;

while(speakerCheckBox.isSelected()){
   speakerPeripheral=89.00;
}

while(printerCheckBox.isSelected()){
  printerPeripheral=59.00;
}
 while(scannerCheckBox.isSelected()){
  scannerPeripheral=415.00;
 }
 while(microphoneCheckBox.isSelected()){
  microphonePeripheral=39.00;
 }
 while(projectorCheckBox.isSelected()){
  projectorPeripheral=549.00;
 }
 while(plotterCheckBox.isSelected()){
  plotterPeripheral=4883.00;
 }
 return costOneUnit=base+speakerPeripheral+printerPeripheral+scannerPeripheral+microphonePeripheral+projectorPeripheral+plotterPeripheral;

}


public boolean isShipping(){
if(calcWithShippingButton.isSelected()){
    return true;
} else{
    return false;
}
}

public double getDiscountPercentage(int quantity){
    double costOneUnit=calculateCost();
    double discountedPrice=0;

    while (quantity<100){
discountedPrice=costOneUnit;
}
    while (quantity>100&quantity<=199){
     discountedPrice=(costOneUnit-(costOneUnit*.01))*quantity;

}
    while (quantity>200&quantity<=299){
     discountedPrice=(costOneUnit-(costOneUnit*.02))*quantity;
}
    while (quantity>300&quantity<=399){
     discountedPrice=(costOneUnit-(costOneUnit*.03))*quantity;
}
    while (quantity>400&quantity<=499){
     discountedPrice=(costOneUnit-(costOneUnit*.04))*quantity;
}
    while (quantity>500&quantity<=599){
     discountedPrice=(costOneUnit-(costOneUnit*.05))*quantity;
}
    while (quantity>600&quantity<=699){
     discountedPrice=(costOneUnit-(costOneUnit*.06))*quantity;
}
    while (quantity>700&quantity<=799){
     discountedPrice=(costOneUnit-(costOneUnit*.07))*quantity;
}
    while (quantity>800&quantity<=899){
     discountedPrice=(costOneUnit-(costOneUnit*.08))*quantity;
}
    while (quantity>900&quantity<=999){
     discountedPrice=(costOneUnit-(costOneUnit*.09))*quantity;
}
    while(quantity<=1000&quantity>999){
      discountedPrice=(costOneUnit-(costOneUnit*.1))*quantity;  
    }
   return discountedPrice;
}  

Solution

  • the "while" in calculateCost() should be "if "

    public double calculateCost(){
           ...
           while(speakerCheckBox.isSelected()){
                  speakerPeripheral=89.00;
           }
    
           while(printerCheckBox.isSelected()){
                 printerPeripheral=59.00;
           }
           while(scannerCheckBox.isSelected()){
                 scannerPeripheral=415.00;
           }
           ....
    

    Like

    if(speakerCheckBox.isSelected()){
          speakerPeripheral=89.00;
    }