[Java] Create something like a product search API

Introduction

First time ** Java **

How can I study ** Java ** (or rather the coding itself) efficiently?

The answer is simple: ** Create the application you want to create! **I think.

I think that the most efficient style is to investigate and implement the required methods as needed.

Purpose

When you enter the keyword of the product you want, like a general EC site A system that returns hit search results (product ** JAN code, title, price, URL to page **) make

Preparation

You need to create a database

This time I tried to make such a database instantly

test.csv


JAN,Title,Price,URL
4543112341136,HG 1/144 (034)Strike Freedom GUNDAM,1344,https://item.rakuten.co.jp/kenbill/4543112341136/
4543112655110,RG 1/144 (002)MS-06S Char's Zaku II,2100,https://item.rakuten.co.jp/kenbill/4543112655110/
4543112752994,MG 1/100 Duel Gundam Assault Shroud GAT-X102,3528,https://item.rakuten.co.jp/kenbill/4543112752994/
4543112815972,MG 1/100 MSN-06S Sinanju,6804,https://item.rakuten.co.jp/kenbill/4543112815972/
4543112728180,MG 1/100 RX-0 Full Armor Unicorn Gundam Ver.ka,6720,https://item.rakuten.co.jp/kenbill/4543112728180/
4573102568328,HGUC 1/144 (108)RGZ-95C Risel(Captain's machine),1848,https://item.rakuten.co.jp/kenbill/4573102568328/

From the left, *** JAN code, product title, price, link to product page ***

For example, if a user searches for "** Gundam ", a product list (JAN code, product title, price, URL) that includes " Gundam **" in the product title will be returned as the search result. It is a product.

Code (completed version)

Last update history: 2019/04/21

Main.java


//coding:utf-8
import java.io.FileWriter;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;//Standard input
import java.io.IOException;

public class Main{
    public static String green  = "\u001b[00;32m";
    public static String pink   = "\u001b[00;35m";
    public static String end    = "\u001b[00m";
    public static HashMap<String, String>  HashTitle = new HashMap<String, String>();
    public static HashMap<String, String>  HashPrice = new HashMap<String, String>();
    public static HashMap<String, String>  HashURL   = new HashMap<String, String>();
    public static void main(String args[]){
        //Read file
        try{
            File file = new File("test.csv");
            FileReader filereader = new FileReader(file);
            BufferedReader bufferreader = new BufferedReader(filereader);

            String line;
            int cnt=0;
            while ((line = bufferreader.readLine()) != null) {
                String[] list = line.split(",");
                String JAN=list[0], Title=list[1], Price=list[2], URL=list[3];
                if (cnt != 0){
                    resister(JAN,Title,Price,URL);
                }
                cnt+=1;
            }     
            filereader.close();
        }catch(FileNotFoundException e){
            System.out.println(e);
        }catch(IOException e){
            System.out.println(e);
        }
        //Standard input
        Scanner scanner = new Scanner(System.in);
        System.out.print("input item keywords> ");
        String input = scanner.nextLine();
        System.out.println(pink+input+end);
        scanner.close();

        //Product Search
        Search sh = new Search();
        Search value = sh.search_item(input, HashTitle, HashPrice, HashURL);
        System.out.println("api.Output xml");
    }

    public static void resister(String JAN,String Title, String Price, String URL){
         HashTitle.put(JAN, Title);
         HashPrice.put(JAN, Price);
         HashURL.put(JAN, URL);
    }
}


//Search class definition
//(search_I want to have multiple return values of item, so I need to define a class)
class Search {
    String keyword;
    String result="";//I just declared"null"Will enter
    String result_JAN;
    String result_Title;
    String result_Price;
    String result_URL;

    public Search search_item(String input, HashMap<String, String> HashTitle, HashMap<String, String> HashPrice, HashMap<String, String> HashURL){
        Search sh  = new Search();
        sh.keyword = input;
        try {
            FileWriter fw = new FileWriter("api.xml");
            fw.write("<API_result>\n");
            for (String key : HashTitle.keySet()){
                if (HashTitle.get(key).contains(sh.keyword)){
                    sh.result_JAN   = key;
                    sh.result_Title = HashTitle.get(key);
                    sh.result_Price = HashPrice.get(key);
                    sh.result_URL   = HashURL.get(key);
                    sh.result       = sh.result + sh.result_JAN+','+sh.result_Title+','+sh.result_Price+','+sh.result_URL+'\n';
                    fw.write("<Item>\n");
                    fw.write("<JAN>"+sh.result_JAN+"</JAN>\n");
                    fw.write("<Title>"+sh.result_Title+"</Title>\n");
                    fw.write("<Price>"+sh.result_Price+"</Price>\n");
                    fw.write("<URL>"+sh.result_URL+"</URL>\n");
                    fw.write("</Item>\n\n");
            }
        }
            fw.write("</API_result>");
            fw.close();
        } catch (IOException ex) {
             ex.printStackTrace();
        }
        //fw.close();
        return sh;//Return by class name
    }
}

Run

Command line スクリーンショット 2019-04-20 21.35.19.png

Output file

api.xml


<API_result>
<Item>
<JAN>4543112655110</JAN>
<Title>RG 1/144 (002)MS-06S Char's Zaku II</Title>
<Price>2100</Price>
<URL>https://item.rakuten.co.jp/kenbill/4543112655110/</URL>
</Item>

<Item>
<JAN>4543112341136</JAN>
<Title>HG 1/144 (034)Strike Freedom GUNDAM</Title>
<Price>1344</Price>
<URL>https://item.rakuten.co.jp/kenbill/4543112341136/</URL>
</Item>

<Item>
<JAN>4573102568328</JAN>
<Title>HGUC 1/144 (108)RGZ-95C Risel(Captain's machine)</Title>
<Price>1848</Price>
<URL>https://item.rakuten.co.jp/kenbill/4573102568328/</URL>
</Item>
</API_result>

Completed by returning the search result (XML)! !!

If you do the same thing with Python, you can do it in seconds ...

Progress memo

-Read database (file) ・ Dictionary type construction ・ Class generation ・ Pattern matching ・ File export ・ XML

Recommended Posts

[Java] Create something like a product search API
Study Java: Use Timer to create something like a bomb timer
Implement something like a stack in Java
[Java] New Yahoo! Product Search API Implementation
[Java] Create a filter
[Java] I tried to implement Yahoo API product search
Create a java method [Memo] [java11]
[Java] Create a temporary file
Create a SlackBot with AWS lambda & API Gateway in Java
Create a Java project using Eclipse
[Java] How to create a folder
Create a named Skip List like redis sorted set in Java
Let's create a TODO application in Java 6 Implementation of search function
Create a high-performance enum with fields and methods like Java with JavaScript
Create a restaurant search app with IBM Watson + Gurunavi API (with source)
[Java] Create and apply a slide master
Make something like Java Enum with Typescript
To create a Zip file while grouping database search results in Java
Let's create a Java development environment (updating)
Create a TODO app in Java 7 Create Header
Create a RESTful API service using Grape
[Rails] Let's create a super simple Rails API
Create API using Retrofit2, Okhttp3 and Gson (Java)
Create a simple search app with Spring Boot
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
Let's create a REST API using WildFly Swarm.
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
Create a Lambda Container Image based on Java 15
Try to create a bulletin board in Java
[Java] Create a collection with only one element
Let's create a super-simple web framework in Java
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
Create Scala Seq from Java, make Scala Seq a Java List
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Java] Get images with Google Custom Search API
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
Create a web api server with spring boot
Java Stream API
Something about java
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
Create a portfolio app using Java and Spring Boot
Do something like a JS immediate function in Ruby
Create a Java development environment using jenv on Mac
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
Create a docker image that runs a simple Java app
How to create a Java environment in just 3 seconds
I want to do something like "cls" in Java
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[SwiftUI] Create a TextField that looks like a Qiita tag
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Beginner] Create a competitive game with basic Java knowledge
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
Java / Twitter clone / task management system (1) Create a database
I tried to create a Clova skill in Java
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]