62,614
社区成员
发帖
与我相关
我的任务
分享
class A
{
private String productname; //产品名称
private String productunit; //产品计量单位
private Int productnum; //产品数量
private String type; //出入库类型 出库/入库
}
List <A> list = new ArrayList<A>();
向list里面添加如下元素
产品名称 产品计量单位 产品数量 出入库类型
A 个 5 出
A 个 2 入
A 个 1 入
B 只 6 入
B 只 2 出
C 个 1 入
C 个 2 入
C 个 3 出
.
.
.
list是按相同产品名称排序的,同一种产品计量单位相同。产品名称 产品计量单位 产品数量
A 个 -2
B 只 4
C 个 0
package org.lxh.demo11.comparabledemo;
import java.util.*;
class Product implements Comparable<Product> //指定类型为Product
{
private String name; //产品名称
private String unit; //产品计量单位
private int num; //产品数量
private String type; //出库/入库
public Product(String name,String unit,int num,String type){
this.name=name;
this.unit=unit;
this.num=num;
this.type=type;
}
public String toString(){
return name+"\t\t"+unit+"\t\t"+num+"\t\t"+type+"\n";
}
public int compareTo(Product pro){//覆写compareTo方法,实现排序规则的应用
if(this.name.compareTo(pro.name)>0){
return 1;
}else if(this.name.compareTo(pro.name)<0){
return -1;
}else{
return 0;
}
}
public void setName(String name){
this.name=name;
}
public void setUnit(String unit){
this.unit=unit;
}
public void setNum(int num){
this.num=num;
}
public void setType(String type){
this.type=type;
}
public String getName(){
return this.name;
}
public String getUnit(){
return this.unit;
}
public int getNum(){
return this.num;
}
public String getType(){
return this.type;
}
}
class ComparableDemo002
{
public static void main(String[] args)
{
Product pro[]={
new Product("A","个",5,"出"),
new Product("A","个",2,"入"),
new Product("A","个",1,"入"),
new Product("B","只",6,"入"),
new Product("B","只",2,"出"),
new Product("C","个",1,"入"),
new Product("C","个",2,"入"),
new Product("C","个",3,"出"),
};
Arrays.sort(pro);
System.out.println(Arrays.toString(pro));
int[] sum=new int[3];
for(int i=0;i<pro.length;i++){
if("A".equals(pro[i].getName())){
if("出".equals(pro[i].getType())){
sum[0]=sum[0]-pro[i].getNum();
}else if("入".equals(pro[i].getType())){
sum[0]=sum[0]+pro[i].getNum();
}
}
if("B".equals(pro[i].getName())){
if("出".equals(pro[i].getType())){
sum[1]=sum[1]-pro[i].getNum();
}else if("入".equals(pro[i].getType())){
sum[1]=sum[1]+pro[i].getNum();
}
}
if("C".equals(pro[i].getName())){
if("出".equals(pro[i].getType())){
sum[2]=sum[2]-pro[i].getNum();
}else if("入".equals(pro[i].getType())){
sum[2]=sum[2]+pro[i].getNum();
}
}
}
System.out.println(Arrays.toString(sum));
}
}
List<String> result = new ArrayList<String>();
A temp = null;
int count = 0,len = list.size();
for(A a : list){
if(null == temp){
temp = a;
}
if(!a.getProductname().equals(temp.getProductname())){
result.add(temp.getProductname()+" "+temp.getProductunit()+" "+count);
temp = a;
count = 0;
}
count += ("入".equals(a.getType())?1:-1)*a.getProductnum();
}
result.add(temp.getProductname()+" "+temp.getProductunit()+" "+count);
System.out.println(result.toString());
for(A a:list){
boolean isContained=false;
if(a.getType().equals("出")){
a.setProductnum(0-a.getProductnum());
}
for(A aa:list2){
if(aa.getProductname().equals(a.getProductname())){
aa.setProductnum(aa.getProductnum()+a.getProductnum());
isContained=true;
}
}
if(!isContained)
list2.add(a);
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author dell
*/
public class NewClass {
List<A> list2 = new ArrayList<A>();
static List<A> test(List<A> list) {
Collections.sort(list);
List<A> list2 = new ArrayList<A>();
A temp = null;
for (A a : list) {
if (temp == null) {
temp = a;
if (temp.getType().equals("出")) {
temp.setProductnum(0 - temp.getProductnum());
}
} else {
if (temp.getProductname().equals(a.getProductname())) {
int t = temp.getProductnum();
if (a.getType().equals("入")) {
t = t + a.getProductnum();
} else {
t = t - a.getProductnum();
}
temp.setProductnum(t);
} else {
list2.add(temp);
temp = a;
if (temp.getType().equals("出")) {
temp.setProductnum(0 - temp.getProductnum());
}
}
}
}
list2.add(temp);
return list2;
}
static List<A> list = new ArrayList<A>();
public static void main(String[] args) {
list.add(new A("A", "个", 5, "出"));
list.add(new A("A", "个", 2, "入"));
list.add(new A("A", "个", 1, "入"));
list.add(new A("B", "只", 6, "入"));
list.add(new A("B", "只", 2, "出"));
list.add(new A("C", "个", 1, "入"));
list.add(new A("C", "个", 2, "入"));
list.add(new A("C", "个", 3, "出"));
List<A> newlist = test(list);
for (A a : newlist) {
System.out.println(a.toString());
}
}
}
class A implements Comparable {
private String productname; //产品名称
private String productunit; //产品计量单位
private Integer productnum; //产品数量
private String type; //出入库类型 出库/入库
public A(String productname, String productunit, Integer productnum, String type) {
this.productname = productname;
this.productunit = productunit;
this.productnum = productnum;
this.type = type;
}
public int compareTo(Object object) {
return this.productname.compareTo(((A) object).productname);
}
public String toString() {
return "productname \t" + productname + "\tproductunit\t " + productunit + "\tproductnum\t " + productnum + "\ttype\t " + type;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public Integer getProductnum() {
return productnum;
}
public void setProductnum(Integer productnum) {
this.productnum = productnum;
}
public String getProductunit() {
return productunit;
}
public void setProductunit(String productunit) {
this.productunit = productunit;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}