-
-
Save iamhosseindhv/1eb775b4a2bcda31fd3c834d0eb9208c to your computer and use it in GitHub Desktop.
FakeDataProvider takes a .txt file (containing coordinates of an area) as input in addition to an integer (number of fake data to be produced for that coordinate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.ArrayList; | |
import java.text.DecimalFormat; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.util.Objects; | |
import java.util.Random; | |
import java.util.InputMismatchException; | |
import java.io.*; | |
/* | |
example input file - coordinates.txt: | |
((29.4958671, 52.38693239999998), (29.8352834, 52.67900420000001)) | |
*/ | |
/* | |
USAGE: java FakeDataProvide coordinates.txt 150 | |
*/ | |
public class FakeDataProvider { | |
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { | |
PrintWriter writer = new PrintWriter("mysql-data.csv", "UTF-8"); | |
double[] coordinates = readFile(args[0]); | |
int noOfRecords = Integer.parseInt(args[1]); | |
double ne_lat = coordinates[0]; | |
double ne_lng = coordinates[1]; | |
double sw_lat = coordinates[2]; | |
double sw_lng = coordinates[3]; | |
System.out.println("writing to file..."); | |
for (int count=0 ; count < noOfRecords ; count++){ | |
//random latitute and longtitute | |
double randomLng = getRandomCoordinate(sw_lng, ne_lng); | |
double randomLat = getRandomCoordinate(sw_lat, ne_lat); | |
//id | |
int id = getRandomId(randomLat, randomLng); | |
String location = ""; | |
int price = (int) getRandom(60, 700); | |
String thumbnail_img = ""; | |
String is_favourite = getRandomIsFavourite(); | |
int review_count = (int) getRandom(5, 110); | |
String type = getRandomType(); | |
String title = getRandomTitle(type); | |
int bedroom_count = (int) getRandom(1, 9); | |
int bed_count = (int) getRandom(bedroom_count, 10); | |
int bathroom_count = (int) getRandom(1, 5); | |
int max_guest = (int) getRandom(1, 15); | |
String user_id = ""; | |
StringBuilder line = new StringBuilder(); | |
line.append(id + ", " + | |
location + ", " + | |
price + ", " + | |
thumbnail_img + ", " + | |
is_favourite + ", " + | |
review_count + ", " + | |
title + ", " + | |
type + ", " + | |
bed_count + ", " + | |
bedroom_count + ", " + | |
bathroom_count + ", " + | |
max_guest + ", " + | |
randomLng + ", " + | |
randomLat + ", " + | |
user_id); | |
writer.println(line.toString()); | |
} | |
writer.close(); | |
System.out.println("completed"); | |
} | |
public static double getRandomCoordinate(double min, double max){ | |
DecimalFormat df = new DecimalFormat("#.#######"); | |
double randNo = getRandom(min,max); | |
return Double.valueOf(df.format(randNo)); | |
} | |
public static double getRandom(double min, double max) { | |
Random r = new Random(); | |
double randomValue = min + (max - min) * r.nextDouble(); | |
return randomValue; | |
} | |
public static String getRandomIsFavourite() { | |
int min = 1; | |
int max = 10; | |
int randNo = (int) getRandom(min, max); | |
if (randNo % 2 == 0) { | |
return "true"; | |
} else { | |
return "false"; | |
} | |
} | |
public static String getRandomType() { | |
ArrayList<String> types = new ArrayList<>(); | |
types.add("Shared Room"); | |
types.add("Private Room"); | |
types.add("Entire House"); | |
int min = 0; | |
int max = 2; | |
int randNo = (int) getRandom(min, max); | |
if (randNo == 0) { | |
return types.get(0); | |
} else if (randNo == 1) { | |
return types.get(1); | |
} else { | |
return types.get(2); | |
} | |
} | |
public static int getRandomId(double lat, double lng){ | |
long timestamp = System.currentTimeMillis(); | |
int id = Objects.hash(lat, lng, timestamp); | |
if (id < 0) { | |
id *= -1; | |
} | |
return id; | |
} | |
public static String getRandomTitle(String myType) { | |
String type = ""; | |
if (myType == "Shared Room" || myType == "Private Room"){ | |
type = "room"; | |
} else { | |
type = "house"; | |
} | |
ArrayList<String> titles = new ArrayList<>(); | |
titles.add("Beautiful"); | |
titles.add("Nice"); | |
titles.add("Cozy"); | |
titles.add("Luxury"); | |
titles.add("Lovely"); | |
ArrayList<String> complemetary = new ArrayList<>(); | |
complemetary.add("in the heart of city"); | |
complemetary.add("near beach"); | |
complemetary.add("..."); | |
int min = 0; | |
int maxTitles = titles.size() - 1; | |
int maxComplemetary = complemetary.size() - 1; | |
int randNo = (int) getRandom(min, maxTitles); | |
int randNo2 = (int) getRandom(min, maxComplemetary); | |
String finalTitle = ""; | |
for (int i=0 ; i < titles.size() ; i++){ | |
if (randNo == i){ | |
finalTitle = titles.get(i) + " " + type; | |
} | |
} | |
for (int i=0 ; i < complemetary.size() ; i++){ | |
if (randNo2 == i){ | |
finalTitle = finalTitle + " " + complemetary.get(i); | |
} | |
} | |
return finalTitle; | |
} | |
private static final Pattern PATTERN = Pattern.compile( | |
"^\\s*\\u0028+" | |
+ "(-?\\d+\\.\\d+)\\u002c+\\s+" | |
+ "(-?\\d+\\.\\d+)\\u0029+\\u002c+" | |
+ "\\s*\\u0028+" | |
+ "(-?\\d+\\.\\d+)\\u002c+\\s+" | |
+ "(-?\\d+\\.\\d+)\\u0029+$" | |
); | |
public static double[] readFile(String fileName) throws FileNotFoundException { | |
Scanner input = new Scanner(new File(fileName)); | |
String firstLine = input.nextLine(); | |
Matcher matcher = PATTERN.matcher(firstLine); | |
if (matcher.matches()) { | |
double sw_lat = Double.parseDouble(matcher.group(1)); | |
double sw_lng = Double.parseDouble(matcher.group(2)); | |
double ne_lat = Double.parseDouble(matcher.group(3)); | |
double ne_lng = Double.parseDouble(matcher.group(4)); | |
double[] coordinates = {ne_lat, ne_lng, sw_lat, sw_lng}; | |
return coordinates; | |
} | |
else { | |
System.out.println(firstLine); | |
throw new InputMismatchException("ERROR: invalid coordinates format"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment