要求:写一个商品信息录入代码,包括编号、商品名称、进货日期、价格
格式:1,牛肉,2013-3-17,35
拆分字符串,自定义key存入HashMap<String,String>,每个商品对应一个map
输入一个商品完成后,提示是否继续输入,n结束,y继续
多个商品信息放入ArrayList<HashMap<String,String>>中
所有商品信息录入完毕,从ArrayList取出所有商品信息,每个商品转换成一个Product对象,再放入一个新的ArrayList<Product>中
按id判断是否包含指定商品。
代码:
货物类Goods:
import java.util.Date; public class Goods { private int Num; private String name; private Date date; private double price; public Goods() { super(); // TODO Auto-generated constructor stub } public Goods(int num, String name, Date date, double price) { super(); Num = num; this.name = name; this.date = date; this.price = price; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Num; result = prime * result + ((date == null) ? 0 : date.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); 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 (Num != other.Num) return false; if (date == null) { if (other.date != null) return false; } else if (!date.equals(other.date)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(price) != Double .doubleToLongBits(other.price)) return false; return true; } public int getNum() { return Num; } public void setNum(int num) { Num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Goods [Num=" + Num + ", name=" + name + ", date=" + date + ", price=" + price + "]"; } }
商品测试类TestGoods:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class TestGoods { public static void main(String[] args) { Date gdate=null; String strNum; String strName; String strDate; String strPrice; Scanner scan = new Scanner(System.in); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); //将录入的商品信息存入hashmap中,并将每个商品的map存入ArrayList中。 for (;;) { HashMap<String, String> map = new HashMap<String, String>(); System.out.print("请输入商品编号:"); strNum = scan.next(); System.out.print("请输入商品名称:"); strName = scan.next(); System.out.print("请输入商品的进货日期:"); strDate = scan.next(); System.out.print("请输入商品价格:"); strPrice = scan.next(); map.put("Gnum", strNum); map.put("Gname", strName); map.put("Gdate", strDate); map.put("Gprice", strPrice); list.add(map); System.err.println("录入完毕,是否继续录入(n或y)?"); String flag = scan.next(); if ("n".equals(flag)) { System.out.println("录入结束!"); break; } else if ("y".equals(flag)) { continue; } } //从ArrayList中取map值,并用每个map中存的信息生成货物对象 ArrayList<Goods> glist = new ArrayList<Goods>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); for (HashMap<String, String> hashMap : list) { //System.out.println(hashMap.get("Gdate")); int gnum = Integer.parseInt(hashMap.get("Gnum")); String gname = hashMap.get("Gname"); try { gdate = dateFormat.parse(hashMap.get("Gdate")); gdate = java.sql.Date.valueOf(hashMap.get("Gdate")); } catch (ParseException e) { e.printStackTrace(); } double gprice = Double.parseDouble(hashMap.get("Gprice")); glist.add(new Goods(gnum, gname, gdate, gprice)); } //列出所有商品 for (Goods goods : glist) { System.out.println(goods); } while (true) { //按商品号查询商品。存在返回tru,否则返回false System.out.println("请输入商品号:"); int qnum = Integer.parseInt(scan.next()); Iterator<Goods> git = glist.iterator(); while (git.hasNext()) { Goods goods = (Goods) git.next(); if (goods.getNum() == qnum) { System.out.println(true); break; } else { if (!git.hasNext()) { System.out.println("false"); } } } } } }
运行结果:
请输入商品编号:01 请输入商品名称:面包 请输入商品的进货日期:2012-07-25 请输入商品价格:10.0 录入完毕,是否继续录入(n或y)? y 请输入商品编号:02 请输入商品名称:汽水 请输入商品的进货日期:2013-07-01 请输入商品价格:2.00 录入完毕,是否继续录入(n或y)? n 录入结束! Goods [Num=1, name=面包, date=2012-07-25, price=10.0] Goods [Num=2, name=汽水, date=2013-07-01, price=2.0] 请输入商品号: 1 true 请输入商品号: 3 false 请输入商品号: |