gpt4 book ai didi

java - 按对象的给定构造函数值对对象的 ArrayList 进行排序

转载 作者:行者123 更新时间:2023-11-30 07:08:55 24 4
gpt4 key购买 nike

我的问题的焦点与 takeInventory() 方法有关。

您可以假设 InventoryDe​​momain() 方法是有效的(除了 takeInventory() 方法的实现)。

如果需要,您可以找到其他类(class) here .

我的 takeInventory() 方法的目标是对我的 list 进行排序,并报告 Product 的每个唯一实例的整数值类型。

这要通过名字来区分:

Product(name, cost)).

Product 相似名称应归为一组(不考虑成本)。

输出应该报告:

This shows the desired output.

我假设有一种方法可以对这些数据进行排序,它比我当前的方法更有效。但是,我不知道有一个。

import java.util.*;
public class InventoryDemo
{
public static void main(String [] args) {
ArrayList<Product> list = new ArrayList<Product>();
list.add(new Car("Jaguar", 1000000));
list.add(new Car("Neon", 17000));
list.add(new Tool("JigSaw", 149.18));
list.add(new Car("Jaguar", 110000));
list.add(new Car("Neon", 17500));
list.add(new Car("Neon", 17875.32));
list.add(new Truck("RAM", 35700));
list.add(new Tool("CircularSaw", 200));
list.add(new Tool("CircularSaw", 150));
list.add(new Tool("saw1", 200));
list.add(new Tool("saw2", 150));

if(list.get(9).compareTo(list.get(10)) == 0) {
System.out.println("\nBoth saws are of equal value.");
} else if(list.get(9).compareTo(list.get(10)) > 0) {
System.out.println("\nThe first saw is more expensive.");
} else {
System.out.println("\nThe second saw is more expensive.");
}

takeInventory(list);
}

public static void takeInventory(ArrayList<Product> list) {
int inventory[] = new int[list.size()];
int counter = 0;
for(Product token: list) {
for(int x = 0; x < list.size(); x++) {
if(token.compareTo(list.get(x)) == 0) {
inventory[counter] = 0;
} else {
counter++;
}
}
}

for(int token : inventory) {
System.out.println(token);
}
}
}

如果不是很清楚:

我想对我的 takeInventory() 方法进行补救。此方法的目的是对给定的 ArrayList 对象进行排序,并报告其唯一类型值的总成本。这在输出中得到了清楚的证明。输出的最后一个字符串文字是由我的 main() 方法中的条件生成的。其余部分由 takeInventory() 方法生成。

我确定我当前的 takeInventory() 工作。

最佳答案

我会 build 一个 Map<String, C>其中 C是包含数量 ( int ) 和成本 ( double ) 的辅助类。遍历产品列表,并针对每个产品:

  • 如果名称不在 map 中,则将该名称关联到new C(1, cost) .
  • 如果名称在 map 中,则将与名称关联的数量增加 1并将与名称相关的成本增加 cost .

最后遍历map并打印结果;然后你就完成了。

引用:http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

这是一些代码:

import java.util.*;

class Product {
private String name;
private double cost;

public Product (String name, double cost) {
this.name = name;
this.cost = cost;
}

public String getName() {
return name;
}

public double getCost() {
return cost;
}
}

class Car extends Product {
public Car(String name, double cost) {
super(name, cost);
}
}

class Truck extends Product {

public Truck(String name, double cost) {
super(name, cost);
}
}

class Tool extends Product {

public Tool(String name, double cost) {
super(name, cost);
}
}



class Entry {
private int quantity = 1;
private double cost;

public int getQuantity() {
return quantity;
}

public double getCost() {
return cost;
}

public Entry(double cost) {
this.cost = cost;
}

public void add (double cost) {
quantity++;
this.cost += cost;
}

@Override
public String toString() {
return ("Quantity = " + quantity + ", Total cost = " + cost);
}
}


public class Inventory {

static void takeInventory(List<Product> list) {
Map<String, Entry> map = new HashMap<>();

for (Product p : list) {
Entry e = map.get(p.getName());
if (e == null) {
map.put(p.getName(), new Entry(p.getCost()));
} else {
e.add(p.getCost());
}
}

for (String s : map.keySet()) {
System.out.print(s);
Entry e = map.get(s);
System.out.println(" " + e);
}
}

public static void main(String [] args) {
ArrayList<Product> list = new ArrayList<Product>();
list.add(new Car("Jaguar", 100000));
list.add(new Car("Neon", 17000));
list.add(new Tool("JigSaw", 149.18));
list.add(new Car("Jaguar", 110000));
list.add(new Car("Neon", 17500));
list.add(new Car("Neon", 17875.32));
list.add(new Truck("RAM", 35700));
list.add(new Tool("CircularSaw", 200));
list.add(new Tool("CircularSaw", 150));
list.add(new Tool("saw1", 200));
list.add(new Tool("saw2", 150));

takeInventory(list);
}
}

输出:

saw1 Quantity = 1, Total cost = 200.0
saw2 Quantity = 1, Total cost = 150.0
CircularSaw Quantity = 2, Total cost = 350.0
RAM Quantity = 1, Total cost = 35700.0
JigSaw Quantity = 1, Total cost = 149.18
Jaguar Quantity = 2, Total cost = 210000.0
Neon Quantity = 3, Total cost = 52375.32

关于java - 按对象的给定构造函数值对对象的 ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23699478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com