活跃农民
- 积分
- 494
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-3-20
- 最后登录
- 1970-1-1
|
- package stripeOA;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.*;
- public class Solution {
-
- public static void main(String[] args) {
- List<String> test1 = new ArrayList<>();
- test1.add("CREATE: id=13&amount=800¤cy=USD");
- System.out.println("test1: " + compute(test1)); //800
- List<String> test2 = new ArrayList<>();
- test2.add("CREATE: id=16&amount=800¤cy=USD");
- test2.add("FINALIZE: id=16&amount=600¤cy=USD");
- System.out.println("test2: " + compute(test2));//600
-
- List<String> test3 = new ArrayList<>();
- test3.add("CREATE: id=16&amount=800¤cy=USD");
- test3.add("FINALIZE: id=16&amount=600¤cy=USD");
- test3.add("CREATE: id=13&amount=800¤cy=USD");
- System.out.println("test3: " + compute(test3));//1400
-
- List<String> test4 = new ArrayList<>();
- test4.add("CREATE: id=16&amount=800¤cy=USD");
- test4.add("FINALIZE: id=16&amount=600¤cy=USD");
- test4.add("PAY: id=16");
- System.out.println("test4: " + compute(test4));//0
-
- List<String> test5 = new ArrayList<>();
- test5.add("CREATE: amount=800&id=16¤cy=USD");
- test5.add("FINALIZE: id=16&amount=600¤cy=USD&otherfield=a");
- test5.add("PAY: otherfield=a&id=16");
- System.out.println("test5: " + compute(test5));//0
-
- List<String> test6 = new ArrayList<>();
- test6.add("CREATE: amount=800&id=16¤cy=USD");
- test6.add("FINALIZE: id=16&amount=600¤cy=US");
- System.out.println("test6: " + compute(test6));//800
- test6.add("PAY: id=16");
- System.out.println("test6: "+compute(test6)); // 800
-
-
- List<String> test7 = new ArrayList<>();
- test7.add("Random action");
- System.out.println("test7: " + compute(test7));//0
-
- List<String> test8 = new ArrayList<>();
- test8.add("CREATE: id=16&amount=800¤cy=USD");
- test8.add("FINALIZE: id=16&amount=600¤cy=USD");
- test8.add("PAY: id=16");
- test8.add("CREATE: id=16&amount=800¤cy=USD");
- System.out.println("test8: " + compute(test8));//0
-
- List<String> test9 = new ArrayList<>();
- // test9.add("FINALIZE: id=16&amount=800¤cy=USD");
- test9.add("CREATE: id=14&amount=500¤cy=RMB");
- test9.add("CREATE: id=15&amount=500¤cy=USD");
- test9.add("FINALIZE: id=14&amount=600¤cy=USD");
- test9.add("FINALIZE: id=15&amount=400¤cy=USD");
- // test9.add("PAY: id=16");
- System.out.println("test9: " + compute(test9));//0
-
-
- }
-
-
- public static int compute(List<String> input) {
-
- Map<String, Invoice> map = new HashMap<>();
- int re = 0;
-
- for(String s : input) {
- String[] arr = s.split(":");
- String state = arr[0].trim();
- if(state.equals("CREATE")) {
- Invoice in = getInvoice(arr[1]);
- if(in !=null) {
- if(!map.containsKey(in.id)) {
- map.put(in.id,in);
- }
- }
-
- }else if(state.equals("FINALIZE")) {
- Invoice in = getInvoice(arr[1]);
- if(in != null) {
- in.state = "FINALIZE";
- if(map.containsKey(in.id)) {
- Invoice temp = map.get(in.id);
- if(temp.state.equals("CREATE")) {
- map.put(in.id, in);
- }
- }
- }
-
-
- }else if(state.equals("PAY")) {
- String id = payInvoice(arr[1]);
- if(map.containsKey(id)) {
- Invoice in = map.get(id);
- if(in.state.equals("FINALIZE")){
- in.amount = "0";
- in.state = "PAY";
- map.put(id, in);
- }
- }
- }
-
- }
- for(String id: map.keySet()) {
- Invoice in = map.get(id);
- int amount = Integer.valueOf(in.amount);
- re+=amount;
- }
-
-
-
- return re;
-
- }
-
- public static Invoice getInvoice(String s) {
- String[] infos = s.split("&");
- String id = null;
- String amount = null;
- String currency =null;
- Invoice invoice = null;
- for(String info : infos) {
- char[] ch = info.toCharArray();
- StringBuilder sb = new StringBuilder();
- if(info.contains("id=")) {
- int index = info.indexOf("id=");
- id = handleInfo(index+3,info);
- }else if(info.contains("amount=")) {
- int index = info.indexOf("amount=");
- amount = handleInfo(index+7,info);
- }else if(info.contains("currency=")) {
- int index = info.indexOf("currency=");
- currency = info.substring(index+9, info.length());
- }
- }
- if(id !=null && amount !=null && currency.equals("USD")) {
- invoice = new Invoice(id,amount,currency, "CREATE");
- }
-
- return invoice;
- }
-
- public static String payInvoice(String s) {
-
- int index = s.indexOf("id=");
- StringBuilder sb = new StringBuilder();
- char[] ch = s.toCharArray();
- for(int i = index+3;i<ch.length;i++) {
- if(Character.isDigit(ch[i]))sb.append(ch[i]);
- else break;
- }
- return sb.toString();
-
- }
-
- public static String handleInfo(int index, String s) {
- char[] ch = s.toCharArray();
- StringBuilder sb = new StringBuilder();
- for(int i = index; i<ch.length;i++) {
- if(Character.isDigit(ch[i]))sb.append(ch[i]);
- else break;
- }
-
- return sb.toString();
- }
-
-
-
-
- }
- class Invoice {
- String id;
- String amount;
- String currency;
- String state;
- Invoice(String id, String amount, String currency, String state){
- this.id = id;
- this.amount = amount;
- this.currency = currency;
- this.state = state;
- }
- }
复制代码 |
|