Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 购物车类如何添加新项目?_Java - Fatal编程技术网

Java 购物车类如何添加新项目?

Java 购物车类如何添加新项目?,java,Java,在我的cart类中,我很难将新项目添加到数组中。我尝试SaleItem item=new SaleItem,但它给了我一个错误,因为我的SaleItem是一个抽象,我们应该保留这个抽象。Cart类中的一些部分是错误的,我希望我知道我在做什么。有什么建议吗?还是帮忙 这是方向: **Cart构造函数方法接受一个整数参数作为输入,该参数确定可以放置在购物车中的最大项目数。addItem,它接受五个输入参数,其中前四个输入表示要添加的项目的代码、标题、价格和数量。第五个输入参数给出项目的类型(1本书,

在我的cart类中,我很难将新项目添加到数组中。我尝试SaleItem item=new SaleItem,但它给了我一个错误,因为我的SaleItem是一个抽象,我们应该保留这个抽象。Cart类中的一些部分是错误的,我希望我知道我在做什么。有什么建议吗?还是帮忙

这是方向: **Cart构造函数方法接受一个整数参数作为输入,该参数确定可以放置在购物车中的最大项目数。addItem,它接受五个输入参数,其中前四个输入表示要添加的项目的代码、标题、价格和数量。第五个输入参数给出项目的类型(1本书,2本字典,3张有声CD)。根据要添加的项的类型,该方法要求用户提供其余实例变量的值。然后,该方法实例化相应的对象并将其添加到数组中。 cartTotalCost:返回购物车中所有项目的总成本的方法。getNumBooks:返回购物车中图书数量的方法。 GetNumDS:返回购物车中音频CD数量的方法。 toString:一种方法,它打印一个格式良好的购物车输出,其中每个商品都打印在一行中

销售项目类别:

public abstract class SaleItem {
private int code;
private String title;
private double price;
private int quantity;

public SaleItem(int code, String title, double price, int quantity){
this.code = code;
this.title = title;
this.price = price;
this.quantity = quantity;
}

//Getters
public int getCode(){
    return this.code;
}
public String getTitle(){
    return this.title;
}
public double getPrice(){
    return this.price;
}
public int getQuantity(){
    return this.quantity;
}
// Setters
public void setCode(int code){
    this.code = code;
}
public void setTitle(String title){
    this.title = title;
}
public void setPrice(double price){
    this.price = price;
}
public void setQuantity(int quantity){
    this.quantity = quantity;
}

//ItemTotalCost method
public double itemTotalCost(){
    double total = this.price * this.quantity;
    return total;
}

//ToString
public String toString(){
    String outputItemDes = "";
    outputItemDes += "Code: "+ this.code;
    outputItemDes += "Title: "+ this.title;
    outputItemDes += "Price: "+ this.price;
    outputItemDes += "Quantity: "+ this.quantity;
    outputItemDes += "Item Total Cost: " + this.itemTotalCost();
    return outputItemDes;
}
}

图书类别:

public class Book{
private String author;
private int numPages;

public Book(int code, String title, double price, int quantity, String author, int numPages){
    this.author = author;
    this.numPages = numPages;
}

//Getters
public String getAuthor(){
    return this.author;
}
public int getNumPages(){
    return this.numPages;
}

//Setters
public void setAuthor(String author){
    this.author = author;
}
public void setNumPages(int numPages){
    this.numPages = numPages;
}

//toString
@Override
public String toString(){
    String outputBookDes = "";
    outputBookDes += "Author: "+ this.author;
    outputBookDes += "Number Pages: "+ this.numPages;
    return outputBookDes;
}
}

音频CD类:

public class AudioCD extends SaleItem {
private String artist;
private String label;
private int playingTime;

public AudioCD(int code, String title, double price, int quantity, String artist, String label, int playingTime){
    super(code,title,price,quantity);
    this.artist = artist;
    this.label = label;
    this.playingTime = playingTime;
}

//Getters
public String getArtist(){
    return this.artist;
}
public String getLabel(){
    return this.label;
}
public int getPlayingTime(){
    return this.playingTime;
}

//Setters
public void setArtist(String artist){
    this.artist = artist;
}
public void setLabel(String label){
    this.label = label;
}
public void setPlayingTime(int playingTime){
    this.playingTime = playingTime;
}

//toSting method as in the Book class
@Override
public String toString(){
    String outputAudioDes = "";
    outputAudioDes += "Artist: "+ this.artist;
    outputAudioDes += "Label: "+ this.label;
    outputAudioDes += "Playing Time: "+ this.playingTime;
    return outputAudioDes;
}
}

购物车类别:

public class Cart {
// Variables
private SaleItem[] itemList;
private int numItems;
private Book book;
private Dictionary dictionary;
private AudioCD audioCD;

//Constructors
public Cart(int maxItems){
    this.numItems = 0;
    this.itemList = new SaleItem[maxItems];
}
//Setters
public void setBook(Book book){
    this.book = book;
}
public void setDictionary(Dictionary dictionary){
    this.dictionary = dictionary;
}
public void setAudioCD(AudioCD audioCD){
    this.audioCD = audioCD;
}
//Add Item method goes into the cart
public void addItem(int code, String title, double price, int quantity, int itemNum){
    int maxItems = this.itemList.length;

    if (itemNum == 1){
        System.out.println(book);
        }
    else if (itemNum == 2){
        System.out.println(dictionary);
    }
    else if (itemNum == 3){
        System.out.println(audioCD);
    }

    if (this.numItems < maxItems){
        SaleItem item = new SaleItem(code,title,price,quantity,itemNum);
        itemList[this.numItems] = item;
        this.numItems++;
    }
}
// Sums up all of the item in the cart
public double cartTotalCost(){
    double cartTotal = 0;
    for (int i = 0; i < itemList.length; i++){
        SaleItem item = itemList[i];
        double subTotal = (item.getPrice() * item.getQuantity());
        cartTotal += subTotal;
    }
    return cartTotal;
}
//Getters
public Book getNumBooks(){
    return book;
}
public AudioCD getNumCDS(){
    return audioCD;
}
public String toString(){
    String cartDes = "";
    for (int i = 0; i < itemList.length; i++){
        cartDes += itemList[i];
    }
    return cartDes;
}
公共类购物车{
//变数
私人销售物品[]物品清单;
私有内部网络;
私人书籍;
私人字典;
私人音频光盘;
//建设者
公共购物车(int maxItems){
this.numItems=0;
this.itemList=新SaleItem[maxItems];
}
//二传手
公共作废退册(书){
这本书;
}
公共字典(字典){
这本字典=字典;
}
公共无效设置AudioCD(AudioCD){
this.audioCD=audioCD;
}
//添加项目方法进入购物车
public void addItem(整数代码、字符串标题、双倍价格、整数数量、整数itemNum){
int maxItems=this.itemList.length;
如果(itemNum==1){
系统输出打印(图书);
}
else if(itemNum==2){
System.out.println(字典);
}
else if(itemNum==3){
系统输出打印LN(音频CD);
}
if(this.numItems
}第五个输入参数给出了项的类型(1本书、2本字典、3张CD)。根据要添加的项的类型,该方法要求用户提供其余实例变量的值

您不应该创建新的
SaleItem
,您应该使用第5个输入来确定您应该实例化并添加到购物车中的
SaleItem

您的代码
SaleItem item=新的SaleItem(代码、标题、价格、数量、itemNum)无论如何都是错误的


您的
SaleItem
构造函数是
public-SaleItem(int-code,String-title,double-price,int-quantity)
,因此您甚至没有传递正确数量的参数。

CD或书籍都是销售项目。不能调用SaleItem()构造函数-必须调用SaleItem实现的构造函数(如CD或书籍)。因此,如果您想制作一个新的销售项目,请首先确定要制作的类型,然后执行以下操作:

SaleItem item = new Book(0, "BookTitle", 5.99, 1, "Author", 500);
显然,本例中的论点将取决于您试图创建的特定书籍


至于赋值,听起来您需要向用户询问在addItem方法中创建适当对象所需的所有信息。因此,对于一本书,你会询问作者、页数等。

你正在做的作业是试图向你介绍或加强你的继承技能
SaleItem
将保持抽象,因为待售商品应继承
SaleItem
的属性
SaleItem
不能像它是一张
AudioCD
那样被实例化

您可以将
AudioCD
称为
SaleItem
,因为
AudioCD
SaleItem
的子类。可以这样做:

SaleItem saleItem = new AudioCD(Object parameter, Object parameter2, ...);

查看文档或继承文档。

首先,该书必须扩展SaleItem

实例化适当SaleItem最简单的方法是在上面的ifs语句中: 如果itemNum==1 item=带有参数的新书()。 其他的也一样


为什么你们要在购物车里保存对特定书籍、字典和cd的引用?我看到他们的唯一用法是,你试图数一数购物车里的书的数量。如果这是你的意思,那就不是办法。您应该迭代items数组并计算书籍实例

请仅发布代码的相关部分。它似乎无法修复我的错误,因为我需要公共void addItem(int-code、String-title、double-price、int-quantity、int-itemNum)我在main中输入的任何参数都将添加到数组中,而不是通过在Cart类的方法中添加Book as。