Hi,
I having problems with loading created objects into an array, then being able to access the records later in the program.
The following is a listing of my current program. The code I have thus far, reads information from a file, builds the objects (DVD, Ipod, or Walkman, then loads the objects into an array. My code appears like it's working except that I can't look at what's in the objects in the arrays.
I'm new to Java, so any help you can give me will be greatly appreciated.
Thanks,
My code in question:
/********************************************************************
* Name: Inventory
* Purpose:
*
* 1.Create an abstract inventory class with 3 children classes called:
*
* a. DVDplayer
* b. IPod
* c. Walkman
*
* 2. In an input file, create some objects of each class- the object
* name should be the serial number of the object.
*
* 3. Create 3 instance variables of each child class with info that
* describes the object (Example: color, #of songs it holds, price).
*
* 4. Your program should read the input file and the output file should
* consist of a tally of:
*
* a. each type of object (DVD player, IPod and Walkman)
* b. the serial number of each of those objects
* c. the value of each type of stock
*
* Date: 08/06/2005
* Author: Bob Richardson
* References: None
*********************************************************************/
import java.text.*;
import cs1.Keyboard;
import java.text.NumberFormat;
import java.io.*;
abstract class MusicInv
{
String Description = " ";
int SerialNum = 0;
public MusicInv()
{
Description = " ";
SerialNum = 0;
}
}
class DVD extends MusicInv
{
String Color = " ";
double Price = 0.00;
public DVD()
{
Color = " ";
Price = 0.00;
}
public DVD(String DescriptionIn, int SerialNumIn, String ColorIn, double PriceIn)
{
this.Description = DescriptionIn;
this.SerialNum = SerialNumIn;
this.Color = ColorIn;
this.Price = PriceIn;
}
}
class Ipod extends MusicInv
{
String Color = " ";
double Price = 0.00;
int MaxSongs = 0;
public Ipod(String DescriptionIn, int SerialNumIn,
String ColorIn, double PriceIn, int MaxSongsIn)
{
this.Description = DescriptionIn;
this.SerialNum = SerialNumIn;
this.Color = ColorIn;
this.Price = PriceIn;
this.MaxSongs = MaxSongsIn;
}
}
class Walkman extends MusicInv
{
String Color = " ";
double Price = 0.00;
public Walkman(String DescriptionIn, int
SerialNumIn, String ColorIn, double PriceIn)
{
this.Description = DescriptionIn;
this.SerialNum = SerialNumIn;
this.Color = ColorIn;
this.Price = PriceIn;
}
}
class Inventory
{
public static void main(String args[]) throws IOException
{
char c;
final int MAX = 50;
String line = " ",
ColorIn = " ",
DescriptionIn = " ",
SerialNumStr = " ",
PriceStr = " ",
MaxSongsStr = " ";
int SerialNumIn = 0,
MaxSongsIn = 0,
i = 0;
double PriceIn = 0.00;
FileReader fr = new FileReader("Inventory.txt");
BufferedReader br = new BufferedReader(fr);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter("Invout.txt")));
String recout;
DVD[] DVDS = new DVD[MAX];
Ipod[] Ipods = new Ipod[MAX];
Walkman[] Walkmans = new Walkman[MAX];
while((line=br.readLine()) != null0)
{
i++;
String[] columns = line.split(" ");
DescriptionIn = columns[0];
SerialNumStr = columns[1];
ColorIn = columns[2];
PriceStr = columns[3];
MaxSongsStr = columns[4];
if (columns[0].equals("DVDPlayer"))
{
SerialNumIn = Integer.parseInt(SerialNumStr);
PriceIn = Double.parseDouble(PriceStr);
DVD DVDPlayer = new DVD(DescriptionIn, SerialNumIn,
ColorIn, PriceIn);
DVDS[i] = DVDPlayer;
System.out.println(DescriptionIn+" "+SerialNumIn+" "+ColorIn+" "+PriceIn+" "+DVDPlayer);
}
else
if (columns[0].equals("IPod"))
{
SerialNumIn = Integer.parseInt(columns[1]);
PriceIn = Double.parseDouble(columns[3]);
MaxSongsIn = Integer.parseInt(columns[4]);
Ipod IpodPlayer = new Ipod(DescriptionIn, SerialNumIn, ColorIn,
PriceIn, MaxSongsIn);
}
else
if (columns[0].equals("Walkman"))
{
SerialNumIn = Integer.parseInt(columns[1]);
PriceIn = Double.parseDouble(columns[3]);
Walkman WalkmanPlayer = new Walkman(DescriptionIn,
SerialNumIn, ColorIn, PriceIn);
}
}
for (DVD Description : DVDS)
{
[ System.out.println(Description);
}