LoginSignup
5
3

More than 1 year has passed since last update.

【Java】商品検索API的なものを作る

Last updated at Posted at 2019-04-20

はじめに

初めてのJava

Java(というよりはコーディングそのもの)を効率よく勉強するにはどうしたらいいのか?

答えは簡単で,自分で作りたいと思ったアプリケーションを作ること!だと思います.

そこで必要になるメソッドを随時調べたりして実装していくのが一番効率の良いスタイルだと思います.

目的

一般的なECサイトのように,ユーザが欲しい商品のキーワードを入力すると
ヒットした検索結果(商品のJANコード,タイトル,価格,ページへのURL)を返してくれるシステム
を作る

準備

データベースを作っておく必要があります

今回はこのようなデータベースを即席で作ってみました

test.csv
JAN,Title,Price,URL
4543112341136,HG 1/144 (034)ストライクフリーダムガンダム,1344,https://item.rakuten.co.jp/kenbill/4543112341136/
4543112655110,RG 1/144 (002)MS-06S シャア専用 ザクII ,2100,https://item.rakuten.co.jp/kenbill/4543112655110/
4543112752994,MG 1/100 デュエルガンダムアサルトシュラウド GAT-X102,3528,https://item.rakuten.co.jp/kenbill/4543112752994/
4543112815972,MG 1/100 MSN-06S シナンジュ,6804,https://item.rakuten.co.jp/kenbill/4543112815972/
4543112728180,MG 1/100 RX-0 フルアーマーユニコーンガンダム Ver.ka,6720,https://item.rakuten.co.jp/kenbill/4543112728180/
4573102568328,HGUC 1/144 (108)RGZ-95C リゼル (隊長機),1848,https://item.rakuten.co.jp/kenbill/4573102568328/

左から,JANコード,商品タイトル,価格,商品ページへのリンクです

例えば,ユーザが”ガンダム”で検索したら商品タイトルに"ガンダム"が含まれる商品リスト(JANコード,商品タイトル,価格,URL)が検索結果として帰って来るというシンプルなつくりです.

コード(完成版)

最終更新履歴: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;//標準入力
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[]){
        //ファイルの読み込み
        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);
        }
        //標準入力
        Scanner scanner = new Scanner(System.in);
        System.out.print("input item keywords> ");
        String input = scanner.nextLine();
        System.out.println(pink+input+end);
        scanner.close();

        //商品検索
        Search sh = new Search();
        Search value = sh.search_item(input, HashTitle, HashPrice, HashURL);
        System.out.println("api.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);
    }
}


//Serch classの定義
//(search_itemの戻り値を複数にしたいのでクラスを定義する必要がある)
class Search {
    String keyword;
    String result="";//ただ宣言しただけだと"null"が入っちゃう
    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;//クラス名でリターン
    }
}

実行

コマンドライン
スクリーンショット 2019-04-20 21.35.19.png

出力ファイル

api.xml
<API_result>
<Item>
<JAN>4543112655110</JAN>
<Title>RG 1/144 (002)MS-06S シャア専用 ザクII </Title>
<Price>2100</Price>
<URL>https://item.rakuten.co.jp/kenbill/4543112655110/</URL>
</Item>

<Item>
<JAN>4543112341136</JAN>
<Title>HG 1/144 (034)ストライクフリーダムガンダム</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 リゼル (隊長機)</Title>
<Price>1848</Price>
<URL>https://item.rakuten.co.jp/kenbill/4573102568328/</URL>
</Item>
</API_result>

検索結果(XML)を返すとこまで完了!!

Pythonで同じことさせたら秒でできるレベル・・・

経過メモ

・データベース(ファイル)読み込み
・辞書型構築
・クラス生成
・パターンマッチング
・ファイル書き出し
・XML

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3