Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i just wrote this program but i can

Status
Not open for further replies.

twinclw2002

Programmer
Jan 10, 2004
2
GB
i just wrote this program but i cant take the sub total and the price of the products display correctly also the vat ect ect please help me

// Inner class declarations used to create event handlers.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Tesco extends Frame implements ActionListener,ItemListener{


ArrayList basket = new ArrayList();
double [] itemPrice = new double [10];
int[] count = {0,0,0,0,0,0,0,0,0,0};
double [] price = {0.35,1.00,0.50,1.15,2.50,0.50,1.30,0.75,0.65,1.00};
private Label display,welcome;

private Button exitButton,addToBasket,show;
private JComboBox selectNo;
Choice selectItem = new Choice();
private TextField priceItem,subTotal,VAT,totalPrice,selectKG,changeGiven;
TextArea itemsBought = new TextArea(10, 12);

double cost = 0.0f;

// set up GUI
public Tesco () {

// The Title Bar Display
super( "Tesco Suppermarket" );

//____________________________________________________________________________________________________________________________________________
String choises[] = {"", "Baked Beans", "CornFlakes", "Sugar","Tea Bags", "Instant Coffe", "Bread", "Sausages", "Eggs", "Milk", "Patatoes"};
String quantity[] = {"", "1", "2", "3", "4","5","6","7","8","9"} ;

//____________________________________________________________________________________________________________________________________________

setLayout ( new FlowLayout(FlowLayout.LEFT,10,20)) ; // Method llows more than one command button to be entered
setFont(new Font("Courier", Font.BOLD, 20)); // change font
setBackground(Color.red);
//---------------------------------------------------------------------------------------------------------------

//***************************************************
display = new Label ("Please Select One Of The Items");
display.setBackground(Color.yellow);
selectItem.addItem("Baked Beans");
selectItem.addItem("CornFlakes ");
selectItem.addItem("Sugar ");
selectItem.setBackground(Color.green);
add(display);
add(selectItem);
selectItem.addItemListener(this);
//selectItem.addActionListener(this);

//'''''''''''''''''''''''''''''''''''''''''''''''''''''
display = new Label ("Please Select The Quantity");
display.setBackground(Color.yellow);
selectNo = new JComboBox(quantity);
selectNo.setBackground(Color.green);
selectKG = new TextField (" ");
add(display);add(selectNo);add(selectKG);
selectKG.setVisible(false);
//selectNo.addItemListener(this);
//selectNo.addActionListener(this);

//========================================================================
display = new Label ("Price Displayed For Items Selected");
display.setBackground(Color.yellow);
priceItem = new TextField("£ 0.00");
add(display);
add(priceItem);
priceItem.setEditable(false);
priceItem.setBackground(Color.white);

//¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬

display = new Label ("\n\n\n\n\n\nItems Bought\n\n\n\n");
itemsBought = new TextArea();
add(display);
add(itemsBought );
itemsBought.setEditable(false);
itemsBought.setBackground(Color.white);
//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

display = new Label ("Subtotal");
display.setBackground(Color.green);
subTotal = new TextField (" ");
add(display);
add(subTotal);
subTotal.setEditable(false);
subTotal.setBackground(Color.orange);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
display = new Label ("VAT");
display.setBackground(Color.green);
VAT = new TextField (" ");
add(display);
add(VAT);
VAT.setEditable(false);
VAT.setBackground(Color.orange);

display = new Label ("Price");
display.setBackground(Color.green);
totalPrice = new TextField (" ");
add(display);
add(totalPrice);
totalPrice.setEditable(false);
totalPrice.setBackground(Color.orange);


display = new Label ("Change");
display.setBackground(Color.green);
changeGiven = new TextField(" ");
add(display);
add(changeGiven);
changeGiven.setEditable(false);
changeGiven.setBackground(Color.orange);

show = new Button ("Add To Basket");
add(show);
show.setBackground(Color.red);





} // end constructor
public void paint (Graphics g) {

for (int i =0 ;i <basket.size() ;i++ )
{
g.drawString(basket.get(i) + &quot;&quot; + count, 50, 300+20*i);
}

}

public void insert (String s, double p, ArrayList a, int [] c, double []d) {


int i = a.indexOf(s);

if (i < 0)
{
a.add(s);
c[a.size()-1]++;
d[a.size()-1]=p;
}

else c++;

cost +=p;

String con =&quot;&quot;;

for (int j=0;j<basket.size() ; j++)
{
con += (basket.get(j) + &quot;\t\t\t\t\t\t\t&quot; + count[j] + &quot;\t\t\t\t\t\t\t&quot; + cost + &quot;\n&quot;);
//con += &quot;Bill is £ &quot; + cost;
itemsBought.setText(con);
}

}
public void actionPerformed (ActionEvent e) {


/*
String contents = &quot;&quot;;
String quantity = &quot;&quot;;
contents = (String) selectItem.getSelectedItem();
if (contents ==&quot;Patatoes&quot;)

{
selectKG.setVisible(true);
selectNo.setVisible(false);

}
else
{
selectKG.setVisible(false);
selectNo.setVisible(true);

}


quantity = (String) selectNo.getSelectedItem();

for(int j = 0; j < basket.size(); ++j) {


//contents += basket.get(j) + &quot;\n&quot; ;

// contents += &quot;\n\n&quot;;


}
//itemsBought.setText(contents);

for (int i = 0;i < basket.size() ;++i )

{
contents += basket.get(i);
}


*/


}



public void itemStateChanged (ItemEvent e)

{



insert (selectItem.getSelectedItem(), price [selectItem.getSelectedIndex()],basket,count,itemPrice);

// String contents = &quot;&quot;;
// String quantity = &quot;&quot;;
//contents = (String) selectItem.getSelectedItem();

/*
if (contents ==&quot;Patatoes&quot;)

{
selectKG.setVisible(true);
selectNo.setVisible(false);

}
else
{
selectKG.setVisible(false);
selectNo.setVisible(true);

}


quantity = (String) selectNo.getSelectedItem();

for(int j = 0; j < basket.size(); ++j) {


//contents += basket.get(j) + &quot;\n&quot; ;

// contents += &quot;\n\n&quot;;


}
//itemsBought.setText(contents);

for (int i = 0;i < basket.size() ;++i )

{
contents += basket.get(i);
}
*/

}




public static void main( String args[] ) {
Tesco window = new Tesco();

window.addWindowListener(
new WindowAdapter() {
public void windowClosing (WindowEvent e){
System.exit(0);
}

}
);

window.setSize( 820, 570 );
window.setVisible( true );


}




} // end class TimeTestWindow


 
// Inner class declarations used to create event handlers.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.BorderLayout;
public class Tesco2 extends JFrame implements ActionListener{


ArrayList basket = new ArrayList();
double [] itemPrice = new double [10];
int[] count = {0,0,0,0,0,0,0,0,0,0};
double [] price = {0.35,1.00,0.50,1.15,2.50,0.50,1.30,0.75,0.65,1.00};
private JLabel charLabel[] = new JLabel[121];
private JLabel charLabel140,charLabel141,charLabel142,charLabel143,charLabel144,charLabel145,charLabel146,charLabel147,charLabel148;
private JLabel charLabelB[] = new JLabel[30];
private JLabel displayItemName,displayItemQuantity,displayPrice,displayItemBought,displaySubtotal,displayVat,displayChange,welcome;

private JButton exitButton,addToBasket,show;
private JComboBox selectNameCB, selectQuantityCB;
String choises[] = {&quot;Baked Beans&quot;, &quot;CornFlakes&quot;, &quot;Sugar&quot;,&quot;Tea Bags&quot;, &quot;Instant Coffe&quot;, &quot;Bread&quot;, &quot;Sausages&quot;, &quot;Eggs&quot;, &quot;Milk&quot;, &quot;Patatoes&quot;};
String quantity[] = {&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;} ;
private JTextField priceItem,subTotal,VAT,totalPrice,selectKG,changeGiven;
private float totalPriceFloat;
JTextArea itemsBought;
//BorderLayout myBorderLayout;
JPanel northPanel, centerPanel, southPanel;
GridBagLayout gridBag;
GridBagConstraints c;
private Container myContainer;
double cost = 0.0f;

// set up GUI
public Tesco2 () {

// The Title Bar Display
super( &quot;Tesco Suppermarket&quot; );
totalPriceFloat = 0.0f;
gridBag = new GridBagLayout();
c = new GridBagConstraints();
//c.insets = new Insets(10,0,0,0);
//____________________________________________________________________________________________________________________________________________


//____________________________________________________________________________________________________________________________________________

//getContentPane().setLayout ( new FlowLayout(FlowLayout.LEFT,10,20)) ; // Method llows more than one command button to be entered
myContainer = getContentPane();
myContainer.setLayout(gridBag);
//myContainer.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
Font myFont = new Font(&quot;Courier&quot;, Font.BOLD, 18); // change font
Font myMiddleFont = new Font(&quot;Courier&quot;, Font.BOLD, 17); // change font
Font mySmallFont = new Font(&quot;Courier&quot;, Font.BOLD, 15); // change font
myContainer.setBackground(Color.red);
//---------------------------------------------------------------------------------------------------------------

//***************************************************
////private void addComponent( Component c,int row, int column, int width, int height )
//c.fill = GridBagConstraints.VERTICAL;
int lastTwo = charLabel.length - 1;
for (int i=0; i<lastTwo; i++)
{
charLabel = new JLabel(&quot;e&quot;);
if ((i+1)%10==0)
charLabel.setForeground(Color.green);
//charLabel.setFont(myFont);

charLabel.setForeground(Color.red); //remark this for seeing my ruler
addComponent(charLabel,0,i,1,1);
}
charLabel[lastTwo] = new JLabel(&quot;e&quot;);
charLabel[lastTwo].setFont(myFont);
addComponent(charLabel[lastTwo],0,lastTwo,-999,1);
charLabel[lastTwo].setForeground(Color.red); //remark this for seeing my ruler

for (int i=0; i<charLabelB.length; i++)
{
charLabelB = new JLabel(&quot;e&quot;);
charLabelB.setForeground(Color.green);
charLabelB.setForeground(Color.red);//remark this for seeing my ruler
charLabelB.setFont(myFont);
}

displayItemName = new JLabel (&quot;Please Select One Of The Items&quot;,JLabel.LEFT);
displayItemName.setOpaque(true);
displayItemName.setBackground(Color.yellow);
displayItemName.setFont(myFont);
displayItemName.setBorder(BorderFactory.createEmptyBorder(0,5,0,28));
//public static Border createEmptyBorder(int top,int left,int bottom,int right);
addComponent(displayItemName,1,1,52,1);

selectNameCB = new JComboBox(choises);
addComponent(selectNameCB,1,49,20,1);
selectNameCB.setFont(mySmallFont);
selectNameCB.setBackground(Color.green);
selectNameCB.addActionListener(this);

//'''''''''''''''''''''''''''''''''''''''''''''''''''''
displayItemQuantity = new JLabel (&quot;Please Select The Quantity&quot;);
displayItemQuantity.setOpaque(true);
displayItemQuantity.setBackground(Color.yellow);
displayItemQuantity.setFont(myFont);
displayItemQuantity.setBorder(BorderFactory.createEmptyBorder(0,2,0,2));
addComponent(displayItemQuantity,1,67,50,1);

selectQuantityCB = new JComboBox(quantity);
selectQuantityCB.setFont(mySmallFont);
selectQuantityCB.setMaximumRowCount(10);
addComponent(selectQuantityCB,1,105,-999,1);
selectQuantityCB.setBackground(Color.green);
selectQuantityCB.addActionListener(this);

selectKG = new JTextField (&quot;! &quot;);

//addComponent(selectKG,1,9,1,1);

//selectKG.setVisible(true);
//selectNo.addItemListener(this);
//selectNo.addActionListener(this);

//========================================================================
displayPrice = new JLabel (&quot;Price Displayed For Items Selected&quot;);
displayPrice.setOpaque(true);
displayPrice.setBackground(Color.yellow);
displayPrice.setFont(myFont);
displayPrice.setBorder(BorderFactory.createEmptyBorder(0,4,0,2));
addComponent(displayPrice,2,2,50,1);
charLabel140 = new JLabel(&quot;|&quot;);
charLabel140.setForeground(Color.red);
addComponent(charLabel140,2,49,2,1);

c.fill = GridBagConstraints.HORIZONTAL;
priceItem = new JTextField(&quot;? &quot;+String.valueOf(price[0]));
priceItem.setFont(myFont);
addComponent(priceItem,2,50,18,1);
priceItem.setEditable(true);
priceItem.setBackground(Color.white);

c.fill = GridBagConstraints.NONE;
addComponent(charLabelB[0],2,120,-999,1); // to finish adding column in this row

displayItemBought = new JLabel (&quot; Items Bought&quot;,JLabel.RIGHT);
displayItemBought.setFont(myFont);
//addComponent(displayItemBought,3,0,5,1);
addComponent(displayItemBought,3,2,50,1);

c.fill = GridBagConstraints.BOTH;
itemsBought = new JTextArea(&quot;&quot;,12,30);
itemsBought.setFont(myMiddleFont);
JScrollPane scroll = new JScrollPane(itemsBought);
itemsBought.setEditable(true);
itemsBought.setBackground(Color.white);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
addComponent(scroll,4,5,100,12);

addComponent(charLabelB[1],4,120,-999,1);


addComponent(charLabelB[2],5,120,-999,1);

addComponent(charLabelB[3],6,120,-999,1);

addComponent(charLabelB[4],7,120,-999,1);

addComponent(charLabelB[5],8,120,-999,1);

addComponent(charLabelB[6],9,120,-999,1);

addComponent(charLabelB[7],10,120,-999,1);

addComponent(charLabelB[8],11,120,-999,1);

addComponent(charLabelB[9],12,120,-999,1);

addComponent(charLabelB[10],13,120,-999,1);

addComponent(charLabelB[11],14,120,-999,1);

addComponent(charLabelB[12],15,120,-999,1);

addComponent(charLabelB[13],16,120,-999,1);
c.fill = GridBagConstraints.NONE;

JLabel displayItemName2 = new JLabel (&quot;Please Select One Of The Items&quot;,JLabel.LEFT);
displayItemName2.setOpaque(true);
displayItemName2.setBackground(Color.yellow);
displayItemName2.setFont(myFont);
displayItemName2.setBorder(BorderFactory.createEmptyBorder(0,5,0,28));
//public static Border createEmptyBorder(int top,int left,int bottom,int right);
//addComponent(displayItemName2,19,1,52,1);

displaySubtotal = new JLabel (&quot;Subtotal&quot;);
displaySubtotal.setOpaque(true);
displaySubtotal.setFont(myMiddleFont);
displaySubtotal.setBackground(Color.green);
displaySubtotal.setBorder(BorderFactory.createEmptyBorder(0,1,0,1));
addComponent(displaySubtotal,19,2,16,1);


//&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot;

charLabel141 = new JLabel(&quot;|&quot;);
charLabel141.setForeground(Color.red);
addComponent(charLabel141,19,15,1,1);
c.fill = GridBagConstraints.HORIZONTAL;

subTotal = new JTextField (&quot;&quot;);
subTotal.setEditable(false);
subTotal.setOpaque(true);
subTotal.setFont(mySmallFont);
subTotal.setBackground(Color.orange);
addComponent(subTotal,19,16,10,1);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
c.fill = GridBagConstraints.NONE;

//charLabel142 = new JLabel(&quot;|&quot;);
//charLabel142.setForeground(Color.green);
//addComponent(charLabel142,20,25,1,1);

displayVat = new JLabel (&quot;VAT&quot;);
displayVat.setOpaque(true);
displayVat.setFont(mySmallFont);
displayVat.setBorder(BorderFactory.createEmptyBorder(1,2,1,2));
displayVat.setBackground(Color.green);
addComponent(displayVat,19,27,6,1);

c.fill = GridBagConstraints.NONE;
charLabel143 = new JLabel(&quot;|&quot;);
charLabel143.setForeground(Color.red);
addComponent(charLabel143,19,33,1,1);

c.fill = GridBagConstraints.HORIZONTAL;
VAT = new JTextField (&quot; &quot;);
addComponent(VAT,19,34,10,1);
VAT.setFont(mySmallFont);
VAT.setEditable(true);
VAT.setBackground(Color.orange);

displayPrice = new JLabel (&quot;Price&quot;);
displayPrice.setOpaque(true);
displayPrice.setFont(mySmallFont);
displayPrice.setBackground(Color.green);
displayPrice.setBorder(BorderFactory.createEmptyBorder(1,2,1,2));
addComponent(displayPrice,19,46,6,1);

c.fill = GridBagConstraints.HORIZONTAL;
totalPrice = new JTextField (&quot;&quot;);
totalPrice.setEditable(false);
totalPrice.setFont(mySmallFont);
totalPrice.setBackground(Color.orange);
addComponent(totalPrice,19,54,14,1);

displayChange = new JLabel(&quot;Change&quot;);
displayChange.setOpaque(true);
displayChange.setFont(mySmallFont);
displayChange.setBackground(Color.green);
displayChange.setBorder(BorderFactory.createEmptyBorder(1,2,1,2));
addComponent(displayChange,19,70,8,1);

//display.setBackground(Color.green);
// changeGiven = new TextField(&quot; &quot;);
c.fill = GridBagConstraints.HORIZONTAL;
changeGiven = new JTextField (&quot;&quot;);
changeGiven.setEditable(false);
changeGiven.setFont(mySmallFont);
changeGiven.setBackground(Color.orange);
addComponent(changeGiven,19,80,14,1);

c.fill = GridBagConstraints.NONE;
addToBasket = new JButton (&quot;Add To Basket&quot;);

addComponent(addToBasket,19,98,18,1);
addToBasket.setFont(mySmallFont);
addToBasket.addActionListener(this);
addToBasket.setBackground(Color.red);

} // end constructor
private void addComponent( Component myComponent,int row, int column, int width, int height )
{

// set gridx and gridy
c.gridx = column;
c.gridy = row;

// set gridwidth and gridheight
if (width==-999)
{
c.gridwidth = c.REMAINDER;
}
else
{c.gridwidth = width; }
c.gridheight = height;

// set constraints
gridBag.setConstraints( myComponent, c );
myContainer.add( myComponent ); // add component
}


public void insert (String s, double p, ArrayList a, int [] c, double []d) {


int i = a.indexOf(s);

if (i < 0)
{
a.add(s);
c[a.size()-999]++;
d[a.size()-999]=p;
}

//else c++;

cost +=p;

String con =&quot;&quot;;

for (int j=0;j<basket.size() ; j++)
{
con += (basket.get(j) + &quot;\t\t\t\t\t\t\t&quot; + count[j] + &quot;\t\t\t\t\t\t\t&quot; + cost + &quot;\n&quot;);
//con += &quot;Bill is ? &quot; + cost;
itemsBought.setText(con);
}

}
public void actionPerformed (ActionEvent e)
{
int myNameIndex=0,myQuantityIndex=0;
myNameIndex = selectNameCB.getSelectedIndex();
myQuantityIndex = Integer.parseInt(quantity[selectQuantityCB.getSelectedIndex()],10);
//selectNameCB, selectQuantityCB;
Object ObjJustClicked = (Object)(e.getSource());
if (ObjJustClicked == selectNameCB)
{
//myNameIndex = ((JComboBox)ObjJustClicked).getSelectedIndex();
priceItem.setText(&quot;? &quot;+String.valueOf(price[myNameIndex]));
}
else
{
if (ObjJustClicked == selectQuantityCB)
System.out.println(&quot;selectQuantityCB clicked&quot;);
else
if (ObjJustClicked == addToBasket)
{
itemsBought.append( String.valueOf(choises[myNameIndex])+&quot; X &quot;+String.valueOf(myQuantityIndex)+&quot;\n&quot; );

totalPriceFloat += price[myNameIndex]*myQuantityIndex;

totalPriceFloat*=100f;
totalPriceFloat=Math.round(totalPriceFloat)/100f;

System.out.println(totalPriceFloat);

totalPrice.setText(&quot;? &quot;+String.valueOf(totalPriceFloat));
subTotal.setText(&quot;? &quot;+String.valueOf(price[myNameIndex]*myQuantityIndex));
}
}

/*
String contents = &quot;&quot;;
String quantity = &quot;&quot;;
contents = (String) selectItem.getSelectedItem();
if (contents ==&quot;Patatoes&quot;)

{
selectKG.setVisible(true);
selectNo.setVisible(false);

}
else
{
selectKG.setVisible(false);
selectNo.setVisible(true);

}


quantity = (String) selectNo.getSelectedItem();

for(int j = 0; j < basket.size(); ++j) {


//contents += basket.get(j) + &quot;\n&quot; ;

// contents += &quot;\n\n&quot;;


}
//itemsBought.setText(contents);

for (int i = 0;i < basket.size() ;++i )

{
contents += basket.get(i);
}


*/


}

public static void main( String args[] ) {
Tesco2 window = new Tesco2();

window.addWindowListener(
new WindowAdapter() {
public void windowClosing (WindowEvent e){
System.exit(0);
}

}
);

window.setSize( 820, 600 );
window.setVisible( true );
}
} // end class TimeTestWindow
// Please give me a star by click Thanks prosper for this valuable post if code is suitable for you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top