I have a program in which one selects a group of videos from a list and the total price is displayed. My problem is that I want the program to add up the price of the selected movies as I select them on the list and subtract their costs from the total if I deselect the movie. I have a double array(double[] videoPrice) for the prices and another array (int[] video) since I needed an array in int form. Anyway, if I select one video, the price is correct. However, for each additional movie selected, the total price is the previous sum plus the sum of ALL selected movies, not just the one. Also, I don't know how to subtract the individual cost of the videos when I deselect them. I am pretty sure I'm close and that I need only a few corrections. My code is below. If anybody can help me I would greatly appreciate it. My problem area is in the itemStateChanged( ).
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Practice2 extends Applet implements ItemListener {
List videoList = new List(10, true);
double totalPrice = 0;
double[] videoPrice = {2.00, 2.00, 2.50, 2.00, 2.50, 2.50, 2.00, 2.00, 2.50, 2.00};
int i = 0;
public void init() {
add(videoList);
videoList.setMultipleMode(true);
videoList.addItemListener(this);
videoList.add("Aliens: $2.00"
videoList.add("Braveheart: $2.00"
videoList.add("Final Fantasy: The Spirits Within: $2.50"
videoList.add("Gladiator: $2.00"
videoList.add("Jurassic Park III: $2.50"
videoList.add("Planet of the Apes: $2.50"
videoList.add("Saving Private Ryan: $2.00"
videoList.add("The Terminator: $2.00"
videoList.add("Tomb Raider: $2.50"
videoList.add("Tombstone: $2.00"
}
public void itemStateChanged(ItemEvent check) {
int[] video = videoList.getSelectedIndexes();
for(i = 0; i < video.length; ++i)
totalPrice += (videoPrice[video]);
//if (videoList.isSelected(0))
//--totalPrice;
repaint();
}
public void paint(Graphics gr) {
gr.drawString("Price for video(s):", 100, 100);
gr.drawString(Double.toString(totalPrice) + "0", 100, 150);
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Practice2 extends Applet implements ItemListener {
List videoList = new List(10, true);
double totalPrice = 0;
double[] videoPrice = {2.00, 2.00, 2.50, 2.00, 2.50, 2.50, 2.00, 2.00, 2.50, 2.00};
int i = 0;
public void init() {
add(videoList);
videoList.setMultipleMode(true);
videoList.addItemListener(this);
videoList.add("Aliens: $2.00"
videoList.add("Braveheart: $2.00"
videoList.add("Final Fantasy: The Spirits Within: $2.50"
videoList.add("Gladiator: $2.00"
videoList.add("Jurassic Park III: $2.50"
videoList.add("Planet of the Apes: $2.50"
videoList.add("Saving Private Ryan: $2.00"
videoList.add("The Terminator: $2.00"
videoList.add("Tomb Raider: $2.50"
videoList.add("Tombstone: $2.00"
}
public void itemStateChanged(ItemEvent check) {
int[] video = videoList.getSelectedIndexes();
for(i = 0; i < video.length; ++i)
totalPrice += (videoPrice[video]);
//if (videoList.isSelected(0))
//--totalPrice;
repaint();
}
public void paint(Graphics gr) {
gr.drawString("Price for video(s):", 100, 100);
gr.drawString(Double.toString(totalPrice) + "0", 100, 150);
}
}