Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,354 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,805 people online right now. Registration is fast and FREE... Join Now!




Problem with action listener

 

Problem with action listener

shangyi

3 Jul, 2009 - 11:54 PM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 28



Thanked: 3 times
My Contributions
hi guys, i have experimented with action listner and produced the following programme. However, i have this problem.

Firstly enter 2 items
item number: 1
item description : test
item price : 1
item quantity :2
click add

add another:
item number:2
item description: test2
item price:2
item quantity:2
click add

now click amend item, then search item number: 2
click amend. A popup will display amend item successful.
click search again, and enter item number 1
click amend. A pupup will display amend item sucessful. but displays it 2 times... which means the operations inside my annonymous action listner is repeated 2 times... why is that so? i only click the amend item button 1 time.
Can anyone help me please? I thank you greatly in advance.

it seems that the code is too long to be posted inside a code block.
here is the url to download the code
BookstoreInventory.java

This post has been edited by shangyi: 4 Jul, 2009 - 02:02 AM

User is offlineProfile CardPM
+Quote Post


bbq

RE: Problem With Action Listener

4 Jul, 2009 - 12:53 AM
Post #2

omgwtfbbq
Group Icon

Joined: 15 May, 2008
Posts: 724



Thanked: 64 times
Dream Kudos: 125
My Contributions
Here is the code - i have not had a look yet, will after dinner smile.gif

java
import java.util.Scanner;
import java.io.IOException;
class Item{
private int itemNumber;
private String itemDescription;
private double sellingPrice;
private int quantity;

Item(){
}
Item(int number, String description, double price, int quant){
itemNumber = number;
itemDescription = description;
sellingPrice = price;
quantity = quant;

}
public boolean equals(Item newItem){
boolean checkItemNumber = false;
boolean checkDescription = false;
if(itemNumber==newItem.itemNumber)
checkItemNumber= true;

if(itemDescription == newItem.itemDescription)
checkDescription = true;

if(checkItemNumber || checkDescription)
return true;
else
return false;
}

public int getItemNumber(){
return itemNumber;
}
public String getItemDescription(){
return itemDescription;
}
public double getSellingPrice(){
return sellingPrice;
}
public int getQuantity(){
return quantity;
}
public void setItemNumber(int number){
itemNumber = number;
}
public void setItemDescription(String text){
itemDescription = text;
}
public void setSellingPrice(double price){
sellingPrice = price;
}
public void setQuantity(int numbers){
quantity =numbers;
}


}

public class BookstoreInventory {
private int lastRecord;
public static Item[] itemsArray = new Item[100];
public static BookstoreInventory bookstore1 = new BookstoreInventory();

BookstoreInventory(){
lastRecord= 0;
}

public void setLastRecord(int lastIndex){
lastRecord = lastIndex;
}
public int getLastRecord(){
return lastRecord;
}

public static int menu(){
Scanner scanner = new Scanner(System.in);

System.out.println("1. Add new item");
System.out.println("2. Amend the details of an item");
System.out.println("3. Searcg for an item");
System.out.println("4. Enter a sales transaction");
System.out.println("5. Input stock recieved");
System.out.println("6. List details of all items.");
System.out.println("7. Exit Programe.");

System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
return choice;
}
public static Item[] addNewItem(){


;
boolean repeat = false;

Scanner scanner = new Scanner(System.in);
Scanner textScanner = new Scanner(System.in);

//first time execution is zero
for(int i =bookstore1.getLastRecord();i<itemsArray.length;i++){
repeat = false;
bookstore1.setLastRecord(i);

if(i==0){System.out.print(bookstore1.getLastRecord());
System.out.print("Enter Item Number: ");
int itemNumber = scanner.nextInt();

System.out.print("Enter Details of the item: ");
String description = textScanner.nextLine();

System.out.print("Enter Selling Price: RM ");
double price = scanner.nextDouble();

System.out.print("Quantity on hand: ");
int quantity = scanner.nextInt();

itemsArray[i] = new Item(itemNumber, description, price, quantity);

}
else{
bookstore1.setLastRecord(i);
do{
repeat = false;
System.out.print(bookstore1.getLastRecord());
System.out.print("Enter Item Number: ");
int itemNumber = scanner.nextInt();

System.out.print("Enter Details of the item: ");
String description = textScanner.nextLine();

System.out.print("Enter Selling Price: RM ");
double price = scanner.nextDouble();

System.out.print("Quantity on hand: ");
int quantity = scanner.nextInt();

Item temporaryArray = new Item(itemNumber, description, price, quantity);

for(int j=0; j<i;j++){
if(temporaryArray.equals(itemsArray[j]))
repeat = true;
}
if(repeat)
System.out.println("ERROR: This item is already added.");
else{
repeat= false;
itemsArray[i] = temporaryArray;
}
}while(repeat);
}


System.out.print("\n Do you want to continue adding more records? (y/n)");
String answer = scanner.next().toUpperCase();

char realAnswer = answer.charAt(0);
if(realAnswer == 'N'){
bookstore1.setLastRecord(i+1);
break;
}
}
return itemsArray;
}
public static void displayItems(Item[] filledArray){

for(Item displayItem : filledArray){
try{

System.out.println("Item Number :"+displayItem.getItemNumber());
System.out.println("Item Description :"+displayItem.getItemDescription());
System.out.printf("Item Selling Price: RM %.2f\n", displayItem.getSellingPrice());
System.out.println("Item Quantity: "+displayItem.getQuantity());
}
catch(NullPointerException e){
System.out.print("Press any key to continue:");
try{
System.in.read();
}catch(IOException ex){}

break;
}


}
}
public static void amendItems(Item[] filledArray){
Scanner scanner = new Scanner(System.in);
Scanner textScanner = new Scanner(System.in);
System.out.print("Enter Item number to begin search: ");
int itemNumber = scanner.nextInt();
int foundIndex=0;
int option;
boolean invalid= false;
boolean changeOtherValues=false;

for(int i=0;i<filledArray.length;i++){
if(itemNumber == filledArray[i].getItemNumber()){
foundIndex = i;
break;
}
else
foundIndex = -1;

}
System.out.println("Item Number :"+filledArray[foundIndex].getItemNumber());
System.out.println("Item Description :"+filledArray[foundIndex].getItemDescription()+ " 1. change item description");
System.out.printf("Item Selling Price: RM %.2f 2. change selling price\n", filledArray[foundIndex].getSellingPrice());
System.out.println("Item Quantity: "+filledArray[foundIndex].getQuantity()+" 3. change item quantity");
do{

System.out.print("\nEnter the number to change the fields: ");
option = scanner.nextInt();
if(option ==1){
System.out.print("Item Description : ");
String description = textScanner.nextLine();
itemsArray[foundIndex].setItemDescription(description);
}
else if(option ==2){
System.out.print("Item Price: ");
double price = scanner.nextDouble();
itemsArray[foundIndex].setSellingPrice(price);
}
else if(option ==3){
System.out.print("Item Quantity: ");
int quantity = scanner.nextInt();
itemsArray[foundIndex].setQuantity(quantity);
}
else{
invalid = true;
System.out.println("Enter only 1 or 2 or 3.");
}
System.out.printf("Do you want to change other fields of Item Number:%d? (y/n)", itemsArray[foundIndex].getItemNumber());
String answer = scanner.next().toUpperCase();

char realAnswer = answer.charAt(0);
if(realAnswer == 'Y')
changeOtherValues = true;
else
break;
}while(invalid || changeOtherValues);

}

public static void main (String[] args) {
Item[] items = new Item[100];
int choice = menu();
while(choice != 7){

if(choice == 1)
items = addNewItem();
else if(choice ==2)
amendItems(items);
else if(choice ==6)
displayItems(items);

choice = menu();
}

}


}

User is offlineProfile CardPM
+Quote Post

shangyi

RE: Problem With Action Listener

4 Jul, 2009 - 01:04 AM
Post #3

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 28



Thanked: 3 times
My Contributions
OH crap! gave you the wrong file! please re download new file >< sorry for the inconvenience caused.

http://maplefrenzy.x10hosting.com/dco/Book...eInventory.java

Sorry again
User is offlineProfile CardPM
+Quote Post

Tanira

RE: Problem With Action Listener

4 Jul, 2009 - 12:41 PM
Post #4

D.I.C Head
**

Joined: 30 May, 2009
Posts: 89



Thanked: 6 times
My Contributions
QUOTE(shangyi @ 4 Jul, 2009 - 01:04 AM) *

OH crap! gave you the wrong file! please re download new file >< sorry for the inconvenience caused.

http://maplefrenzy.x10hosting.com/dco/Book...eInventory.java

Sorry again


90% positive it's because you put the bamendItem actionListener inside the bSearchItem actionListener

Hope that helps!

User is offlineProfile CardPM
+Quote Post

shangyi

RE: Problem With Action Listener

4 Jul, 2009 - 05:33 PM
Post #5

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 28



Thanked: 3 times
My Contributions
thanks, i will go check it out =)
User is offlineProfile CardPM
+Quote Post

shangyi

RE: Problem With Action Listener

4 Jul, 2009 - 05:46 PM
Post #6

New D.I.C Head
*

Joined: 15 Jun, 2008
Posts: 28



Thanked: 3 times
My Contributions
wow it works! thumbs up for you!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 06:58PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month