java - Sorting through an ArrayList of objects by an object's given constructor value -
the focus of question pertains takeinventory()
method.
you can assume inventorydemo
's main()
method functional (aside implementation of takeinventory()
method).
if desire, may find other classes here.
the objective of takeinventory()
method sort through list
, , report integer value of each unique instance of product
types.
this differentiated exclusively name:
product(name, cost)).
product
s of name should grouped (regardless of cost).
the output should report this:
i assume there method of sorting data more effective current approach. however, not know of one.
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 of equal value."); } else if(list.get(9).compareto(list.get(10)) > 0) { system.out.println("\nthe first saw more expensive."); } else { system.out.println("\nthe second saw 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); } } }
if isn't explicitly clear:
i want remedy takeinventory()
method. objective intent of method sort through given arraylist
of objects, , report sum cost of unique-type values. demonstrated in output. last string literal of output produced conditional in main() method. rest produced takeinventory()
method.
i current takeinventory()
not working.
i build map<string, c>
c
helper class containing quantity (int
) , cost (double
). iterate through product list, , each product:
- if name not in map, associate name
new c(1, cost)
. - if name in map, increase quantity associated name
1
, increase cost associated namecost
.
finally, iterate through map , print result; you're done.
reference: http://docs.oracle.com/javase/7/docs/api/java/util/map.html
here code:
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<>(); (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()); } } (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); } }
output:
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
Comments
Post a Comment