- #1
Smiles1
- 8
- 0
For the life of me, I can't figure out why my program won't update the index variable. I've tried setting the index to 0, and then to -1, and to the size of the arrayList - 1. I'm trying to use the index to get the previous and next flashCard in the arrayList to show up.
I have been working on this for HOURS for the last few days and cannot figure out where or what I'm doing wrong. Please give me hints on getting my next and previous buttons to work with the delete. :-(
Thank you!
FlashCardList
FlashCardController
I have been working on this for HOURS for the last few days and cannot figure out where or what I'm doing wrong. Please give me hints on getting my next and previous buttons to work with the delete. :-(
Thank you!
FlashCardList
Code:
import java.util.ArrayList;
public class FlashCardList {
private ArrayList<FlashCard> flashCards;
private FlashCard flashCard;
private int index;
private int position = -1;
FlashCardList() {
this.flashCards = new ArrayList();
this.flashCards.add(new FlashCard("test"));
}
public void putFlashCard(String textColor) {
flashCard = new FlashCard(textColor);
flashCards.add(flashCard);
System.out.println(flashCards.toString());
if(flashCards.size() == 1){
this.index = index + 1;
}
else{
this.index = index++;
}
}
public FlashCard getFlashCard() {
if (this.index < flashCards.size()) {
System.out.println("Get current flashCard index " + this.index);
return flashCards.get(index);
} else {
this.index = index--;
System.out.println("Get current flashCard index " + index);
return flashCards.get(index);
}
}
public FlashCard getNextFlashCard() {
if (this.index < flashCards.size()) {
this.index = index++;
System.out.println("Get Next FlashCard index " + this.index);
return flashCards.get(this.index);
} else {
this.index--;
System.out.println("Get Next FlashCard index " + this.index);
return flashCards.get(index);
}
}
public FlashCard getPreviousFlashCard() {
if (this.index < flashCards.size()) {
System.out.println("Get previous flashCard index " + this.index);
return flashCards.get(index);
} else {
this.index = index--;
System.out.println("Get previous flashCard index " + this.index);
return flashCards.get(index);
}
}
public int getCurrentIndex(String displayText) {
for (int i = 0; i < flashCards.size(); i++) {
if (flashCards.get(i).getTextColor().equals(displayText)) {
position = i;
System.out.println(position);
break;
}
}
return position;
}
public void deleteFlashCard() {
this.flashCards.remove(index);
this.index = index--;
System.out.println("DELETE");
System.out.println(flashCards.toString());
}
public void exitApplication() {
System.exit(0);
}
}
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class FlashCardController {
private FlashCardListUI flashCardListUI;
private FlashCardList flashCardList;
FlashCardController(FlashCardListUI flashCardListUI, FlashCardList flashCardList) {
this.flashCardList = new FlashCardList();
this.flashCardListUI = new FlashCardListUI();
flashCardListUI.setCurrentFlashCard(this.flashCardList.getFlashCard().getTextColor());
class NextFlashCardButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ae) {
flashCardListUI.setCurrentFlashCard(flashCardList.getNextFlashCard().getTextColor());
}
}
class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
flashCardList.putFlashCard(flashCardListUI.getCurrentFlashCard());
}
}
class BackButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
flashCardListUI.setCurrentFlashCard(flashCardList.getPreviousFlashCard().getTextColor());
}
}
class DeleteButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
flashCardList.deleteFlashCard();
flashCardListUI.setCurrentFlashCard(flashCardList.getNextFlashCard().getTextColor());
}
}
class EditButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//int i = flashCardList.getCurrentIndex();
//System.out.println(i);
}
}
class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
flashCardList.exitApplication();
}
}
flashCardListUI.nextFlashCardButtonListener(new NextFlashCardButtonListener());
flashCardListUI.addFlashCardButtonListener(new AddButtonListener());
flashCardListUI.backButtonListener(new BackButtonListener());
flashCardListUI.deleteButtonListener(new DeleteButtonListener());
flashCardListUI.exitButtonListener(new ExitButtonListener());
flashCardListUI.editButtonListener(new EditButtonListener());
}
}