please I need help on my java Applet

J

java10

//i will be grateful if someone can rewrite this code for me, what i
trying to do i the comment below, it is abiut a supermarket checkout
system ,customers shop,pay,receive change

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.applet.Applet;
//<APPLET code = shopping21.class width=400 height=250> </APPLET>

public class shopping21 extends Applet implements ActionListener {

private Choice food = new Choice();
private Choice number = new Choice();
private Button AddItem =new Button("Add");
private Button Show = new Button ("show cost");
private double [] price ={0.35, 1.00, 0.50, 1.15, 2.50, 0.50,
1.30, 0.75, 0.65, 1.00};
private static DecimalFormat twoDP = new
DecimalFormat("0.00");
private double bill=0;
private List list;
private String[] s;

public void init () {
add(food);
food.addItem("Select Item");
food.addItem("Beans");
food.addItem("cornflakes");
food.addItem("sugar");
food.addItem("Tea bag");
food.addItem("coffee");
food.addItem("Bread");
food.addItem("Sausage");
food.addItem("Eggs");
food.addItem("Milk");
food.addItem("Potatoes");


add(number);
number.addItem("----");
number.addItem("1");
number.addItem("2");
number.addItem("3");
number.addItem("4");
number.addItem("5");
number.addItem("6");
number.addItem("7");
number.addItem("8");
number.addItem("9");
number.addItem("10");
number.addItem("11");
number.addItem("12");
number.addItem("13");
number.addItem("14");
number.addItem("15");
number.addItem("16");
number.addItem("17");
number.addItem("18");
number.addItem("19");
number.addItem("20");

add(Show);

Show.addActionListener(this);
add(AddItem);
AddItem.addActionListener(this);

list= new List (10,true); //i am trying to list the product
selected by customer, but didn't work
list.add("Beans");
list.add("cornflakes");
list.add("sugar");
list.add("Tea bag");
list.add("coffee");
list.add("Bread");
list.add("Sausage");
list.add("Eggs");
list.add("Milk");
list.add("Potatoes");
add(list);
list.addActionListener(this);
s = list.getSelectedItems();
}

/**
* calculates the total of the sale from the lineItem subtotals
* @return total of the sale
*/
public double calcPayments() {
paymentsTotal = 0.0;
Iterator i = payments.iterator();
while (i.hasNext()) paymentsTotal += ((Payment)i.next()).getAmount();
return total;
}

/**
* calculates the total of the sale from the lineItem subtotals
* @return total of the sale
*/
public double calcTotal() {
total = 0.0;
Iterator i = lineItems.iterator();
while (i.hasNext()) total += ((LineItem)i.next()).calcTotal();
return total;
}

/**
* retrieves the cached total of the sale
* @return total of sale, 0.0 if calcTotal() has not yet been called
*/
public double getTotal() {
return total;
}

/**
* sets the status of the sale to compltete if payments equal price
* @exception Exce[tion thrown if payments do not equal price
*/
public void complete() throws Exception {
if (calcTotal() != calcPayments()) {
throw new Exception("Payments do not equal total price");
}
status = "Complete";
}
/** status of sale */
private String status = "Incomplete";

/**
* adds a line item for a quantity of a type of item
* @param product the type of item being sold
* @param qty the quantity of the item being sold
*/
public void addLineItem(Product product, int qty) throws Exception {
if (status.equals("Incomplete")) {
lineItems.add(new LineItem(product, qty));
}
else {
throw new Exception("Cannot add items to a completed sale");
}
}
//CashPayment Class
/**
* a subclass that extends the Payment class to represent cash payments
* @stereotype moment-interval
*/
public class CashPayment extends Payment {
/**
* mandatory value constructor requires values for all attributes needed
to
* put the new object into a valid state
*
* @param amount the amount tendered for this payment
*/
public CashPayment(double amount) {
super(amount);
}

/**
* amount tendered is what was passed into the constructor
* @return amount tendered
*/
public double getAmountTendered() {
return super.getAmount();
}

/**
* calculate the change to be given for a required amount
* @return value of change
*/
public double calcChange( double requiredAmount ) {
change = super.getAmount() - requiredAmount;
return change;
}

/**
* override superclass to calc actual amount based on change given
* @return amountTendered - change given
*/
public double getAmount() {
return super.getAmount() - change;
}

/** amopunt of change given */
private double change = 0.0;





//the line below may not necessary be in code





/** public void actionPerformed (ActionEvent e) {
s = list.getSelectedItems();
repaint();
if (e.getSource()== AddItem)
{
int index = food.getSelectedIndex();
int num = number.getSelectedIndex()+1;
bill +=price[index]*num;
}
else
{
JOptionPane.showMessageDialog(this, "please pay this amount £" +
twoDP.format(bill));
JOptionPane.showMessageDialog(this, "thank you for shopping and visit us
again");
JOptionPane.showMessageDialog(this, "I am ");

}

public void paint (Graphics g) {
for ((int i=0; i< s.lenght;i++){
g.drawSting(s(i),50, 150, 100+20*i);"
}*/

}
}

}
 
A

Andrew Thompson

//i will be grateful if someone can rewrite this code for me,

And anybody that chooses to help you would probably be grateful
if you posted an SSCCE[1].

After fixing the wrapped line, the misplaced closing comment
tag and the tab spacing, I discovered your code has 21 compilation
errors, including the lack of Payment and Product classes.

Please post an SSCCE in future.

[1] <http://www.physci.org/codes/sscce.jsp>
 
A

Ann

and a credit card

Andrew Thompson said:
//i will be grateful if someone can rewrite this code for me,

And anybody that chooses to help you would probably be grateful
if you posted an SSCCE[1].

After fixing the wrapped line, the misplaced closing comment
tag and the tab spacing, I discovered your code has 21 compilation
errors, including the lack of Payment and Product classes.

Please post an SSCCE in future.

[1] <http://www.physci.org/codes/sscce.jsp>
 
J

java10

Hi Andrew , i am very new to java that was the reason the whole code was in
mess but i don't understand what you meant by sscce.

dragonmen
 
J

java10

//i have stream line the code too some extent, when i compiled it there was
no error but what i want it to do is when it say "pay this amount" the user
can input an amount to pay then it will display the remaining change.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.applet.Applet;
//<APPLET code = Noname0.class width=400 height=250> </APPLET>

public class Noname0 extends Applet implements ActionListener {

private Choice food = new Choice();
private Choice number = new Choice();
private Button AddItem =new Button("Add");
private Button Show = new Button ("show cost");
private double [] price ={0.35, 1.00, 0.50, 1.15, 2.50, 0.50,
1.30, 0.75, 0.65, 1.00};
private static DecimalFormat twoDP = new
DecimalFormat("0.00");
private double bill=0;
private List list;
private String[] s;

public void init () {
add(food);
food.addItem("Select Item");
food.addItem("Beans");
food.addItem("cornflakes");
food.addItem("sugar");
food.addItem("Tea bag");
food.addItem("coffee");
food.addItem("Bread");
food.addItem("Sausage");
food.addItem("Eggs");
food.addItem("Milk");
food.addItem("Potatoes");


add(number);
number.addItem("----");
number.addItem("1");
number.addItem("2");
number.addItem("3");
number.addItem("4");
number.addItem("5");
number.addItem("6");
number.addItem("7");
number.addItem("8");
number.addItem("9");
number.addItem("10");
number.addItem("11");
number.addItem("12");
number.addItem("13");
number.addItem("14");
number.addItem("15");
number.addItem("16");
number.addItem("17");
number.addItem("18");
number.addItem("19");
number.addItem("20");

add(Show);

Show.addActionListener(this);
add(AddItem);
AddItem.addActionListener(this);

list= new List (10,true);
list.add("Beans");
list.add("cornflakes");
list.add("sugar");
list.add("Tea bag");
list.add("coffee");
list.add("Bread");
list.add("Sausage");
list.add("Eggs");
list.add("Milk");
list.add("Potatoes");
add(list);
list.addActionListener(this);
s = list.getSelectedItems();
}



public void actionPerformed (ActionEvent e) {
s = list.getSelectedItems();
repaint();
if (e.getSource()== AddItem)
{
int index = food.getSelectedIndex();
int num = number.getSelectedIndex()+1;
bill +=price[index]*num;
}
else
{
JOptionPane.showMessageDialog(this, "please pay this amount £" +
twoDP.format(bill));
JOptionPane.showMessageDialog(this, "thank you for shopping and visit us
again");
JOptionPane.showMessageDialog(this, "I am ");

}

};
}
 
J

J. F. Cornwall

java10 said:
Hi Andrew , i am very new to java that was the reason the whole code was in
mess but i don't understand what you meant by sscce.

dragonmen
That's his terse way of saying you should create a "Small Self-Contained
Compilable Code Example" to demonstrate the problem.

Go check out his web page, he does explain these things there. :)

Jim C (also still quite new in Java coding)
 
J

Joona I Palaste

java10 said:
Hi Andrew , i am very new to java that was the reason the whole code was in
mess but i don't understand what you meant by sscce.

It means something like "short simple complete compilable example". It's
an absolutely brilliant concept Andrew invented. It's a piece of Java
code that:

* Constitutes an entire Java program you can copy&paste into your own
computer,
* Compiles cleanly without any errors and does not depend on additional
libraries (other than any that are the cause of your entire problem, and
even then you should make them available),
* Demonstrates your problem,
* Is as short and simple as possible. This is the important bit. Do not
bother posting your entire 200 thousand line program if all you're
having problems with is one Exception in a very simple method. Isolate
the part that is at fault and encapsulate it with a complete, compilable
example Java program.
 
A

Andrew Thompson

It means something like "short simple complete compilable example".

Short, self contained, compilable example. Otherwise, spot on.

And to the OP, read the *link*. ( SheeEEsh ;-)
 
A

Andrew Thompson

//i have stream line the code too some extent, when i compiled it there was
no error but what i want it to do is when it say "pay this amount" the user
can input an amount to pay then it will display the remaining change.

Tabs, long lines, redundant elements in arrays..

The SSCCE document warns you against all three. Please
read it carefully before preparing future examples.

Having said that, this example *is* self-contained (the most
important bit) so I will give you a bit of a push in the right
direction with some code. It is up to you to do the research
needed to understand it. And no, I am still not sure I understand
what you are attempting. But anyway, let's jump straight to
'actionPerformed()'

....
public void actionPerformed (ActionEvent e) { .....
{
JOptionPane.showMessageDialog(this, "please pay this amount £" +
twoDP.format(bill));

JOptionPane.showMessageDialog(this,
"please pay this amount £" +twoDP.format(bill));

String tendered = JOptionPane.showInputDialog(this, "Payment of £");

/* You are dealing with monetary amounts as doubles, but you should
always treat them as int's..
http://www.xdweb.net/~dibblego/java/faq/answers.html#Q41 */
double amountTendered;
try {
amountTendered = Double.parseDouble(tendered);
double balance = amountTendered - bill;
JOptionPane.showMessageDialog(this,
"Your change is £" +twoDP.format(balance));
} catch (NumberFormatException nfe) {
// note how this statement is broken across several lines
// to make it shorter.
JOptionPane.showMessageDialog(this,
tendered + " is not recognised as a money value!");
}
....

HTH
 
J

java10

Thanks a million for your last post, after running the applet I noticed the
following.
a]
I cannot choose more than one products. whenever i tried choose something
by pressing the "add" or "show cost" buttons they just go straight to pay
this amount also if i didn't choose anything and click "add" it says pay
£0.35.

b]if i enter less money than total to pay it gives me a -£4.00 instead of
"amount not enough".



public void actionPerformed (ActionEvent e) {
if (e.getSource()!= AddItem)
{
JOptionPane.showMessageDialog(frame, "Sorry You Must Purchase
Something","Cannot be empty",JOptionPane.ERROR_MESSAGE);
}
else
if (e.getSource()== AddItem)
{
int index = food.getSelectedIndex();
int num = number.getSelectedIndex()+1;
bill +=price[index]*num;
}


JOptionPane.showMessageDialog(this, "please pay this amount £" +
twoDP.format(bill));

String tendered = JOptionPane.showInputDialog(this, "Payment of £");


double amountTendered;
try {
amountTendered = Double.parseDouble(tendered);
double balance = amountTendered - bill;
JOptionPane.showMessageDialog(this,
"Your change is £" +twoDP.format(balance));
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(this,
tendered + " is not recognised as a money value!");


}

}

}
 
A

Andrew Thompson

if (e.getSource()!= AddItem)
{
JOptionPane.showMessageDialog(frame, "Sorry You Must Purchase
Something","Cannot be empty",JOptionPane.ERROR_MESSAGE);

You seem to be ignoring the advice (tabs, line-wrap, the usual..)
I offered re posting code examples..
 
J

java10

actually pardon me for my mistake sir, the code is still behaving rather
very badly just as i described in my last post (mind my written style i
just finished watching
"MR BEAN ".
i will looking forward to hearing from you soon.
java10
 
J

java10

i posted a message and no one respond to it so i checking if {
everyone is still alive;
}
else
{
happy new Year in advance
}
}
 
A

Andrew Thompson

actually pardon me for my mistake sir, the code is still behaving rather
very badly just as i described in my last post (mind my written style i
just finished watching
"MR BEAN ".
i will looking forward to hearing from you soon.

I saw your other post and had to search for this one to realise what you
were referring to, since changing the title effectively started that
message as a new thread in my news client.

In answer to the question from the other thread. Since you have
consistently failed to follow the advice offered by myself and others
re posting code samples, I decided my time was better invested elsewhere.

Good luck with it.
 
C

Chris Uppal

java10 said:
i posted a message and no one respond to it so i checking if {
everyone is still alive;

Well, I /think/ I'm still alive. I admit I'd have a hard time proving it to a
sceptical observer...

I haven't seen a previous message from you; maybe something ate it ?

}
else
{
happy new Year in advance
}
}

And to you -- but why do you restrict it to the living ?

-- chris
 
J

java10

hi everyone i tried to compile this code returns no error but i tried to
view it on appletviewer it say "Start: applet not initialaized".
The compiled code is as follows, thanx


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.applet.*;
import java.applet.Applet.*;
import java.util.*;
//<Applet code=Shoppingx.class height=600 width=600></Applet>

public class Shoppingx extends JApplet implements ActionListener{
//define objects and variables
JComboBox myMenu;
JComboBox myMenuCopy2;
JComboBox myMenuCopy3;
JComboBox myMenuCopy4;


String descriptions1[] = new String[6];
double prices1[] = new double[6];
String descriptions2[] = new String[6];
double prices2[] = new double[6];
String descriptions3[] = new String[6];
double prices3[] = new double[6];
String descriptions4[] = new String[6];
double prices4[] = new double[6];

boolean remv;
TextArea myOrdertbl1;
TextArea tbl1copy;
TextArea myOrdertbl2;
TextArea tbl2copy;
TextArea myOrdertbl3;
TextArea tbl3copy;
TextArea myOrdertbl4;
TextArea tbl4copy;
Basket myBasket1;
Basket myBasket2;
Basket myBasket3;
Basket myBasket4;
Button clr;
Button clr2;
Button clr3;
Button clr4;
Button back;
Button back2;
Button back3;
Button back4;
Button editMenu;
Button getReceiptData;
Applet MenuTable = getAppletContext().getApplet("MenuTable");



public void init(){
JPanel myPanel = new JPanel();
JTabbedPane tabs = new JTabbedPane();
myBasket1 = new Basket();
myBasket2 = new Basket();
myBasket3 = new Basket();
myBasket4 = new Basket();
myOrdertbl1 = new TextArea(10,30);
tbl1copy = new TextArea(10,30);
myOrdertbl2 = new TextArea(10,30);
tbl2copy= new TextArea(10,30);
myOrdertbl3 = new TextArea(10,30);
tbl3copy = new TextArea(10,30);
myOrdertbl4 = new TextArea(10,30);
tbl4copy = new TextArea(10,30);

clr = new Button("Clear Order");
back = new Button("Put Back");
clr2 = new Button("Clear Order");
back2 = new Button("Put Back");
clr3 = new Button("Clear Order");
back3 = new Button("Put Back");
clr4 = new Button("Clear Order");
back4 = new Button("Put Back");
editMenu = new Button("Change menu details");
getReceiptData = new Button("Daily Receipts");
Label spacer = new Label();
Label spacer2 = new Label();
Label spacer3 = new Label();
Label spacer4 = new Label();
boolean remv=false;

myMenu = new JComboBox();
myMenuCopy2= new JComboBox();
myMenuCopy3= new JComboBox();
myMenuCopy4= new JComboBox();
JPanel myPanetbl1 = new JPanel();
myPanetbl1.setLayout(new GridLayout(2,3));
JPanel myPanetbl2 = new JPanel();
myPanetbl2.setLayout(new GridLayout(2,3));
JPanel myPanetbl3 = new JPanel();
myPanetbl3.setLayout(new GridLayout(2,3));
JPanel myPanetbl4 = new JPanel();
myPanetbl4.setLayout(new GridLayout(2,3));
JPanel myPaneKitch = new JPanel();
myPaneKitch.setLayout(new GridLayout(2,2));
JPanel myPaneButtons = new JPanel();
myPaneButtons.setLayout(new GridLayout(2,1));
JPanel myButtonsCopy2 = new JPanel();
myButtonsCopy2.setLayout(new GridLayout(2,1));
JPanel myButtonsCopy3 = new JPanel();
myButtonsCopy3.setLayout(new GridLayout(2,1));
JPanel myButtonsCopy4 = new JPanel();
myButtonsCopy4.setLayout(new GridLayout(2,1));
JPanel managerPanel = new JPanel();
managerPanel.setLayout(new GridLayout(2,1));
clr.addActionListener(this);
myMenu.addActionListener(this);
back.addActionListener(this);
clr2.addActionListener(this);
myMenuCopy2.addActionListener(this);
back2.addActionListener(this);
clr3.addActionListener(this);
myMenuCopy3.addActionListener(this);
back3.addActionListener(this);
clr4.addActionListener(this);
myMenuCopy4.addActionListener(this);
back4.addActionListener(this);
editMenu.addActionListener(this);
getReceiptData.addActionListener(this);

//put data into the arrays
for (int i=0;i<6;i++)
{
//descriptions1 = new String();
descriptions2 = new String();
descriptions3 = new String();
descriptions4 = new String();
}

descriptions1[0]="Pint of Lager @ £2.25";
descriptions1[1]="Glass of wine @ £1.75";
descriptions1[2]="Rump Steak @ £5.50";
descriptions1[3]="Salmon Salad @ £4.75";
descriptions1[4]="Sticky Toffee Pudding @ £2.25";
descriptions1[5]="Ice Cream @ £1.50";
prices1[0]=2.25;
prices1[1]=1.75;
prices1[2]=5.50;
prices1[3]=4.75;
prices1[4]=2.25;
prices1[5]=1.50;

descriptions2[0]="Pint of Lager @ £2.25";
descriptions2[1]="Glass of wine @ £1.75";
descriptions2[2]="Rump Steak @ £5.50";
descriptions2[3]="Salmon Salad @ £4.75";
descriptions2[4]="Sticky Toffee Pudding @ £2.25";
descriptions2[5]="Ice Cream @ £1.50";
prices2[0]=2.25;
prices2[1]=1.75;
prices2[2]=5.50;
prices2[3]=4.75;
prices2[4]=2.25;
prices2[5]=1.50;

descriptions3[0]="Pint of Lager @ £2.25";
descriptions3[1]="Glass of wine @ £1.75";
descriptions3[2]="Rump Steak @ £5.50";
descriptions3[3]="Salmon Salad @ £4.75";
descriptions3[4]="Sticky Toffee Pudding @ £2.25";
descriptions3[5]="Ice Cream @ £1.50";
prices3[0]=2.25;
prices3[1]=1.75;
prices3[2]=5.50;
prices3[3]=4.75;
prices3[4]=2.25;
prices3[5]=1.50;

descriptions4[0]="Pint of Lager @ £2.25";
descriptions4[1]="Glass of wine @ £1.75";
descriptions4[2]="Rump Steak @ £5.50";
descriptions4[3]="Salmon Salad @ £4.75";
descriptions4[4]="Sticky Toffee Pudding @ £2.25";
descriptions4[5]="Ice Cream @ £1.50";
prices4[0]=2.25;
prices4[1]=1.75;
prices4[2]=5.50;
prices4[3]=4.75;
prices4[4]=2.25;
prices4[5]=1.50;

//myMenu.addItem(MenuTable.data[1][0]);
for (int i=0;i<6;i++)
{
myMenu.addItem(descriptions1);
myMenuCopy2.addItem(descriptions2);
myMenuCopy3.addItem(descriptions3);
myMenuCopy4.addItem(descriptions4);
}
//layout screen
myPaneButtons.add(clr);
myPaneButtons.add(spacer);
myPaneButtons.add(back);
myPanetbl1.add(myPaneButtons);
myPanetbl1.add(myMenu);
myPanetbl1.add(myOrdertbl1);

myButtonsCopy2.add(clr2);
myButtonsCopy2.add(spacer2);
myButtonsCopy2.add(back2);
myPanetbl2.add(myButtonsCopy2);
myPanetbl2.add(myMenuCopy2);
myPanetbl2.add(myOrdertbl2);

myButtonsCopy3.add(clr3);
myButtonsCopy3.add(spacer3);
myButtonsCopy3.add(back3);
myPanetbl3.add(myButtonsCopy3);
myPanetbl3.add(myMenuCopy3);
myPanetbl3.add(myOrdertbl3);

myButtonsCopy4.add(clr4);
myButtonsCopy4.add(spacer4);
myButtonsCopy4.add(back4);
myPanetbl4.add(myButtonsCopy4);
myPanetbl4.add(myMenuCopy4);
myPanetbl4.add(myOrdertbl4);

myPaneKitch.add(tbl1copy);
myPaneKitch.add(tbl2copy);
myPaneKitch.add(tbl3copy);
myPaneKitch.add(tbl4copy);

managerPanel.add(editMenu);
managerPanel.add(getReceiptData);

tabs.addTab("Table 1", myPanetbl1);
tabs.addTab("Table 2", myPanetbl2);
tabs.addTab("Table 3", myPanetbl3);
tabs.addTab("Table 4", myPanetbl4);
tabs.addTab("Kitchen", myPaneKitch);
tabs.addTab("Manager", managerPanel);

myPanel.add(tabs);

myPanel.setSize(800,600);
//frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//frame.setVisible(true);
setContentPane(myPanel);

}// End of init method

public void actionPerformed(ActionEvent a){

if(a.getSource()==myMenu){
int i = myMenu.getSelectedIndex();
OrderLine line = new OrderLine(i, descriptions1,prices1);
if (remv){myBasket1.removeOrderLine(line);remv = false;
}else{myBasket1.addOrderLine(line);}
myOrdertbl1.setForeground(Color.black);
myOrdertbl1.setText(myBasket1.showContents());
tbl1copy.setForeground(Color.black);
tbl1copy.setText(myBasket1.showContents());
}
if(a.getSource()==clr){
myBasket1.clearIt();
myOrdertbl1.setForeground(Color.red);
myOrdertbl1.setText("Your order has been Cancelled");
tbl1copy.setForeground(Color.red);
tbl1copy.setText("Order has been Cancelled");
}
if(a.getSource()==back){remv=true;}

if(a.getSource()==myMenuCopy2){
int i = myMenuCopy2.getSelectedIndex();
OrderLine line = new OrderLine(i, descriptions2,prices2);
if (remv){myBasket2.removeOrderLine(line);remv = false;
}else{myBasket2.addOrderLine(line);}
myOrdertbl2.setForeground(Color.black);
myOrdertbl2.setText(myBasket2.showContents());
tbl2copy.setForeground(Color.black);
tbl2copy.setText(myBasket2.showContents());
}
if(a.getSource()==clr2){
myBasket2.clearIt();
myOrdertbl2.setForeground(Color.red);
myOrdertbl2.setText("Your order has been Cancelled");
tbl2copy.setForeground(Color.red);
tbl2copy.setText("Order has been Cancelled");
}
if(a.getSource()==back2){remv=true;}

if(a.getSource()==myMenuCopy3){
int i = myMenuCopy3.getSelectedIndex();
OrderLine line = new OrderLine(i, descriptions3,prices3);
if (remv){myBasket3.removeOrderLine(line);remv = false;
}else{myBasket3.addOrderLine(line);}
myOrdertbl3.setForeground(Color.black);
myOrdertbl3.setText(myBasket3.showContents());
tbl3copy.setForeground(Color.black);
tbl3copy.setText(myBasket3.showContents());
}
if(a.getSource()==clr3){
myBasket3.clearIt();
myOrdertbl3.setForeground(Color.red);
myOrdertbl3.setText("Your order has been Cancelled");
tbl3copy.setForeground(Color.red);
tbl3copy.setText("Order has been Cancelled");
}
if(a.getSource()==back3){remv=true;}

if(a.getSource()==myMenuCopy4){
int i = myMenuCopy4.getSelectedIndex();
OrderLine line = new OrderLine(i, descriptions4,prices4);
if (remv){myBasket4.removeOrderLine(line);remv = false;
}else{myBasket4.addOrderLine(line);}
myOrdertbl4.setForeground(Color.black);
myOrdertbl4.setText(myBasket4.showContents());
tbl4copy.setForeground(Color.black);
tbl4copy.setText(myBasket4.showContents());
}
if(a.getSource()==clr4){
myBasket4.clearIt();
myOrdertbl4.setForeground(Color.red);
myOrdertbl4.setText("Your order has been Cancelled");
tbl4copy.setForeground(Color.red);
tbl4copy.setText("Order has been Cancelled");
}
if(a.getSource()==back4){remv=true;}
//if(a.getSource()==editMenu){
//getAppletContext().showDocument();}
}//end actionPerformed
}//end applet definition

class OrderLine{
public int id;
public String description;
public double price;
public int quantity;
public OrderLine(int i, String des, double p){
this.id=i;
this.description=des;
this.price=p;
this.quantity=1;
}
}//end OrderLine Definition

class Basket{
ArrayList order = new ArrayList();

public void clearIt(){order.clear();}

public void addOrderLine(OrderLine line){
boolean there = false;
for(int j = 0; j<order.size();j++){
OrderLine read = (OrderLine)order.get(j);
if(read.id==line.id){there = true;
read.quantity++;//if an item is there already add one to the quantity
};
}//end loop
if(!there){order.add(line);}//otherwise add it to the order
}//end add OrderLine

public void removeOrderLine(OrderLine line){
for(int j = 0; j<order.size() ;j++){
OrderLine read=(OrderLine)order.get(j);
if(read.id==line.id)read.quantity--;
if(read.quantity<1){order.remove(read);
}
}


}//end removeOrderLine

public String showContents(){
String contents = new String();
double bill = 0;
for (int j = 0 ;j<order.size() ;j++ ){
OrderLine read=(OrderLine)order.get(j);
contents+=read.quantity+" "+read.description;
contents+="\n";
bill+=read.price*read.quantity;}
if(bill>0){contents+="\nTotal cost = £"+bill;
return contents;
}else{
return "You have nothing ordered" ;}
}//end showContents
}//end Basket
 
J

java10

i have read your note on the above topic but the code in my last post
2005-01-05 is correct (no error) and it is compilable, but i don't know
where the problem is given the error (Start: Applet not initialized).
 
F

femibabatunde

Mate, can you have a look at latest post and see if you can fix it for
me te error is [ Start: Applet not initialized]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top