我已经编写了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
?