Advertisement
Guest User

Untitled

a guest
Apr 13th, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package seqSort;
  2.  
  3.  
  4.    
  5.     import java.io.File;
  6.     import java.util.*;
  7.  
  8.     public class Main {
  9.  
  10.        
  11.         public static void main(String[] args) throws Exception{
  12.             //parsing and reading the CSV file data into the customer (object) array
  13.             // This definitely works, have used it for a different method
  14.             Scanner sc = new Scanner(new File("customers_data.csv"));
  15.             Customer[] customers = new Customer[10000];
  16.             Customer[] results = new Customer[10];
  17.  
  18.             // this will just print the header
  19.             System.out.println(sc.nextLine());
  20.  
  21.             int i = 0; String st = "";
  22.             while (sc.hasNextLine())  //returns a boolean value
  23.             {
  24.                 st = sc.nextLine();
  25.                 String[] data = st.split(",");
  26.                 customers[i] = new Customer(Integer.parseInt(data[0]), data[1], data[2], data[3], data[4].charAt(0), data[5]);
  27.                 i++;
  28.             }
  29.             sc.close();  //closes the scanner#
  30.             SequentialSearch(customers, "name");
  31.             Arrays.toString(results);
  32.              for (i = 0; i < results.length; i++) {
  33.                  System.out.print(results[i] + "\n ");
  34.              }
  35.  
  36.              
  37.  
  38. }
  39.        
  40.         static Customer[] SequentialSearch(Customer[] customers, String searchKey){
  41.             int n = customers.length;
  42.             int a = 0;
  43.             Customer[] results = new Customer[n];
  44.             for(int i = 0; i < n; i++){
  45.                 if (customers[i].equals(searchKey)) {
  46.                     results[a] = customers[i];
  47.                     return results;
  48.  
  49.                 }
  50.  
  51.             }
  52.             return results.length > 0 ? results:null;
  53.             }
  54.  
  55.     }
  56.  
  57.    
  58. _____________________________________________________________________________________________________________________________________
  59. /*Customer class*/
  60.     ____________________________________________________________________________
  61.  
  62. public class Customer implements Comparable<Object>{
  63.  
  64.     private int cusNo;
  65.     private String dateOfBirth;
  66.     private String firstName;
  67.     private String lastName;
  68.     private char gender;
  69.     private String joinDate;
  70.  
  71.  
  72.     public Customer(int cusNo, String dateOfBirth, String firstName, String lastName, char gender, String joinDate)
  73.     {
  74.         this.cusNo = cusNo;
  75.         this.dateOfBirth = dateOfBirth;
  76.         this.firstName = firstName;
  77.         this.lastName = lastName;
  78.         this.gender = gender;
  79.         this.joinDate = joinDate;
  80.     }
  81.  
  82.     public int getCusNo() {
  83.         return cusNo;
  84.     }
  85.  
  86.     public void setCusNo(int cusNo) {
  87.         this.cusNo = cusNo;
  88.     }
  89.  
  90.     public String getDateOfBirth() {
  91.         return dateOfBirth;
  92.     }
  93.  
  94.     public void setDateOfBirth(String dateOfBirth) {
  95.         this.dateOfBirth = dateOfBirth;
  96.     }
  97.  
  98.     public String getFirstName() {
  99.         return firstName;
  100.     }
  101.  
  102.     public void setFirstName(String firstName) {
  103.         this.firstName = firstName;
  104.     }
  105.  
  106.     public String getLastName() {
  107.         return lastName;
  108.     }
  109.  
  110.     public void setLastName(String lastName) {
  111.         this.lastName = lastName;
  112.     }
  113.  
  114.     public char getGender() {
  115.         return gender;
  116.     }
  117.  
  118.     public void setGender(char gender) {
  119.         this.gender = gender;
  120.     }
  121.  
  122.     public String getJoinDate() {
  123.         return joinDate;
  124.     }
  125.  
  126.     public void setJoinDate(String joinDate) {
  127.         this.joinDate = joinDate;
  128.     }
  129.  
  130.  
  131.     // NOTE:only allows comparisons based on the firstName
  132.     @Override
  133.     public int compareTo(Object obj) {
  134.         Customer cus = (Customer)obj;
  135.         return firstName.compareTo(cus.getFirstName());
  136.     }
  137.  
  138.     // return a String containing the customer details
  139.     @Override
  140.     public String toString()
  141.     {
  142.         return cusNo+" "+dateOfBirth+" "+firstName+" "+lastName+" "+gender +" "+ joinDate;
  143.     }
  144.  
  145. }
  146.  
  147.    
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement