The idea is about serialize this box code. The program is to build flashcard and I want use serializetion to saving the state of the flashapp boxes, when the application exits into a file, and load the file on startup. So I don't have to redo all the flashcard from the beginning. The box code is as follow:
import java.util.ArrayList;
import java.util.Iterator;
public class Box implements java.io.Serializable {
private ArrayList<Flashcard> cards;
private double prossibility;
private double pivot; //determine the box's selected scope [pivot - prossibility,pivot)
public Box(){
cards = new ArrayList<Flashcard>();
prossibility = 0.0;
pivot = 0.0;
}
public Box(double prossibility,double pivot){
this.cards = new ArrayList<Flashcard>();
this.prossibility = prossibility;
this.pivot = pivot;
}
public Box(ArrayList<Flashcard> cards){
this.cards = cards;
}
public ArrayList<Flashcard> getCards() {
return cards;
}
public void setProssibility(double prossibility){
this.prossibility = prossibility;
}
public double getProssibility(){
return prossibility;
}
public void setPivot(double pivot){
this.pivot = pivot;
}
public double getPivot(){
return pivot;
}
public void addCard(Flashcard card){
cards.add(card);
}
public int searchCard(String challenge){
Iterator<Flashcard> it = cards.iterator();
int index = 0;
while(it.hasNext()){
Flashcard temp = (Flashcard)it.next();
if(temp.getChallenge().equals(challenge)){
break;
}
++index;
}
if(index >= cards.size())
index = -1;
return index;
}
public void removeCard(String challenge){
int index = searchCard(challenge);
if(index >= 0)
cards.remove(index);
}
}
My flashcard class is like:
enum ESide{
FRONT, //challenge side
BACK //response side
};
public class Flashcard implements Cloneable{
private String challenge;
private String response;
private ESide side;
private int boxIndex;
public Flashcard(){
challenge = new String();
response = new String();
side = ESide.BACK;
boxIndex = 0;
}
public Flashcard(String challenge, String response, ESide side){
this.challenge = challenge;
this.response = response;
this.side = side;
this.boxIndex = 0;
}
public Flashcard(String challenge, String response){
this.challenge = challenge;
this.response = response;
this.side = ESide.BACK;
this.boxIndex = 0;
}
public void setChallenge(String challenge){
this.challenge = challenge;
}
public String getChallenge(){
return challenge;
}
public void setResponse(String response){
this.response = response;
}
public String getResponse(){
return response;
}
public void setSide(ESide side){
this.side = side;
}
public ESide getSide(){
return side;
}
public void setBoxIndex(int index){
this.boxIndex = index;
}
public int getBoxIndex(){
return boxIndex;
}
public void flipSide(){
if(side == ESide.BACK)
side = ESide.FRONT;
else
side = ESide.BACK;
}
public Object clone(){
Flashcard o = null;
try {
o = (Flashcard)super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return o;
}
public boolean equals(Object o){
if(this == o)
return true;
if(o == null)
return false;
if(!(o instanceof Flashcard))
return false;
Flashcard temp = (Flashcard)o;
if(!temp.challenge.equals(this.challenge) || !temp.response.equals(this.response)){
return false;
}
return true;
}
}
The serialize code I have done is like this:
import java.io.*;
public class Savingbox implements Serializable {
public static void main(String[] args){
Box e = new Box();
So how can I make the savingbox class save the result that the user just used?