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.
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
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.
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
}
}
Command line
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 ...
-Read database (file) ・ Dictionary type construction ・ Class generation ・ Pattern matching ・ File export ・ XML
Recommended Posts