Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 107,400 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,155 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



applet program

 
Reply to this topicStart new topic

applet program, problem in output need help urgent and desperately

B@LL!ST!C
post 20 Aug, 2007 - 06:11 AM
Post #1


D.I.C Head

**
Joined: 20 Aug, 2007
Posts: 148


My Contributions


i hope you can help me with my program i cant seem to distinguish the mistake im a newbie in java.
if you could run the program you will see the problem
pls i need your help i will pass this tomorrow
this is my finals exam
CODE

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class finals3 extends Applet implements ActionListener,ItemListener

{
    int a;
    
    Label iaLbl = new Label("Items Available:");    
    Checkbox friesBox = new Checkbox("Fries",false);
    Checkbox spagBox = new Checkbox("Spaghetti",false);
    Checkbox burgerBox = new Checkbox("Burger",false);
    Checkbox drinksBox = new Checkbox("Drinks",false);

    Label qtyLbl = new Label("Quantity");
    TextField qty1Txt = new TextField(10);
    TextField qty2Txt = new TextField(10);
    TextField qty3Txt = new TextField(10);
    TextField qty4Txt = new TextField(10);

    Label sizeLbl = new Label("Size");
    Choice fchoice = new Choice();
    Choice schoice = new Choice();
    Choice bchoice = new Choice();
    Choice dchoice = new Choice();

    Label tpLbl = new Label("Total Price:");
    TextField tp1Txt = new TextField(10);
    TextField tp2Txt = new TextField(10);
    TextField tp3Txt = new TextField(10);
    TextField tp4Txt = new TextField(10);
    
    Label tocLbl = new Label("Type of Customer:");
    CheckboxGroup optGroup = new CheckboxGroup();
    Checkbox member = new Checkbox("Member(10% Discount)",false,optGroup);
    Checkbox student = new Checkbox("Student(5% Discount)",false,optGroup);
    Checkbox senior = new Checkbox("Senior Citizen(20% Discount)",false,optGroup);
    Checkbox others = new Checkbox("Others",false,optGroup);

    Label tiLbl = new Label("Total Items:");
    TextField tiTxt = new TextField(10);

    Label taLbl = new Label("Total Amount:");
    TextField taTxt = new TextField(10);

    Label disLbl = new Label("Discount:");
    TextField disTxt = new TextField(10);

    Label atpLbl = new Label("Amount to Pay:");
    TextField atpTxt = new TextField(10);

    Label arLbl = new Label("Amount Received:");
    TextField arTxt = new TextField(10);

    Label changeLbl = new Label("Change:");    
    TextField changeTxt = new TextField(10);

    Button compButton = new Button("COMPUTE");

    Button resetButton = new Button("RESET");

    Button payButton = new Button("PAY");
      
    int ti,ta,qty1,qty2,qty3,qty4,tp1=0,tp2=0,tp3=0,tp4=0,f1=0,f2=0,spag=0,b1=0,b2=0,d1=
0,d2=0,d3=0;
    
    double dis=0,atp=0,change=0,ar=0;
    
    public void init()

    {
    setLayout(new GridLayout(8,7));
    
    add(iaLbl);
    add(friesBox);
    friesBox.addItemListener(this);
    add(spagBox);
    spagBox.addItemListener(this);
    add(burgerBox);
    burgerBox.addItemListener(this);
    add(drinksBox);
    drinksBox.addItemListener(this);

    add(qtyLbl);
    add(qty1Txt);    
    add(qty2Txt);
    add(qty3Txt);
    add(qty4Txt);

    add(sizeLbl);
    add(fchoice);
    fchoice.addItem("Regular P 30");
    fchoice.addItem("Large P 40");
    
    add(schoice);
    schoice.addItem("P 40");

    add(bchoice);
    bchoice.addItem("Regular P 30");
    bchoice.addItem("With Cheese P 35");
    
    add(dchoice);
    dchoice.addItem("Regular P 15");
    dchoice.addItem("Medium P 20");
    dchoice.addItem("Large P 25");

    add(tpLbl);
    add(tp1Txt);    
    add(tp2Txt);
    add(tp3Txt);
    add(tp4Txt);

    add(tocLbl);  
    add(member);
    member.addItemListener(this);
    add(student);
    student.addItemListener(this);
    add(senior);
    senior.addItemListener(this);
    add(others);
    others.addItemListener(this);

    add(tiLbl);
    add(tiTxt);

    add(taLbl);
    add(taTxt);

    add(disLbl);
    add(disTxt);

    add(atpLbl);
    add(atpTxt);
  
    add(arLbl);
    add(arTxt);

    add(changeLbl);
    add(changeTxt);

    add(compButton);
    compButton.addActionListener(this);
    add(resetButton);
    resetButton.addActionListener(this);    
    add(payButton);
    payButton.addActionListener(this);      

    }

    public void itemStateChanged(ItemEvent e)

    {
    
    if(member.getState())
    a=1;
    
    else if(student.getState())
    a=2;
    
    else if(senior.getState())
    a=3;

    else if(others.getState())
    a=4;
     }



     public void actionPerformed(ActionEvent e)

     {
    String arg;    
    
    arg=e.getActionCommand();    

    if(arg.equals("COMPUTE"))
    {
        
        
            if(friesBox.getState())
            {
                if(fchoice.getSelectedItem())
        qty1=Integer.parseInt(qty1Txt.getText());
            f1=30;
            tp1=qty1*f1;
                tp1Txt.setText("" +tp1);
        }
            
                else if(fchoice.getSelectedItem())
                {
                qty1=Integer.parseInt(qty1Txt.getText());
            f2=40;
            tp1=qty1*f2;
               tp1Txt.setText("" +tp1);
                }
        }
        
            if(spagBox.getState())
            {
        
        qty2=Integer.parseInt(qty2Txt.getText());
        spag=40;  
            tp2=qty2*spag;
        tp2Txt.setText("" +tp2);
            }
            
            else
            {  
            tp2=qty2*spag;
        tp2Txt.setText("" +tp2);
            }
          
            if(burgerBox.getState())
            {
                if(bchoice.select())
        {
        qty3=Integer.parseInt(qty3Txt.getText());
            breg=bchoice.getSelectedItem();
            b1=30;
        breg=b1;
                tp3=qty3*b1;
                tp3Txt.setText("" +tp3);
                }
            
            else if(bchoice.select())
                {
            qty3=Integer.parseInt(qty3Txt.getText());
            bchs=bchoice.getSelectedItem();
            b2=35;
        bchs=b2;
            tp3=qty3*b2;
                tp3Txt.setText("" +tp1);
                }
        }
        
            if(drinksBox.getState())
            {
                if(dchoice.select())
        {
        qty4=Integer.parseInt(qty4Txt.getText());
            dreg=dchoice.getSelectedItem();
            d1=15;
        dreg=d1;
                tp4=qty3*d1;
                tp4Txt.setText("" +tp4);  
                }
            
            else if(dchoice.select())
                {
                qty4=Integer.parseInt(qty4Txt.getText());
            dmed=dchoice.getSelectedItem();
            d2=20;
        dmed=d2;
                tp4=qty4*d2;
                tp4Txt.setText("" +tp4);
                }

            else if(dchoice.select())
                {
                qty4=Integer.parseInt(qty4Txt.getText());
            dlar=dchoice.getSelectedItem();
            d3=25;
        dlar=d3;
                tp4=qty4*d3;
                tp4Txt.setText("" +tp4);
                }
        }
        

        if(a==1)
        {
        ti=(qty1) + (qty2) + (qty3) + (qty4);
        tiTxt.setText(""+ti);
        

        ta=(tp1) + (tp2) + (tp3) + (tp4);
        taTxt.setText(""+ta);
    
        dis=ta * 0.1;
        disTxt.setText(""+dis);

        atp=ta-dis;
        atpTxt.setText(""+atp);
        }
        
    
        else if(a==2)
        {
        ti=(qty1) + (qty2) + (qty3) + (qty4);
        tiTxt.setText(""+ti);
        

        ta=(tp1) + (tp2) + (tp3) + (tp4);
        taTxt.setText(""+ta);
    
        dis=ta * 0.05;
        disTxt.setText(""+dis);

        atp=ta-dis;
        atpTxt.setText(""+atp);
        }

        else if(a==3)
        {
        ti=(qty1) + (qty2) + (qty3) + (qty4);
        tiTxt.setText(""+ti);
        

        ta=(tp1) + (tp2) + (tp3) + (tp4);
        taTxt.setText(""+ta);
    
        dis=ta * 0.2;
        disTxt.setText(""+dis);

        atp=ta-dis;
        atpTxt.setText(""+atp);
        }

    else if(a==4)
        {
        ti=(qty1) + (qty2) + (qty3) + (qty4);
        tiTxt.setText(""+ti);
        

        ta=(tp1) + (tp2) + (tp3) + (tp4);
        taTxt.setText(""+ta);
    
        disTxt.setText("" +dis);

        atp=ta-dis;
        atpTxt.setText(""+atp);
        }
        repaint();
    }
    
    else if(arg.equals("RESET"))    
    {
        friesBox.setState(false);
        spagBox.setState(false);
        burgerBox.setState(false);
        drinksBox.setState(false);
        qty1Txt.setText("");
        qty2Txt.setText("");
        qty3Txt.setText("");
        qty4Txt.setText("");
    fchoice.select(0);
    bchoice.select(0);
    dchoice.select(0);
        tp1Txt.setText("");
        tp2Txt.setText("");
        tp3Txt.setText("");
        tp4Txt.setText("");
           member.setState(false);
          student.setState(false);
           senior.setState(false);
    others.setState(false);
        tiTxt.setText("");
        taTxt.setText("");
        disTxt.setText("");
        atpTxt.setText("");
    arTxt.setText("");
    changeTxt.setText("");
        a=1;
        ti=0;
    ta=0;
        dis=0;
        qty1=0;
        qty2=0;
        qty3=0;
        qty4=0;
        tp1=0;
        tp2=0;
        tp3=0;
        tp4=0;
        repaint();
    }

    else if(arg.equals("PAY"))
    {
    ar=Integer.parseInt(arTxt.getText());
    change=ar-atp;
    changeTxt.setText("" +change);
    }
    repaint();
   }
}

here is the html file
CODE

<html>
<applet code="finals3.class" height=300 width=600>
</applet>
</html>


pls pls pls i need your help sad.gif
i am going nuts crazy.gif
User is offlineProfile CardPM

Go to the top of the page


scarecrowfeild
post 20 Aug, 2007 - 07:41 AM
Post #2


New D.I.C Head

*
Joined: 14 Jul, 2007
Posts: 31


My Contributions


inside your public void actionPerformed
1.at friesBox:you mess up with your block.you misplace your {
2.at spagBox and so on:same thing happen.
3.you should use if to compare value.but you compare it with method.
User is offlineProfile CardPM

Go to the top of the page

B@LL!ST!C
post 20 Aug, 2007 - 08:18 AM
Post #3


D.I.C Head

**
Joined: 20 Aug, 2007
Posts: 148


My Contributions


QUOTE(scarecrowfeild @ 20 Aug, 2007 - 07:41 AM) *

inside your public void actionPerformed
1.at friesBox:you mess up with your block.you misplace your {
2.at spagBox and so on:same thing happen.

already got this 2..
QUOTE(scarecrowfeild @ 20 Aug, 2007 - 07:41 AM) *

3.you should use if to compare value.but you compare it with method.

this is my problem how can i do that.
when i choose a different choice lets say the fries should be large,
the tp1(fries's total price) doesn't follow what is chosen.
how can i make the tp's output to follow what i choose in the choice box? blink.gif
User is offlineProfile CardPM

Go to the top of the page

scarecrowfeild
post 20 Aug, 2007 - 08:59 AM
Post #4


New D.I.C Head

*
Joined: 14 Jul, 2007
Posts: 31


My Contributions


try this.hope it solve part of your problem.
it has too many if that i get lost.its some sort of menu right?why don't you use switch case to simplify it.

CODE
if(friesBox.getState())
            {//a1 start fries
                if(fchoice.getSelectedItem()) {//a1.1
        qty1=Integer.parseInt(qty1Txt.getText());
            f1=30;
            tp1=qty1*f1;
                tp1Txt.setText("" +tp1);
        }//a1.1
            
                else if(fchoice.getSelectedItem())
                {//a1.2
                qty1=Integer.parseInt(qty1Txt.getText());
            f2=40;
            tp1=qty1*f2;
               tp1Txt.setText("" +tp1);
                }//a1.2
        }//a1 end fries
User is offlineProfile CardPM

Go to the top of the page

B@LL!ST!C
post 20 Aug, 2007 - 09:19 AM
Post #5


D.I.C Head

**
Joined: 20 Aug, 2007
Posts: 148


My Contributions


will this do?
CODE

if(friesBox.getState())
            {
            switch(frieschoice)
        {
        case 1:
        {
        qty1=Integer.parseInt(qty1Txt.getText());
        fchoice.getSelectedItem();
            f1=30;
            tp1=qty1*f1;
            tp1Txt.setText("" +tp1);
        break;
            }
            
            case 2:
            {
            qty1=Integer.parseInt(qty1Txt.getText());
        fchoice.getSelectedItem();
            f2=40;
            tp1=qty1*f2;
            tp1Txt.setText("" +tp1);
        break;
            }
        }
        }
        
            if(spagBox.getState())
            {
            qty2=Integer.parseInt(qty2Txt.getText());
        fchoice.getSelectedItem();
        s1=40;  
            tp2=qty2*s1;
            tp2Txt.setText("" +tp2);
            }
            
            else
            {
            tp2Txt.setText("0");
            }
          
            if(burgerBox.getState())
            {
        switch(burgerchoice)
        {
        case 1:
        {
            qty3=Integer.parseInt(qty3Txt.getText());
        bchoice.getSelectedItem();
            b1=30;
            tp3=qty3*b1;
            tp3Txt.setText("" +tp3);
        break;
            }
            
            case 2:
            {
            qty3=Integer.parseInt(qty3Txt.getText());
        bchoice.getSelectedItem();
            b2=35;
            tp3=qty3*b2;
            tp3Txt.setText("" +tp1);
        break;
            }
        }
        }
        
            if(drinksBox.getState())
            {
        switch(drinkschoice)
        {
        case 1:
        {
            qty4=Integer.parseInt(qty4Txt.getText());
        dchoice.getSelectedItem();
            d1=15;
            tp4=qty3*d1;
            tp4Txt.setText("" +tp4);
        break;
            }
            
            case 2:
            {
            qty4=Integer.parseInt(qty4Txt.getText());
        dchoice.getSelectedItem();
            d2=20;
            tp4=qty4*d2;
            tp4Txt.setText("" +tp4);
        break;
            }

            case 3:
            {
            qty4=Integer.parseInt(qty4Txt.getText());
        dchoice.getSelectedItem();
            d3=25;
            tp4=qty4*d3;
            tp4Txt.setText("" +tp4);
        break;
            }
        }
        }

there is still some error with the output
do you have any idea why?
User is offlineProfile CardPM

Go to the top of the page

scarecrowfeild
post 20 Aug, 2007 - 09:57 AM
Post #6


New D.I.C Head

*
Joined: 14 Jul, 2007
Posts: 31


My Contributions


what kind of error do you get?
miscalculation?involving fries?burger?drink?spaghetti?
i notice something weird at your spagBox.better check it.
the else statement is suppose to be in the spagbox is outside spagbox block.

This post has been edited by scarecrowfeild: 20 Aug, 2007 - 10:03 AM
User is offlineProfile CardPM

Go to the top of the page

B@LL!ST!C
post 20 Aug, 2007 - 10:11 AM
Post #7


D.I.C Head

**
Joined: 20 Aug, 2007
Posts: 148


My Contributions


here is the error
i boxed it red
this is after i click the compute button
hope you can show me whats wrong


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM

Go to the top of the page

scarecrowfeild
post 20 Aug, 2007 - 11:39 AM
Post #8


New D.I.C Head

*
Joined: 14 Jul, 2007
Posts: 31


My Contributions


i am not sure.but try to put the else at the end of the if else statement.after the end of the drink.
i think your calculation is alright.
User is offlineProfile CardPM

Go to the top of the page

B@LL!ST!C
post 20 Aug, 2007 - 08:24 PM
Post #9


D.I.C Head

**
Joined: 20 Aug, 2007
Posts: 148


My Contributions


do you mean the else is interfering with the other ifs?
User is offlineProfile CardPM

Go to the top of the page

scarecrowfeild
post 21 Aug, 2007 - 06:05 AM
Post #10


New D.I.C Head

*
Joined: 14 Jul, 2007
Posts: 31


My Contributions


yes.you should arrange your if else statement.
User is offlineProfile CardPM

Go to the top of the page

zdnaka
post 22 Aug, 2007 - 04:59 PM
Post #11


New D.I.C Head

*
Joined: 10 Aug, 2007
Posts: 14


My Contributions


yah you should do that

This post has been edited by zdnaka: 22 Aug, 2007 - 05:00 PM
User is offlineProfile CardPM

Go to the top of the page

B@LL!ST!C
post 22 Aug, 2007 - 05:02 PM
Post #12


D.I.C Head

**
Joined: 20 Aug, 2007
Posts: 148


My Contributions


thanks guys!!
i have already figured it out
here is my new code
CODE

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class fin extends Applet implements ActionListener, ItemListener
{
Label items=new Label ("Items Available:");
Label quantity=new Label ("Quantity:");
Label size=new Label ("Size:");
Label totalprice=new Label ("Total Price:");
Checkbox fries=new Checkbox ("Fries",false);
TextField qfries=new TextField (5);
Choice sfries=new Choice();
TextField tfries=new TextField(5);
Checkbox spag=new Checkbox ("Spaghetti",false);
TextField qspag=new TextField(5);
Label space=new Label("      P 35      ");
TextField tspag=new TextField(5);
Checkbox burger=new Checkbox ("Burger",false);
TextField qburger=new TextField(5);
Choice sburger=new Choice();
TextField tburger=new TextField(5);
Checkbox drinks=new Checkbox ("Softdrinks",false);
TextField qdrinks=new TextField(5);
Choice sdrinks=new Choice();
TextField tdrinks=new TextField(5);
Label customer=new Label("Type of Customer:");
CheckboxGroup type=new CheckboxGroup();
Checkbox member1=new Checkbox("Member(10% discount)",false,type);
Checkbox member2=new Checkbox("Student(5% discount)",false,type);
Checkbox member3=new Checkbox("Senior Citizen(20% discount)",false,type);
Checkbox member4=new Checkbox("Others",true,type);
Button compute=new Button ("COMPUTE");
Button reset=new Button ("RESET");
Button pay=new Button ("PAY");
Label totalitems=new Label ("Total Items:");
TextField tfitems=new TextField(10);
Label tamt=new Label ("Total Amount:");
TextField tfamt=new TextField(10);
Label dc=new Label ("Discount:");
TextField tfdc=new TextField(10);
Label tpay=new Label ("Amount to Pay:");
TextField tfpay=new TextField(10);
Label trcvd=new Label ("Amount Received:");
TextField tfrcvd=new TextField(10);
Label tchange=new Label ("Change:");
TextField tfchange=new TextField(10);

public void init()
{
add(items);
add(quantity);
add(size);
add(totalprice);
add(fries);
fries.addItemListener(this);
add(qfries);
add(sfries);
sfries.addItem("Regular (P30)");
sfries.addItem("Large (P40)");
add(tfries);
add(spag);
spag.addItemListener(this);
add(qspag);
add(space);
add(tspag);
add(burger);
burger.addItemListener(this);
add(qburger);
add(sburger);
sburger.addItem("Regular (P25)");
sburger.addItem("w/ Cheese (P35)");
add(tburger);
add(drinks);
drinks.addItemListener(this);
add(qdrinks);
add(sdrinks);
sdrinks.addItem("Regular (P15)");
sdrinks.addItem("Medium (P20)");
sdrinks.addItem("Large (P25)");
add(tdrinks);
add(customer);
add(member1);
member1.addItemListener(this);
add(member2);
member2.addItemListener(this);
add(member3);
member3.addItemListener(this);
add(member4);
member4.addItemListener(this);
add(compute);
compute.addActionListener(this);
add(reset);
reset.addActionListener(this);
add(pay);
pay.addActionListener(this);
add(totalitems);
add(tfitems);
add(tamt);
add(tfamt);
add(dc);
add(tfdc);
add(tpay);
add(tfpay);
add(trcvd);
add(tfrcvd);
add(tchange);
add(tfchange);
}

public void itemStateChanged(ItemEvent ie)
{
}

public void actionPerformed(ActionEvent e)
{
String button,fr,bg,dr;
int qf,qb,qs,qd,tq;
double tf,tb,ts,tdrnk,total,dis,topay,pay1,chg;
button=e.getActionCommand();
qf=0;qb=0;qs=0;qd=0;dis=0;
tf=0;tb=0;ts=0;tdrnk=0;topay=0;pay1=0;

if (button=="COMPUTE")
{

  if(fries.getState())
  {
  fr=sfries.getSelectedItem();
  qf=Integer.parseInt(qfries.getText());
    if(fr.equals("Regular (P30)"))
    {
    tf=qf*30;
    }
    if(fr.equals("Large (P40)"))
    {
    tf=qf*40;
    }
  tfries.setText(""+tf);
  }
  else
  {
  tf=0;
  tfries.setText(""+tf);
  qfries.setText("0");
  }

  if(spag.getState())
  {
  qs=Integer.parseInt(qspag.getText());
  ts=qs*35;
  tspag.setText(""+ts);
  }
  else
  {
  ts=0;
  tspag.setText(""+ts);
  qspag.setText("0");
  }

  if(burger.getState())
  {
  bg=sburger.getSelectedItem();
  qb=Integer.parseInt(qburger.getText());
    if(bg.equals("Regular (25)"))
    {
    tb=qb*25;
    }
    if(bg.equals("w/ Cheese (P35)"))
    {
    tb=qb*35;
    }
  tburger.setText(""+tb);
  }
  else
  {
  tb=0;
  tburger.setText(""+tb);
  qburger.setText("0");
  }

  if(drinks.getState())
  {
  dr=sdrinks.getSelectedItem();
  qd=Integer.parseInt(qdrinks.getText());
    if(dr.equals("Regular (P15)"))
    {
    tdrnk=qd*15;
    }
    if(dr.equals("Medium (P20)"))
    {
    tdrnk=qd*20;
    }
    if(dr.equals("Large (P25)"))
    {
    tdrnk=qd*25;
    }
  tdrinks.setText(""+tdrnk);
  }
  else
  {
  tdrnk=0;
  tdrinks.setText(""+tdrnk);
  qdrinks.setText("0");
  }

tq=qd+qb+qs+qf;
total=tdrnk+tb+ts+tf;

if(member1.getState())
{
dis=(total/100)*10;
}
if(member2.getState())
{
dis=(total/100)*5;
}
if(member3.getState())
{
dis=(total/100)*20;
}
if(member4.getState())
{
dis=0;
}

topay=total-dis;

tfitems.setText(""+tq);
tfamt.setText(""+total);
tfdc.setText(""+dis);
tfpay.setText(""+topay);

}

if(button=="PAY")
{
pay1=Double.parseDouble(tfrcvd.getText());
topay=Double.parseDouble(tfpay.getText());
  chg=pay1-topay;
  if(chg>=0.00)
  {
  tfchange.setText(""+chg);
  }
  if(chg<0.00)
  {
  tfrcvd.setText("Insufficient Funds!");
  }
}

if(button=="RESET")
{
qfries.setText("");
qspag.setText("");
qdrinks.setText("");
qburger.setText("");
tfitems.setText("");
tfamt.setText("");
tfdc.setText("");
tfpay.setText("");
tfchange.setText("");
tfrcvd.setText("");
fries.setState(false);
burger.setState(false);
drinks.setState(false);
spag.setState(false);
member1.setState(false);
member2.setState(false);
member3.setState(false);
member4.setState(true);
tfries.setText("");
tspag.setText("");
tdrinks.setText("");
tburger.setText("");
sfries.select(0);
sburger.select(0);
sdrinks.select(0);
}
}
}
User is offlineProfile CardPM