Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package arrays;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class main {
- public static int searchByName(ArrayList<person> personList) {
- System.out.println("Enter name(first and last, first letters capitlized):");
- String name = System.console().readLine();
- for(int i = 0; i < personList.size(); i++){
- //System.out.println(name + "=" + personList.get(i).getName());
- if (personList.get(i).getName().equals(name)){
- return i;
- }
- }
- return -1;
- };
- public static int searchByCreditCard(ArrayList<person> personList) {
- System.out.println("Enter name(first and last, first letters capitlized):");
- String name = System.console().readLine();
- for(int i = 0; i < personList.size(); i++){
- //System.out.println(name + "=" + personList.get(i).getcCard());
- if (personList.get(i).getcCard().equals(name)){
- return i;
- }
- }
- return -1;
- };
- public static int searchByDateOfBirth(ArrayList<person> personList) {
- System.out.println("Enter name(first and last, first letters capitlized):");
- String name = System.console().readLine();
- for(int i = 0; i < personList.size(); i++){
- //System.out.println(name + "=" + personList.get(i).getdateOfBirth());
- if (personList.get(i).getdateOfBirth().equals(name)){
- return i;
- }
- }
- return -1;
- };
- public static int searchBySocialSecurity(ArrayList<person> personList) {
- System.out.println("Enter name(first and last, first letters capitlized):");
- String name = System.console().readLine();
- for(int i = 0; i < personList.size(); i++){
- //System.out.println(name + "=" + personList.get(i).getsSec());
- if (personList.get(i).getsSec().equals(name)){
- return i;
- }
- }
- return -1;
- };
- public static void info(person personObject) {
- System.out.println('\n' + "Information:");
- System.out.println("Name: " + personObject.getName() );
- System.out.println("Credit Card Number: " + personObject.getcCard() );
- System.out.println("Date Of Birth: " + personObject.getdateOfBirth() );
- System.out.println("Social Security Number: " + personObject.getsSec() );
- System.out.println('\n');
- return;
- };
- public static void search(ArrayList<person> personList) {
- System.out.println("Search by?(name, creditcard, dateofbirth, ssn");
- String field = System.console().readLine();
- int index = -1;
- if (field.equals("name")) {
- index = searchByName(personList);
- info(personList.get(index));
- }
- else if (field.equals("creditcard")) {
- index = searchByCreditCard(personList);
- person personObject = personList.get(index);
- info(personList.get(index));
- }
- else if (field.equals("dateofbirth")) {
- index = searchByDateOfBirth(personList);
- person personObject = personList.get(searchByDateOfBirth(personList));
- info(personList.get(index));
- }
- else if (field.equals("ssn")) {
- index = searchBySocialSecurity(personList);
- person personObject = personList.get(searchBySocialSecurity(personList));
- info(personList.get(index));
- }
- return;
- };
- public static void main (String[] args) {
- FileInputStream fIn;
- int state = 1;
- String name = "";
- boolean acceptingCommands = true;
- try{
- System.out.println("Get file pls");
- String fileName = System.console().readLine();
- FileReader fileReader = new FileReader(fileName);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- StringBuffer stringBuffer = new StringBuffer();
- String line = "";
- String cCard = "";
- String dateOfBirth = "";
- String sSec = "";
- ArrayList<person> personList = new ArrayList<person>();
- fIn = new FileInputStream (fileName);
- Scanner scanner = new Scanner(fIn);
- // System.out.flush();
- //System.out.println("hey im gay");
- while(scanner.hasNextLine()){
- scanner.nextLine();
- line = bufferedReader.readLine();
- //System.out.println(line);
- person currentPerson = new person();
- for (int i = 0; i < line.length(); i++){
- //NAME
- if (state == 1){
- if (line.charAt(i) == ','){
- state = state + 1;
- //System.out.println(name + "\n");
- currentPerson.setName(name);
- name = "";
- }
- else{
- name = name + line.charAt(i);
- }
- }
- //Credit Card
- else if (state == 2){
- if (line.charAt(i) == ','){
- state = state + 1;
- //System.out.println(cCard + "\n");
- currentPerson.setcCard(cCard);
- cCard = "";
- }
- else{
- cCard = cCard + line.charAt(i);
- }
- }
- //Date of Birth
- else if (state == 3){
- if (line.charAt(i) == ','){
- state = state + 1;
- //System.out.println(dateOfBirth + "\n");
- currentPerson.setdateOfBirth(dateOfBirth);
- dateOfBirth = "";
- }
- else{
- dateOfBirth = dateOfBirth + line.charAt(i);
- }
- }
- // Social Security
- else if (state == 4){
- sSec = sSec + line.charAt(i);
- }
- }
- state = 1;
- //System.out.println(sSec + "\n");
- currentPerson.setsSec(sSec);
- sSec = "";
- personList.add(currentPerson);
- }
- scanner.close();
- /*for (person thisPerson : personList){
- System.out.println("Name: " + thisPerson.getName() );
- System.out.println("Credit Card Number: " + thisPerson.getcCard() );
- System.out.println("Date Of Birth: " + thisPerson.getdateOfBirth() );
- System.out.println("Social Security Number: " + thisPerson.getsSec() );
- }*/
- while (acceptingCommands == true){
- String commandWord;
- System.out.println("What's up dude(Enter a command(type help for a list of commands))?");
- commandWord = System.console().readLine();
- if (commandWord.equals("exit")){
- acceptingCommands = false;
- System.out.println("Closing");
- }
- else if (commandWord.equals("help")){
- System.out.println("Commands: ");
- System.out.println("search, sort, print, edit");
- }
- else if (commandWord.equals("search")){
- search(personList);
- }
- }
- }
- catch(IOException ex){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement