Here is an update on my code since i havent posted here in a while.
My Product superclass. No compiler errors
My ProductList class. I get a compiler error at line 26. Saying the Product class cannot be instantiated. I know this, but how would I create a product then add it to the list if I can instantiate the Product class. I thought about doing it from the subclasses, either MusicCD or MovieDVD. But then I could only do one or the other, not both. I have marked the error in red.Code:package AssignmentTwo; public abstract class Product{ protected String code; protected String name; protected double price; protected String playingTime; public Product(){ } public Product(String code, String name, double price, String playingTime) { this.code = code; this.name = name; this.price = price; this.playingTime = playingTime; } public void setCode(String c) { code = c; } public String getCode() { return code; } public void setName(String n) { name = n; } public String getName() { return name; } public void setPrice(double p) { price = p; } public double getPrice() { return price; } public void setPlayingTime(String t) { playingTime = t; } public String getPlayingTime() { return playingTime; } public String showInfo() { return this.code + "\t" + this.name + "\t" + this.price + "\t" + this.playingTime + "\t" ; } // protected abstract String showInfo();
My MusicCD class. No compiler errors.Code:package AssignmentTwo; import java.util.*; public class ProductList{ ArrayList products = new ArrayList(); public ProductList() { } public void add(Product p) {//add a product to the list String key = ((Product)p).getCode(); if(find(key) == null) { products.add(p); } } public void add(String id, String name, double price, String playingTime) {//create a Product and add it to the list for(int i = 0 ; i<products.size(); i++) { Product prod1 = new Product(id,name,price, playingTime);//Cannot Instantiate Product products.add(prod1); } } public int size() {//return how many are in the list return products.size(); } public Object get(int index) {//return the Product at position index return products.get(index); } public void remove(int index) {//remove the Product at position index products.remove(index); } public Object find(String find) {//return a Product based on the ID# Iterator it = products.iterator(); while(it.hasNext()) { Product q = (Product)it.next(); if (q.getCode().equals(find)) { return q; } } return null; } public Iterator it() { return products.iterator(); } public static void main(String[] args) { ProductList pr = new ProductList(); Product prod1 = new MusicCD("111","Highway to Hell",15,"1 Hour", "AC/DC", 11); Product prod2 = new MusicCD("222","Back In Black",15,"3 Hours", "AC/DC", 17); // Product prod3 = new MovieDVD("333","Rocky V",20,"2 Hours", "Stalone",); pr.add(prod1); pr.add(prod2); // pr.add(prod3); /* Iterator it = pr.it(); while (it.hasNext()) { Product p = (Product)it.next(); System.out.println(p.showInfo()); }*/ } }
My MovieDVD class. No compiler errors.Code:package AssignmentTwo; public class MusicCD extends Product{ private String artist; private int numberOfTracks; public MusicCD() { artist=""; numberOfTracks = 0; code= this.getCode(); name=this.getName(); price=this.getPrice(); playingTime=this.getPlayingTime(); } public MusicCD(String code, String name, double price, String playingTime, String a, int n) { super(code, name, price , playingTime); artist=a; numberOfTracks = n; } public String getArtist() { return artist; } public void setArtist(String a) { artist=a; } public int getNumberOfTracks() { return numberOfTracks; } public void setNumberOfTracks(int n) { numberOfTracks = n; } public String showInfo(){ return this.getName() + " " + this.getPrice() + " " + this.getPlayingTime()+ artist + " " + numberOfTracks + " " + this.getCode(); } }
And finally my ProdTest class. No compiler errors.Code:package AssignmentTwo; public class MovieDVD extends Product{ private String director; private int scenes; public MovieDVD() { director=""; scenes=0; code= this.getCode(); name=this.getName(); price=this.getPrice(); playingTime=this.getPlayingTime(); } public MovieDVD(String code, String name, double price, String playingTime, String d, int s) { super(code, name, price , playingTime); director=d; } public String getDirector() { return director; } public void setDirector(String d) { director=d; } public int getScenes() { return scenes; } public void setScenes(int s) { scenes=s; } public String showInfo(){ return this.getCode()+" " + this.getName() + " " + this.getPrice() + " " + this.getPlayingTime()+ director + " " + scenes ; } }
Code:package AssignmentTwo; import java.util.*; public class ProdTest { public static void main(String[] args) { ProductList pr = new ProductList(); Iterator it = pr.it(); while (it.hasNext()) { Product p = (Product)it.next(); System.out.println(p.showInfo()); } } }




Reply With Quote