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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lists and arrays....

Status
Not open for further replies.

Philhelm

Programmer
Nov 3, 2001
1
0
0
US
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(&quot;Price for video(s):&quot;, 100, 100);
gr.drawString(Double.toString(totalPrice) + &quot;0&quot;, 100, 150);
}
}
 
There are a few ways you could do this but the simplest thing to do is to calculate a new total each time a list item is changed. See the code below.

public void itemStateChanged(ItemEvent check) {

//reset the total, our list has changed
totalPrice=0;

//get the list of selected items
int[] video = videoList.getSelectedIndexes();

//loop through each selected video and
//update the total
for(i = 0; i < video.length; ++i) {
totalPrice += videoPrice[video];
}
//display the total
System.out.println(&quot;total price=&quot;+totalPrice);
repaint();
}


Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top