Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.lang.reflect.Type;
  3. import java.util.*;
  4. import com.google.gson.Gson;
  5. import com.google.gson.reflect.TypeToken;
  6. import com.google.gson.stream.JsonReader;
  7. public class Main{
  8.  
  9. public static void main(String[] args) throws Exception{
  10.  
  11. Type type = new TypeToken<List<Snackers>>(){}.getType();
  12.  
  13. //Reading directly from local file due to proxy issues
  14. JsonReader reader = new JsonReader(new FileReader("C:\\Users\\193010493\\IdeaProjects\\DeskNibbleChallenge\\src\\com\\company\\SnackerList.json"));
  15. Gson gson = new Gson();
  16.  
  17. //List of all snackers
  18. List<Snackers> dataSnacker = gson.fromJson(reader, type);
  19.  
  20. //Reading directly from local file due to proxy issues
  21. reader = new JsonReader(new FileReader("C:\\Users\\193010493\\IdeaProjects\\DeskNibbleChallenge\\src\\com\\company\\SnacksList.json"));
  22.  
  23. //Products object containing ArrayList of Snacks
  24. Products dataSnacks = gson.fromJson(reader, Products.class);
  25.  
  26. //ArrayList to be populated with any snackers that match vendor list
  27. List<Snackers> faveSnackers = new ArrayList<>();
  28.  
  29. //Iterate through to match Snacker's fave snack to any found in vendor list
  30. //Runs in O(n^2), needs to be more efficient for larger datasets
  31. for(int i=0; i < dataSnacker.size(); i++){
  32. for (int j=0; j < dataSnacks.products.size(); j++){
  33. if ((dataSnacker.get(i).fave_snack).equals(dataSnacks.products.get(j).title)){
  34. faveSnackers.add(dataSnacker.get(i));
  35. }
  36. }
  37. }
  38.  
  39.  
  40. //Iterate through our populated faveSnackers to display their fave snack, email, and cost of snack
  41. for(int i =0; i<faveSnackers.size();i++){
  42. System.out.println(faveSnackers.get(i).first_name +" "+ faveSnackers.get(i).last_name + " listed " + faveSnackers.get(i).fave_snack + " as their fave snack!");
  43. System.out.println("Their email is " + faveSnackers.get(i).email);
  44. System.out.println("If they purchase through Desk Nibbles, they would pay $" + getPrice(faveSnackers.get(i).fave_snack, dataSnacks)+"0");
  45. System.out.println();
  46. }
  47. }
  48.  
  49. private static double getPrice(String snackName, Products dataSnacks){
  50. for (int i =0; i<dataSnacks.products.size(); i++){
  51. if (snackName.equals(dataSnacks.products.get(i).title)){
  52. return dataSnacks.products.get(i).variants.get(0).price;
  53. }
  54. }
  55.  
  56. return (0);
  57. }
  58.  
  59.  
  60. }
  61.  
  62.  
  63. class Snackers {
  64.  
  65. public long id;
  66. public String first_name;
  67. public String last_name;
  68. public String email;
  69. public String gender;
  70. public String ip_address;
  71. public String fave_snack;
  72.  
  73. }
  74.  
  75. class Products {
  76.  
  77. public List<Snack> products = new ArrayList<>();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement