loadingData and Saving data from a list..<_>..thx all!!pls help
i am doing a muisc Cd program..but i got stuck on saving and loading data from a list after inserting my data to the list. I have a general concept on saving and loading data .And i could sucessfully saving and loading the number of item from the list. but i totally fail how to saving and loading all the object from the list ...pls help....my code is post as following ..i post all my program to be here to let u understand what i am doing..thx
====================
>javac musicCdDemo.java
>Exit code: 0
>java musicCdDemo
Please enter your CD`s title : ivan
Do you have another Cd ? (Y/N) : y
Please enter your CD`s title : lll
Do you have another Cd ? (Y/N) : y
Please enter your CD`s title : joho
Do you have another Cd ? (Y/N) : y
Please enter your CD`s title : we are the world
Do you have another Cd ? (Y/N) : n
Number of items in the list: 4
Title: we are the world
Title: joho
Title: lll
Title: ivan
Saving Data to the List......
Loading Data from the List......
Number of Music Cd in SMOA : 4<<-- i could load my num of items in list sucessfully ,,but how i could load and save all the date from or in a list ....thx
>Exit code: 0
===================================
testing driver
import java.io.*;
publicclass musicCdDemo
{
SortedList musicCdList =new SortedList();
publicvoid saveDate()
{
try
{
File f =new File("jessica.txt");
FileOutputStream fos =new FileOutputStream(f);
ObjectOutputStream oos =new ObjectOutputStream(fos);
oos.writeObject(new Integer(musicCdList.getNumberOfItems()));
//how should i implented my code to save all the object to my list
//oos.writeObject(musicCdList.getMusicCd()); //<- i know i should do something on here...but i don`t know i need to do exactly to save all my musicCdList`s object..
oos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
publicvoid loadDate()
{
try
{
File g =new File("jessica.txt");
FileInputStream fis =new FileInputStream(g);
ObjectInputStream ois =new ObjectInputStream(fis);
//first : list size (Integer)
Integer i = (Integer)ois.readObject();// <<- i could load my numberOfitem from the list...
System.out.println("Number of Music Cd in SMOA : " + i);
//but how do i load all my object from the list..i don`t know what i should do here..
//pls help...
ois.close();
}catch (Exception ioe)
{
ioe.printStackTrace();
}
}
publicvoid insertCd()
{
String musicCdsTitle;
while (true)
{
String continueInsertCd ="Y";
ReadOperation theRo =new ReadOperation();
do
{
musicCdsTitle = theRo.readString("Please enter your CD`s title : ");
if(!musicCdList.isDuplicate(musicCdsTitle))
{
musicCdList.insert(new MusicCd(musicCdsTitle));//, muiseCdsArtistOrGroupName, validMuiseCdsYearOfRelease, muiseCdsMusicGenre, muiseCdsAComment));
}
else
{
System.out.println("\nYou got a duplicate Cd`s Title " +"'" + musicCdsTitle +"'"
+"\nwith one of the existed CD in SMOA."
+"\nHence, This cd can not be inserted to SMOA!!");
}
// saveToFile(MusicCdList);
continueInsertCd = theRo.readString("Do you have another Cd ? (Y/N) : ");
}while (continueInsertCd.equals("Y") || continueInsertCd.equals("y"));
System.out.println("\nNumber of items in the list: "
+ musicCdList.getNumberOfItems() +"\n" + musicCdList.toString() );
break;
}
}
publicstaticvoid main(String[] args)
{
musicCdDemo testing =new musicCdDemo();
testing.insertCd();
System.out.println("Saving Data to the List......");
testing.saveDate();
System.out.println("Loading Data from the List......\n");
testing.loadDate();
}
}
=====================================================
musieNode for my sortedList
publicclass MusicCdNode
{
private MusicCd cd;
private MusicCdNode next;
// Default constructor
public MusicCdNode()
// Description: Initialize the reference for the cd object and the value of next to null.
// Postcondition: cd = null; next = null;
{
cd =null;
next =null;
}
// Parameterized constructor
public MusicCdNode(MusicCd aCd)
// Description: Set the reference for the cd object according to the parameters and value of next to null.
// Postcondition: cd = aCd; next = null;
{
cd = aCd;
next =null;
}
public MusicCd getMusicCd()
{
returnnew MusicCd(cd.getCdTitle());
}
public MusicCdNode getNext()
{
return next;
}
publicvoid setMusicCd(MusicCd aCd)
{
cd =new MusicCd(aCd.getCdTitle());
}
publicvoid setNext(MusicCdNode aCd)
{
next = aCd;
}
}
========================================================
music cd object
// File: MusicCd.java
// Author: Chi Lun To (Ivan To)
// Created on: June 5, 2007
// Class Description
// The MusicCd class defines a music cd object that contain the CD`s title, CD`s artist/GroupName,
// CD`s yearOfRelease , Cd`s music genre, and any comment of the Cd`s. This class provides functions
// to access the musicCdsTitle, artistOrGroupName, yearOfRelease, musicGenre, and aComment variable.
// Class Invariant: All MusicCd objects have a string musicCdsTitle, string artistOrGroupName, integer yearOfRelease
// String musicGenre, and String aComment. A string type musicCdsTitle,artistOrGroupName, musicGenre,or aComment of "None"
// indicates no real name specified yet. A integer yearOfRelease of 1000 indicates no real years specific yet.
publicclass MusicCd
{
String theCdTitle;// the CD`s Title
// Default constructor
public MusicCd()
// Description: Initialize theCdTitle to empty string
// Postcondition: theCdTitle = " ";
{
theCdTitle =" ";
}//end constructor
// Parameterized constructor
public MusicCd(String aCdTitle)
// Description: Set theCdTitle according to the parameters
// Postcondition: theCdTitle = aCdTitle;
{
theCdTitle = aCdTitle;
}// end constructor
// Accessor function : getCdTitle( ) function
public String getCdTitle()
// Description: Method to return the theCdTitle
// Postcondition: the value of theCdTitle is returned
{
return theCdTitle;
}// end getCdTitle( ) function
// Mutator function: setCdTitle( ) function
publicvoid setCdTitle(String aCdTitle)
// Description: Method to set theCdTitle according to the parameter
// Postcondition: theCdTitle = aCdTitle;
{
theCdTitle = aCdTitle;
}// end setCdTitle( ) function
// toString( ) function
public String toString()
// Description: Method to return the theCdTitle
// Postcondition: the value of theCdTitle is returned as String
{
return("Title: " + theCdTitle );
}// end toString( ) function
}
=======================================================
for better understanding..thc
publicabstractclass ShellSortedList
{
protected MusicCdNode head;
protectedint numberOfItems;
public ShellSortedList()
{
head =null;
numberOfItems = 0;
}
publicint getNumberOfItems()
{
return numberOfItems;
}
public MusicCdNode getMusicCdNode()
{
return head;
}
publicboolean isEmpty()
{
return( numberOfItems == 0 );
}
publicboolean isDuplicate(String newCD)
{
boolean found =false;
MusicCdNode current = head;
for(int i=0; i < numberOfItems; i++)
{
if(current.getMusicCd().getCdTitle().equalsIgnoreCase(newCD))
{
System.out.println("Duplicate Cd is found !!");
found =true;
}
current = current.getNext();
}
return found;
}
public String toString()
{
String listString =" ";
MusicCdNode current = head;
for(int i=0; i < numberOfItems; i++)
{
listString += current.getMusicCd().toString() +"\n";
current = current.getNext();
}
return listString;
}
}
====================================================
inferface sortedList
publicinterface SortedListInterface<T>
{
//public SortedList();
publicvoid insert( T anElement );
}
=======================================================
publicclass SortedListextends ShellSortedListimplements SortedListInterface<MusicCd>
{
public SortedList()
{
super();
}
publicvoid insert( MusicCd aCd )
{
MusicCdNode cdNode =new MusicCdNode( aCd );
cdNode.setNext( head );
head = cdNode;
numberOfItems++;
}
}
=====================================================
reading operatio
import java.util.*;
publicclass ReadOperation{
public String readString(String userInstruction)
{
String aString =null;
try
{
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
aString = scan.nextLine();
}
catch (NoSuchElementException e)
{
//if no line was found
System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
}
catch (IllegalStateException e)
{
// if this scanner is closed
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aString;
}
publicchar readTheFirstChar(String userInstruction)
{
char aChar =' ';
String strSelection =null;
try
{
//char charSelection;
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
strSelection = scan.next();
aChar = strSelection.charAt(0);
}
catch (NoSuchElementException e)
{
//if no line was found
System.out.println("\nNoSuchElementException error occurred (no line was found) " + e);
}
catch (IllegalStateException e)
{
// if this scanner is closed
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aChar;
}
publicint readInt(String userInstruction){
int aInt = 0;
try{
Scanner scan =new Scanner(System.in);
System.out.print(userInstruction);
aInt = scan.nextInt();
}catch (InputMismatchException e){
System.out.println("\nInputMismatchException error occurred (the next token does not match the Integer regular expression, or is out of range) " + e);
}catch (NoSuchElementException e){
System.out.println("\nNoSuchElementException error occurred (input is exhausted)" + e);
}catch (IllegalStateException e){
System.out.println("\nIllegalStateException error occurred (scanner is closed)" + e);
}
return aInt;
}
}
thx all....
Message was edited by:
Ivan1238
Message was edited by:
Ivan1238

