package Week03.day0725;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
public class practice0725 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Product p1 = new Product(3424, "갤럭시S10");
Product p2 = new Product(3331, "아이폰XR");
Product p3 = new Product(5552, "아이폰X");
Product p4 = new Product(1111, "아이폰S7");
HashMap products = new HashMap();
// products.put(004, "갤럭시S10");
// products.put(001, "아이폰XR");
// products.put(002, "아이폰X");
// products.put(003, "아이폰S7");
Order or = new Order(123, f.parse("2019-07-01"), products);
or.addProduct(1, p1);
or.addProduct(2, p2);
or.addProduct(3, p3);
or.addProduct(4, p4);
or.delProduct(123);
Product pr = new Product(9997, "아이패드");
or.chgProduct(1, pr);
or.printKeyBySort(products);
Compare1 c = new Compare1();
or.printValueBySort(c);
//comparable로 구현
// System.out.println("====printValueBySort(by comparable)====");
// ArrayList<Product> list = new ArrayList<Product>();
// list.addAll(products.values());
// Collections.sort(list);
// for(Product p : list) {
// System.out.println(p.toString());
// }
}
}
class Order{
private int orderNo;
private Date orderDt;
private HashMap products = new HashMap(); // 주문상품
public int getOrderNo() {
return orderNo;
}
public void setOrderNo(int orderNo) {
this.orderNo = orderNo;
}
public Order(int orderNo, Date orderDt, HashMap products) {
this.orderNo = orderNo;
this.orderDt = orderDt;
this.products = products;
}
public void addProduct(int ordProdNo, Product p) { // 주문상품을 추가한다
this.orderNo = orderNo;
this.products.put(ordProdNo, p);
}
public void delProduct(int ordProdNo) { // 주문상품을 삭제한다
products.remove(ordProdNo);
}
public void chgProduct(int orProdNo, Product p) { // 주문상품을 변경한다
products.replace(orProdNo, p);
}
public void printKeyBySort(HashMap products) { // 주문상품을 키를 기준으로 정렬하여 출력한다
System.out.println("====printKeyBySort====");
HashMap<Integer, Product> hs = new HashMap<Integer, Product>(products);
Iterator<Integer> it = products.keySet().iterator(); //키값 오름차순 정렬(기본)
while(it.hasNext()) {
Integer key = it.next();
System.out.println("key 값 : " + (key) +", 제품명 : " + hs.get(key).getProductNm() + ", 제품번호 : " + hs.get(key).getProductNo());
}
}
public void printValueBySort(Comparator <Product> c) {
//주문상품을 Value를 기준으로 정렬하여 출력한다
//Value는 제품번호를 기준으로 정렬한다
System.out.println("====printValueBySort====");
ArrayList<Product> list = new ArrayList<Product>();
list.addAll(this.products.values()); // 값을 리턴. comparable로 (내부)
Collections.sort(list, c);
for(Product p : list) {
System.out.println(p.toString());
}
}
}
class Compare1 implements Comparator<Product> {
@Override
public int compare(Product o1, Product o2) {
if(o1.getProductNo() > o2.getProductNo()){
return 1;
} else if (o1.getProductNo() < o2.getProductNo()) {
return -1;
} else {
return 0;
}
}
}
// Comparator - 편법 키값을 정렬해서 다시 value를 정렬하는 방법.
// list.addAll(products.keySet()); // 키값을 리턴. comparator로 (외부)
// Collections.sort(list, new Comparator() {
// @Override
// public int compare(Object o1,Object o2) {
// Object v1 = products.get(o1);
// Object v2 = products.get(o2);
// return ((Comparable<Object>) v2).compareTo(v1);
// }
// });
//
// Collections.reverse(list); // 주석시 오름차순
// return;
class Product{
private int productNo; // 제품번호
private String productNm; // 제품이름
public Product(int productNo, String productNm) {
this.productNo = productNo;
this.productNm = productNm;
}
public int getProductNo() {
return productNo;
}
public void setProductNo(int productNo) {
this.productNo = productNo;
}
public String getProductNm() {
return productNm;
}
public void setProductNm(String productNm) {
this.productNm = productNm;
}
public int hashCode() {
return Objects.hash(this.productNm, this.productNo);
}
public boolean equals(Object obj) {
if(obj instanceof Product) {
Product tmp = (Product)obj;
return this.productNo== tmp.productNo && this.productNm.equals(tmp.productNm);
}
return false;
}
public String toString() {
return "제품번호 : " + this.getProductNo() + "/" + "제품이름 : " + this.getProductNm();
}
//comparable로 구현 (Product클래스에 implements Comparable 붙어야함)
// public int compareTo(Object s) {
// if(this.productNo < ((Product)s).getProductNo()) {
// return -1;
// } else if (this.productNo > ((Product)s).getProductNo()) {
// return 1;
// }
// return 0;
// }
}