Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.lang.reflect.Type;
- import java.util.*;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import com.google.gson.stream.JsonReader;
- public class Main{
- public static void main(String[] args) throws Exception{
- Type type = new TypeToken<List<Snackers>>(){}.getType();
- //Reading directly from local file due to proxy issues
- JsonReader reader = new JsonReader(new FileReader("C:\\Users\\193010493\\IdeaProjects\\DeskNibbleChallenge\\src\\com\\company\\SnackerList.json"));
- Gson gson = new Gson();
- //List of all snackers
- List<Snackers> dataSnacker = gson.fromJson(reader, type);
- //Reading directly from local file due to proxy issues
- reader = new JsonReader(new FileReader("C:\\Users\\193010493\\IdeaProjects\\DeskNibbleChallenge\\src\\com\\company\\SnacksList.json"));
- //Products object containing ArrayList of Snacks
- Products dataSnacks = gson.fromJson(reader, Products.class);
- //ArrayList to be populated with any snackers that match vendor list
- List<Snackers> faveSnackers = new ArrayList<>();
- //Iterate through to match Snacker's fave snack to any found in vendor list
- //Runs in O(n^2), needs to be more efficient for larger datasets
- for(int i=0; i < dataSnacker.size(); i++){
- for (int j=0; j < dataSnacks.products.size(); j++){
- if ((dataSnacker.get(i).fave_snack).equals(dataSnacks.products.get(j).title)){
- faveSnackers.add(dataSnacker.get(i));
- }
- }
- }
- //Iterate through our populated faveSnackers to display their fave snack, email, and cost of snack
- for(int i =0; i<faveSnackers.size();i++){
- System.out.println(faveSnackers.get(i).first_name +" "+ faveSnackers.get(i).last_name + " listed " + faveSnackers.get(i).fave_snack + " as their fave snack!");
- System.out.println("Their email is " + faveSnackers.get(i).email);
- System.out.println("If they purchase through Desk Nibbles, they would pay $" + getPrice(faveSnackers.get(i).fave_snack, dataSnacks)+"0");
- System.out.println();
- }
- }
- private static double getPrice(String snackName, Products dataSnacks){
- for (int i =0; i<dataSnacks.products.size(); i++){
- if (snackName.equals(dataSnacks.products.get(i).title)){
- return dataSnacks.products.get(i).variants.get(0).price;
- }
- }
- return (0);
- }
- }
- class Snackers {
- public long id;
- public String first_name;
- public String last_name;
- public String email;
- public String gender;
- public String ip_address;
- public String fave_snack;
- }
- class Products {
- public List<Snack> products = new ArrayList<>();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement