Answer to Question #239300 in Java | JSP | JSF for Reena Dhingra

Question #239300

Implement a standalone product search program in Java that lists matching products for a user who is looking for T-shirts.

You are given 3 CSV files, each containing the T-shirts data for Nike, Puma and Adidas respectively. Sample CSV files links are attached here for reference. You can add more data in existing files or can add more CSV files for another companies.



1
Expert's answer
2021-09-20T00:11:50-0400
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc=new Scanner(System.in);
        String dept_loc,arrival_loc,date;
        int choiceCode;

        System.out.print("Enter Departure Location  : ");
        dept_loc=sc.nextLine().toUpperCase();
        System.out.print("Enter Arrival Location  : ");
        arrival_loc=sc.nextLine().toUpperCase();
        System.out.print("Enter Date   : ");
        date=sc.nextLine().toUpperCase();
        System.out.print("Enter Output Preference :   1. Fare \t 2. Flight Duration \nEnter Preference Choice Code : ");
        choiceCode=sc.nextInt();

        DataController fc=new DataController();

        fc.searchData("src/FlightInformation/AIR FRANCE",dept_loc,arrival_loc,date);
        fc.searchData("src/FlightInformation/BRITISH AIRWAYS",dept_loc,arrival_loc,date);
        fc.searchData("src/FlightInformation/LUFTHANSA AIRLINES",dept_loc,arrival_loc,date);
        fc.updateView(choiceCode);
    }
}

class DataController {
    ArrayList<Model> flightList=new ArrayList<Model>();
    ArrayList<String> arr;
    DataView view=new DataView();

    public void searchData(String filename, String dept_loc, String arrival_loc, String date) throws FileNotFoundException
    {
        Scanner sc = new Scanner(new File(filename));
        while(sc.hasNext()) {
            String line = sc.nextLine().toUpperCase().toString();
            if (!line.isEmpty()) {
                StringTokenizer token = new StringTokenizer(line, "|");
                arr = new ArrayList<>(line.length());
                while (token.hasMoreTokens()) {
                    arr.add(token.nextToken());
                }
                if (arr.get(1).equals(dept_loc) && arr.get(2).equals(arrival_loc) && arr.get(3).equals(date)) {
                    Model model = new Model(arr.get(0), arr.get(1), arr.get(2), arr.get(3), Integer.parseInt(arr.get(4)), Float.parseFloat(arr.get(5)));
                    flightList.add(model);
                }
            }
        }
    }

    public void updateView(int choiceCode)
    {
        if(choiceCode==1)
        {
            Collections.sort(flightList, new Comparator<Model>() {
                @Override
                public int compare(Model o1, Model o2) {
                    return o1.getFare() - o2.getFare();
                }
            });
        }
        else if(choiceCode==2)
        {
            Collections.sort(flightList, new Comparator<Model>() {
                @Override
                public int compare(Model o1, Model o2) {
                    return (int)( o1.getDuration() - o2.getDuration());
                }
            });
        }
        else
        {
            System.out.println("Wrong Choice.");
            return;
        }
        view.viewFlights(flightList);

    }
}

class Model {

    private String flightNum;
    private String departLoc;
    private String arrivalLoc;
    private String date;
    private int fare;
    private float duration;

    public Model(){}

    public Model(String flightNum, String departLoc, String arrivalLoc, String date, int fare, float duration) {
        this.flightNum = flightNum;
        this.departLoc = departLoc;
        this.arrivalLoc = arrivalLoc;
        this.date = date;
        this.fare = fare;
        this.duration = duration;
    }

    public String getFlightNum()
    {
        return flightNum;
    }
    public void setFlightNum(String flightNum)
    {
        this.flightNum=flightNum;
    }

    public String getDepartLoc()
    {
        return departLoc;
    }
    public void setDepartLoc(String departLoc)
    {
        this.departLoc=departLoc;
    }

    public String getArrivalLoc()
    {
        return arrivalLoc;
    }
    public void setArrivalLoc(String arrivalLoc)
    {
        this.arrivalLoc=arrivalLoc;
    }
    public String getDate()
    {
        return date;
    }
    public void setDate(String date)
    {
        this.date=date;
    }

    public int getFare()
    {
        return fare;
    }
    public void setFare(int fare)
    {
        this.fare=fare;
    }

    public float getDuration()
    {
        return duration;
    }
    public void setDuration(float duration)
    {
        this.duration=duration;
    }

}

class DataView {

    public void viewFlights(ArrayList<Model> flightList)
    {
        System.out.println("\n \t\t ***** FLIGHT INFORMATION *****");
        System.out.println("FLIGHT_NO|DEP_LOC|ARR_LOC|VALID_TILL | FARE |DURATION|");
        for(Model f:flightList)
        {
            System.out.print(" "+f.getFlightNum());
            System.out.print("\t |\t"+f.getDepartLoc());
            System.out.print("\t |\t"+f.getArrivalLoc());
            System.out.print("\t |"+f.getDate());
            System.out.print(" | "+f.getFare());
            System.out.println("\t|  "+f.getDuration()+"\t |");
        }
        if(flightList.isEmpty())
        {
            System.out.println("Flights Not Available.");
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS