java - 如何更改通用数组列表中的值?

标签 java arraylist

这是一个关于“购物车”的代码,可以容纳“item”类的元素。我必须完成“元素”类并编写另一个可以降低元素价格的“折扣”类。

import java.util.ArrayList;

public class Shoppingcart extends Item {

  // all shopping carts:
  private ArrayList<Shoppingcart> allshoppingcart = new ArrayList<Shoppingcart>();
  //Items in the shopping cart:
  private ArrayList<Item> content = new ArrayList<Item>();
  // Counter for shopping carts
  private static int number;

  /**
   * Constructor
   */

  public Shoppingcart() {

    allshoppingcart .add(this);
    this.number = number ;
    this.number++;

  }

  /**
   * load something in Shoppingcart
   */
  public void load(Item i) {
    this.content.add(i);
  }

  /**
   * Sum of all items loaded in the shoppingcart
   *
   * @return sum of the content in the shopping cart
   */
  public double sumShoppingCart() {
    double sum = 0.0;
    for (Item i : content) {
      sum = sum + item.getPrice();
    }
    return sum;
  }

}

类“item”,这样我就可以在数组列表中存储两种不同的类型。

public class Item {

  // normal price of item
  protected double price;
  // Name of product
  protected String name;

  /**
   * setter for price and name
   */
  public void setPB(String name, double price) {
    this.name = name;
    this.price = price;

  }

  /**
   * getter for price
   */
  public double getPrice() {
    return this.price;
  }

  /**
   * getter for the name
   */
  public String getName() {
    return this.name;
  }
}

“折扣”类可减少某件商品,例如促销(特别优惠)。

public class Discount extends Item
{

    // instance variables - replace the example below with your own


    public Discount()
    {
        // initialise instance variables

    }


    public void makeSale(int percent){

        percent =(100-percent)/100;
        price = w.getPrice()*percent;

    }
}

测试类

导入java.util.ArrayList;

public class Test {

  public static void main(String[] args) {

    ArrayList<Item> content = new ArrayList<>();

    Item item = new Item();
    Item item1 = new Item();
    Item item2 = new Item();

    item.setPB("Steak", 100.00);
    item1.setPB("Water", 200.00);
    item2.setPB("Groceries", 300.00);

    content.add(item);
    content.add(item1);
    content.add(item2);

    System.out.println("The item has the value : " + item.getPrice() + " and the name: " + item.getName());
    System.out.println("There are : " + content.size() + " Item(s) in the shopping cart.");


  }
}

我如何访问商品并减少促销? 谢谢

最佳答案

这是一个典型的 OOP 练习。您想要访问单个集合中的两种不同类型。为此,它们都需要具有相同的接口(interface)或父级。

如果您使用抽象基类,您可以使用以下示例。
首先让我们创建一个抽象 Item 类:

abstract class Item {

  protected double price;
  protected String name;

  public Item(double price, String name) {
    this.price = price;
    this.name = name;
  }

  public double getPrice() {
    return this.price;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  abstract void discount(double amount);
}

以及从该基类扩展的两个类:

class Food extends Item {

  public Food(double price, String name) {
    super(price, name);
  }

  @Override
  void discount(double amount) {
    this.price -= amount;
  }
}

class Bevarage extends Item {

  public Bevarage(double price, String name) {
    super(price, name);
  }

  @Override
  void discount(double amount) {
    this.price -= amount;
  }
}

现在让我们创建一个类(class)折扣,其中有一个可以对这两种商品都打折的商品列表。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Discount {

  public void printAllContent(List<Item> items) {
      items.forEach(item -> System.out.printf("name = %s, price = %.00f \n", item.getName(), item.getPrice()));
  }

  public static void main(String[] args) {
    Discount card = new Discount();

    List<Item> items = Arrays.asList(
        new Food(3.0, "burgers"), new Food(9.0, "tomato"), new Food(8.0, "fries"),
        new Bevarage(9.0, "cola"), new Bevarage(12.0, "water"), new Food(2.0, "beer")
    );


    card.printAllContent(items);
    for (Item item : items) {
      item.discount(1.0);
    }

    card.printAllContent(items);
    // if you want to do different discounts for different items
    for(Item item: items) {
      if (item instanceof Bevarage) {
        item.discount(2.0);
      } else {
        item.discount(1.0);
      }
    }
    card.printAllContent(items);
  }
}

关于java - 如何更改通用数组列表中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284467/

相关文章:

java - 新手: Maven execution exception

java - 用值填充 List<List<String>>

java - 将形状对象的 ArrayList 绘制到框架

java - Java中实现接口(interface)的类可以声明为接口(interface)吗?

java - 康威的生命游戏更新(下一代)

java - 抛出新异常

java - 在 JasperReports 中使用 ArrayList 作为主要数据源 - 仅打印第一个元素

java - ArrayList 元素替换为新条目

java - 将 ArrayList 转换为 Map

java - 我需要在 ADFS 2.0 中添加证书的地方