I have written the CDException class and the CD class, and now I'm trying to write a driver class to run the other classes. I have to
Read the artist name from user
Read the album name from user
Read the price from user
Read how many in stock from user
And print what they wrote but only the valid entries. Other entries will be ignored upon printing if they just enter blanks.
Here's what I have for my CD class
import java.text.*; public class CD { private String sArtist; private String sAlbum; private double dPrice = 0; private int inStock = 0; String decimalFormats = "$##.##"; public CD(String sArtist, String sAlbum, double dPrice, int inStock) throws Exception{//constructors this.setArtist(sArtist); this.setAlbum(sAlbum); this.setPrice(dPrice); this.setinStock(inStock); } public String toString( ){//to string method DecimalFormat df = new DecimalFormat(decimalFormats); //Formats the price String s = "Album: " + sAlbum + "\nArtist: " + sArtist + "\nPrice: " + df.format(dPrice); if(inStock == 0) return "Album: " + sAlbum + "\nArtist: " + sArtist + "\nPrice: $" + dPrice + "\nNot in Stock"; else return s + "\nCurrently " + inStock + " in stock"; } //mutator methods public void setArtist(String newArtist)throws Exception{ if(newArtist.length( ) == 0){ CDException cde = new CDException( ); cde.setMessage("Artist name contains no characters" + "\nCannot create CD"); throw cde; } else{ this.sArtist = newArtist; } } public void setAlbum(String newAlbum) throws Exception{ if(newAlbum.length( ) == 0){ CDException cde = new CDException( ); cde.setMessage("Album name contains no characters" + "\nCannot create CD"); throw cde; } else{ this.sAlbum = newAlbum; } } public void setPrice(double newPrice)throws Exception{ if(newPrice < 0){ CDException cde = new CDException( ); cde.setMessage("CD Price cannot be negative" + "\nCannot create CD"); throw cde; } else{ this.dPrice = newPrice; } } public void setinStock(int newInStock)throws Exception{ if(newInStock < 0){ CDException cde = new CDException( ); cde.setMessage("In stock cannot be negative" + "\nCannot create CD"); throw cde; } else{ this.inStock = newInStock; } } public String getsArtist( ){ return this.sArtist; } public String getsAlbum( ){ return this.sAlbum; } public double getdPrice( ){ return this.dPrice; } public int getinStock( ){ return this.inStock; } } }
And here is my Driver class, It needs to be modified so that way I can ask the user to enter the Artist name, album name, price of CD and how many in stock and wrap it up to add it into an ArrayList. If the user enters blanks, those will be ignored upon printing them. Only valid entries will be printed. I also have a CDException class that I wrote as well. This is frustrating me, any help would be much appreciated! I am very new to Java, so please bear with me as I may seem like I dont know what I'm talking about or doing.
public class CDStore{ public static void main (String[ ] arg) throws Exception{ ArrayList (CD) CD = new ArrayList( ); try{ CD cd1 = new CD("Muse", "The Resistance", 11.99, 20); System.out.println(cd1.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } try{ CD cd2 = new CD("Yanni", "The Live at the Acropolis", 11.99, 0); System.out.println(cd2.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } try{ CD cd3 = new CD("Muse", "Black Holes and Revelations", 10.99, 15); System.out.println(cd3.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } try{ CD cd4 = new CD("Paul Mc Cartney", "All the Best", -0.99, 12); System.out.println(cd4.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } try{ CD cd5 = new CD("Lady Gaga", "The Fame Monster", 14.99, 44); System.out.println(cd5.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } try{ CD cd6 = new CD("", "California Gurls", 1.29, 4); System.out.println(cd6.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } try{ CD cd7 = new CD("Pitbull", "", 11.99, 12); System.out.println(cd7.toString( )); System.out.println("========================="); } catch(CDException cde){ System.out.println(cde.getMessage( )); System.out.println("========================="); } }//main method ends } //program ends