繁体   English   中英

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

[英]How can i change a value in an generic 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;

    }
}

考试班

导入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练习。 您想在一个集合中访问两种不同的类型。 为此,它们都需要具有相同的接口或父对象。

如果使用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);
  }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM