calling a class from another class
i know this is a basic enough question but i just cant seem to get it to work! ok the diagram for my project is attached.
i need to fill an item(object?) list in class StorageSystem. this is my StorageSystem code:
Code:
public class StorageSystem
{
static Item[] itemList;
public static int totalStored = 0;
int totalTime;
int choosenTime;
public StorageSystem(int totalIn)
{
totalStored = totalIn;
itemList = new Item[totalStored];
}
public void addItem(Item itemListIn)
{
itemList[totalStored] = itemListIn;
totalStored += 1;
}
public int calcPartyTime()
{
for(int i=0; i<totalStored; i++)
{
totalTime += itemList[i].playingTime;
}
return totalTime;
}
public int calcChoosenItemTime()
{
for(int i=0; i<totalStored; i++)
{
if(itemList[i].gotIt == true)
choosenTime += itemList[i].playingTime;
}
return choosenTime;
}
public void printList()
{
for(int i=0; i<totalStored; i++)
{
System.out.println(itemList[i].title);
}
}
}
so basic enough. here is the code for Item class
Code:
public abstract class Item
{
String title, comment;
int playingTime;
boolean gotIt = false;
float price;
public Item(String titleIn, int playingTimeIn)
{
title = titleIn;
playingTime = playingTimeIn;
}
public void setComment(String commentIn)
{
comment = commentIn;
}
public String getComment()
{
return comment;
}
public void setOwn(boolean gotItIn)
{
gotIt = gotItIn = true;
}
public boolean getOwn()
{
return gotIt;
}
public void setPrice(float priceIn)
{
price = priceIn;
}
public float getPrice()
{
return price;
}
public void print()
{
System.out.println(title);
System.out.println(comment);
System.out.println(playingTime);
System.out.println(gotIt);
System.out.println(price);
}
}
now item is abstract, so i have a CD class and a DVD class, i'm starting with the CD class which is here:
Code:
public class CD extends Item
{
String artist;
int numOfTracks;
public CD(String artistIn, String titleIn, int playingTimeIn, int tracksIn)
{
super(titleIn, playingTimeIn);
artist = artistIn;
numOfTracks = tracksIn;
}
public void print()
{
System.out.println("name" + artist);
System.out.println("title" + title);
System.out.println("playingTime" + playingTime);
System.out.println("numOfTracks" + numOfTracks);
}
}
now i have a TestMethod class in which im getting the user to enter in the data to fill the Item[] array found in StorageSystem, but i just cant seem to get this to work!! so... here is my TestMethod class!!
Code:
public class Test
{
public static void main(String args[])
{
int select;
String title, artist;
int playTime, tracks, count;
System.out.println("enter 1 for add new CD");
System.out.println("enter 2 for add new DVD");
System.out.println("enter 3 to calculate party time");
System.out.println("enter 4 to calculate chosen time");
System.out.println("enter 5 to print item list");
System.out.println("enter 6 to quit");
select = EasyIn.getInt();
CD newcd = new CD(artist,title,playTime,tracks);
switch (select)
{
case 1:
//StorageSystem.itemList[StorageSystem.totalStored] = EasyIn.getString()
System.out.println("enter title");
title = EasyIn.getString();
System.out.println("enter artist");
artist = EasyIn.getString();
System.out.println("enter play time");
playTime = EasyIn.getInt();
System.out.println("enter number of tracks");
tracks = EasyIn.getInt();
// StorageSystem.itemList[StorageSystem.totalStored].CD(artist,title,playTime,tracks);
System.out.println("result");
// for(int i=0; i<StorageSystem.totalStored; i++)
//StorageSystem.addItem(Item.Item(artist, title));
StorageSystem.itemList[count]
StorageSystem.printList();
break;
case 2:
case 3:
case 4:
case 5:
case 6:
break;
}
}
}
any help here would be greatly appreciated, i know theres a lot of code there. also i have tried to create an item list (CD newcd = new CD()) and it says cannot find constructor, where as if you look in CD class, the constructor is there!
thanks in advance for any help received :)
Moderator Edit: Code tags added