下面有两个列表(不是数据库的表):
列表A(订单表)
物料号 物品数
A10001 1000
A10002 500
A10003 200
A10003 100
列表B(库存表)
物料号 物品数
A10001 700
A10002 800
A10003 500
A10004 100
. .
. .
写一个函数,输入参数是列表A和列表B,输出参数是表B中的物品数是否大于表A中的物品数,如果满足返回“满意/true”,如果不满足给予提示/false。注意:表A中的“A10003”号中的物品数做处理(就是要把表A中的“A10003”号中的物品数先进行加和形成新的列表,然后再与表B中的内容作比较)。
表中的内容用ArrayList存储。
列表A(订单表)
物料号 物品数
A10001 1000
A10002 500
A10003 200
A10003 100
列表B(库存表)
物料号 物品数
A10001 700
A10002 800
A10003 500
A10004 100
. .
. .
写一个函数,输入参数是列表A和列表B,输出参数是表B中的物品数是否大于表A中的物品数,如果满足返回“满意/true”,如果不满足给予提示/false。注意:表A中的“A10003”号中的物品数做处理(就是要把表A中的“A10003”号中的物品数先进行加和形成新的列表,然后再与表B中的内容作比较)。
表中的内容用ArrayList存储。
LZ,不是很难吧?
============
怎么看着感觉有歧义?这个大于是怎么个大法才算大于?
/**
* 订单匹配方法
* @param arlA是订单,按照一个商品编号一个商品数量来排列
* @param arlB是存货,也按照一个商品编号一个商品数量来排列
* @return 是否存货充足,并提示不足的第一个商品的编号
* 因时间问题未作输入界限处理,假定传入数据正常范围
*/
public static boolean isEnough(ArrayList arlA,ArrayList arlB)
{
for(int i=0;i<arlA.size();i=i+2){
//处理订单中重复的商品
for(int j=i+2;j<arlA.size()-1;j=j+2)
if(arlA.get(i)==arlA.get(j)){
arlA.add(i+1, (Integer)arlA.get(i+1)+(Integer)arlA.get(j+1));
arlA.remove(j);
arlA.remove(j+1);
}
}
//遍历对比
for(int i=1;i<arlA.size();i=i+2){
if((Integer) arlA.get(i)>=(Integer) arlB.get(i)){
System.out.println("商品号:"+arlA.get(i-1)+"数量不足!");
return false;
}
}
return true;
}
public static void main(String[] args) {
//测试
ArrayList arlA=new ArrayList();
arlA.add("A10001");
arlA.add(1000);
arlA.add("A10002");
arlA.add(500);
arlA.add("A10003");
arlA.add(200);
arlA.add("A10003");
arlA.add(100);
ArrayList arlB=new ArrayList();
arlB.add("A10001");
arlB.add(700);
arlB.add("A10002");
arlB.add(800);
arlB.add("A10003");
arlB.add(500);
arlB.add("A10004");
arlB.add(100);
System.out.println(isEnough(arlA,arlB));
}}
比较的问题的话就不多说了。
我感觉用map做好一点。
个人愚见,望指点
import java.util.*;
public class HelloWorld {
class Item {
String itemID;
int itemCount;
Item(String id, int count){
itemID = id;
itemCount = count;
}
}
static boolean isEnough(ArrayList<Item> order, ArrayList<Item> inventory) {
int orderIndex = 0;
int inventoryIndex = 0;
while (orderIndex < order.size()){
String id = order.get(orderIndex).itemID;
int count = order.get(orderIndex).itemCount;
while (++orderIndex < order.size() && order.get(orderIndex).itemID == id){
count += order.get(orderIndex).itemCount;
}
while(inventory.get(inventoryIndex).itemID != id){
inventoryIndex++;
}
if (count > inventory.get(inventoryIndex).itemCount){
return false;
}
}
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorld hello = new HelloWorld();
ArrayList<Item> orderList = new ArrayList<Item>();
orderList.add(hello.new Item("A10001", 1000));
orderList.add(hello.new Item("A10002", 500));
orderList.add(hello.new Item("A10003", 200));
orderList.add(hello.new Item("A10003", 1000));
ArrayList<Item> inventoryList = new ArrayList<Item>();
inventoryList.add(hello.new Item("A10001", 10000));
inventoryList.add(hello.new Item("A10002", 500));
inventoryList.add(hello.new Item("A10003", 1300));
inventoryList.add(hello.new Item("A10004", 10000));
if (isEnough(orderList, inventoryList))
{
System.out.println("Enough");
}
else {
System.out.println("Not Enough");
}
return;
}}
package test;public class Goods
{
private String gid;
private int count;
public Goods()
{
super();
} public Goods(String gid, int count)
{
super();
this.gid = gid;
this.count = count;
} public String getGid()
{
return gid;
}
public void setGid(String gid)
{
this.gid = gid;
}
public int getCount()
{
return count;
}
public void setCount(int count)
{
this.count = count;
} @Override
public boolean equals(Object obj)
{
Goods goods = (Goods)obj;
// TODO Auto-generated method stub
return this.gid.equals(goods.getGid());
} @Override
public int hashCode()
{
// TODO Auto-generated method stub
return super.hashCode();
}
}package test;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class Test
{ /**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
ArrayList<Goods> orderList = new ArrayList<Goods>();
orderList.add(new Goods("A10001", 1000));
orderList.add(new Goods("A10002", 500));
orderList.add(new Goods("A10003", 400));
orderList.add(new Goods("A10003", 1000));
orderList.add(new Goods("A10002", 1000));
ArrayList<Goods> inventoryList = new ArrayList<Goods>();
inventoryList.add(new Goods("A10001", 10000));
inventoryList.add(new Goods("A10002", 500));
inventoryList.add(new Goods("A10003", 1300));
inventoryList.add(new Goods("A10004", 10000));
ArrayList<Goods> aList = getNewList(orderList);
Map<String, Goods> map = getMap(inventoryList);
for(Goods g : aList)
{
if(g.getCount() <= map.get(g.getGid()).getCount())
{
System.out.println(g.getGid() + "满意");
}
else
{
System.out.println(g.getGid() + "不满意");
}
}
}
public static Map<String, Goods> getMap(List<Goods> inventoryList){
Map<String, Goods> map = new HashMap<String, Goods>();
for(Goods g : inventoryList)
{
map.put(g.getGid(), g);
}
return map;
}
public static ArrayList<Goods> getNewList(List<Goods> list)
{
ArrayList<String> sList = new ArrayList<String>();
ArrayList<Goods> gList = new ArrayList<Goods>();
Map<String, Goods> map = new HashMap<String, Goods>();
for(Goods goods : list)
{
String gid = goods.getGid();
int count = goods.getCount();
Goods g = new Goods(gid, count);
if(!sList.contains(gid))
{
gList.add(g);
map.put(gid, g);
sList.add(gid);
}
else
{
gList.remove(g);
g.setCount(map.get(gid).getCount() + count);
gList.add(g);
}
}
return gList;
}}
public class Goods implements Comparable{
private String id;
private int num;
//库存是否充足
private boolean isEnough;
public Goods(){}
public Goods(String id,int num) {
this.id = id;
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean isEnough() {
return isEnough;
}
public void setEnough(boolean isEnough) {
this.isEnough = isEnough;
}
public int compareTo(Object o) {
Goods goods = (Goods) o;
return this.id.compareTo(goods.id);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + num;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Goods other = (Goods) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (num != other.num)
return false;
return true;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class EnoughTest { /**
* @param args
*/
public static void main(String[] args) {
List<Goods> order = new ArrayList<Goods>();
List<Goods> storage = new ArrayList<Goods>();
order.add(new Goods("A10001",1000));
order.add(new Goods("A10002",500));
order.add(new Goods("A10003",200));
order.add(new Goods("A10003",100));
storage.add(new Goods("A10001",700));
storage.add(new Goods("A10002",800));
storage.add(new Goods("A10003",500));
storage.add(new Goods("A10004",100));
Collections.sort(order);
Goods tempGoods = new Goods("0",0);
for(int i = 0;i<order.size();i++) {
Goods goods = order.get(i);
if(tempGoods.getId().equals(goods.getId())) {
order.get(i-1).setNum(order.get(i-1).getNum()+order.get(i).getNum());
order.remove(i);
i--;
}
tempGoods = order.get(i);
}
Collections.sort(storage);
//比较库存是否充足
int k = 0;
for(int i=0;i<storage.size();i++) {
//默认库存充足 如果出现不足 再设为不足
storage.get(i).setEnough(true);
for(int j=k;j<order.size();j++) {
if(storage.get(i).compareTo(order.get(j)) == 0) {
if(storage.get(i).getNum()<order.get(j).getNum()) {
storage.get(i).setEnough(false);
}
break;
}
}
}
//打印库存是否充足
for(Goods goodsIsEnough : storage) {
System.out.println(goodsIsEnough.getId() + ":" + goodsIsEnough.isEnough());
}
}
}