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

时间:2019-05-24 00:31:47

标签: 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'类,因此我可以在arraylist中存储两种不同的类型。

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;

    }
}

测试班

import 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.");


  }
}

如何访问商品并减少其销售量? 谢谢

2 个答案:

答案 0 :(得分:0)

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

如果使用Abstract基类,则可以使用以下示例。
首先让我们创建一个抽象的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;
  }
}

现在,我们为类折扣提供一个可以同时折扣两个商品的列表商品。

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

答案 1 :(得分:-1)

java.util.List具有以下功能get(index)set(index, object),可以使用:

//1. get your item 
int index = 0;
Item item=content.get(index);

//2. create your discount
Discount discount=new Discount(); 
// Fill/process your discount object with the appropriate data

//3. replace the item with the discount in the list
content.set(index,discount);