Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package seqSort;
- import java.io.File;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws Exception{
- //parsing and reading the CSV file data into the customer (object) array
- // This definitely works, have used it for a different method
- Scanner sc = new Scanner(new File("customers_data.csv"));
- Customer[] customers = new Customer[10000];
- Customer[] results = new Customer[10];
- // this will just print the header
- System.out.println(sc.nextLine());
- int i = 0; String st = "";
- while (sc.hasNextLine()) //returns a boolean value
- {
- st = sc.nextLine();
- String[] data = st.split(",");
- customers[i] = new Customer(Integer.parseInt(data[0]), data[1], data[2], data[3], data[4].charAt(0), data[5]);
- i++;
- }
- sc.close(); //closes the scanner#
- SequentialSearch(customers, "name");
- Arrays.toString(results);
- for (i = 0; i < results.length; i++) {
- System.out.print(results[i] + "\n ");
- }
- }
- static Customer[] SequentialSearch(Customer[] customers, String searchKey){
- int n = customers.length;
- int a = 0;
- Customer[] results = new Customer[n];
- for(int i = 0; i < n; i++){
- if (customers[i].equals(searchKey)) {
- results[a] = customers[i];
- return results;
- }
- }
- return results.length > 0 ? results:null;
- }
- }
- _____________________________________________________________________________________________________________________________________
- /*Customer class*/
- ____________________________________________________________________________
- public class Customer implements Comparable<Object>{
- private int cusNo;
- private String dateOfBirth;
- private String firstName;
- private String lastName;
- private char gender;
- private String joinDate;
- public Customer(int cusNo, String dateOfBirth, String firstName, String lastName, char gender, String joinDate)
- {
- this.cusNo = cusNo;
- this.dateOfBirth = dateOfBirth;
- this.firstName = firstName;
- this.lastName = lastName;
- this.gender = gender;
- this.joinDate = joinDate;
- }
- public int getCusNo() {
- return cusNo;
- }
- public void setCusNo(int cusNo) {
- this.cusNo = cusNo;
- }
- public String getDateOfBirth() {
- return dateOfBirth;
- }
- public void setDateOfBirth(String dateOfBirth) {
- this.dateOfBirth = dateOfBirth;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public char getGender() {
- return gender;
- }
- public void setGender(char gender) {
- this.gender = gender;
- }
- public String getJoinDate() {
- return joinDate;
- }
- public void setJoinDate(String joinDate) {
- this.joinDate = joinDate;
- }
- // NOTE:only allows comparisons based on the firstName
- @Override
- public int compareTo(Object obj) {
- Customer cus = (Customer)obj;
- return firstName.compareTo(cus.getFirstName());
- }
- // return a String containing the customer details
- @Override
- public String toString()
- {
- return cusNo+" "+dateOfBirth+" "+firstName+" "+lastName+" "+gender +" "+ joinDate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement