编写实现CD对象的ArrayList的驱动程序

时间:2013-04-19 00:24:00

标签: arraylist wrapper instance-variables

我编写了CDException类和CD类,现在我正在尝试编写一个驱动程序类来运行其他类。我必须

  • 从用户
  • 中读取艺术家姓名
  • 从用户
  • 中读取相册名称
  • 阅读用户的价格
  • 阅读用户
  • 的库存量

打印他们写的内容,但只打印有效的条目。如果只是输入空白,则在打印时将忽略其他条目。

CD课程:

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;
        }

    }   
}

这适用于我的CDException课程

public class CDException extends Exception {

   private String message = "Cannot create CD";

   public CDException( ){

   }
   public String getMessage( ){
    return this.message;
   }
   public void setMessage (String message){
    this.message = message;
   }
 }

这是我到目前为止我的驱动程序类,但我需要将其修改为我可以询问并从用户读取艺术家姓名,专辑标题,CD的价格,以及库存和添加的数量它是ArrayList,但首先包装然后添加到ArrayList

 public class CDStore2{

  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

我如何以及在哪里写信要求用户输入标题,专辑,价格和库存数量?我是在CD类或驱动程序类中编写的吗?我将如何使用ArrayLists在那里写它?

0 个答案:

没有答案