Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. mport java.awt.*; import javax.swing.*; import sun.audio.*; import java.io.*; import java.util.Scanner; public class Macrosoft extends JFrame { /*Macrosoft(JTextArea text) { setLayout(new FlowLayout()); //everything gets displayed depending on window size - "flows with it" setTitle("Macrosoft"); //title of window add(text); }*/ public static void main(String[] args) throws IOException { //opening menu - ALI DID THIS PART String[] choices = { "List", "Search", "Add" }; //user chooses choice String input = (String) JOptionPane.showInputDialog(null, "Choose what you want:", "Macrosoft", JOptionPane.QUESTION_MESSAGE, null, choices, choices[1]); BufferedReader br = new BufferedReader(new FileReader("C:/Users/Ali Hijazi/Desktop/My Folder/Comsci/FileReading/staff_1.csv")); String n = "NAME", sn = "SURNAME", e = "EMAIL", nat = "NATIONALITY", g = "GENDER", l = "LANGUAGE"; //JAMES DID THIS PART File log = new File("C:/Users/Ali Hijazi/Desktop/My Folder/Comsci/FileReading/staff_1.csv"); PrintWriter pw = new PrintWriter(new FileWriter(log, true)); //EUGENE DID THIS PART if (input.equals("List")) { System.out.println("Reading file..."); //The headers made below (with printf) do not show up when the employees are //listed because there are 1000 which is a lot so it disappears along with //some of the names at the top of the list. This is NOT a fault in the code. System.out.printf("%-12s %-10s %-35s %-30s %-10s %s\n",n,sn,e,nat,g,l); String line; //initialise arrays String[] name = new String[1500]; String[] surname = new String[1500]; String[] email = new String[1500]; String[] countries = new String[1500]; char[]gender = new char[1500]; String[] languages = new String[1500]; int counter = 0;//begin counter at 0 while((line = br.readLine()) != null) {//keeps looping until there are no more employees String[] data = line.split(","); name[counter] = data[0]; surname[counter] = data[1]; email[counter] = data[2]; countries[counter] = data[3]; gender[counter] = data[4].charAt(0); languages[counter] = data [5]; counter++; //counts number of employees //print out every employee line by line... NICELY System.out.printf("%-12s %-10s %-35s %-30s %-10s %s\n",data[0],data[1],data[2],data[3],data[4],data[5]); } }//END OF EUGENE //ALI DID THIS PART else if (input.equals("Search")) { System.out.print("Enter the first name of who you want to search for > "); Scanner kb = new Scanner (System.in); String first_name = kb.nextLine(); //user enters first name System.out.print("Enter his/her surname please > "); String surname = kb.nextLine(); //user enters surname kb.close(); String line; while((line = br.readLine()) != null) { String[] data = line.split(","); if (first_name.equalsIgnoreCase(data[0]) && surname.equalsIgnoreCase(data[1])) { //if same name is found System.out.println("Employee found! Here are the details:"); System.out.printf("%-12s %-10s %-35s %-30s %-10s %s\n",n,sn,e,nat,g,l); //print out details clearly System.out.printf("%-12s %-10s %-35s %-30s %-10s %s\n",data[0],data[1],data[2],data[3],data[4],data[5]); } } } //ALI CONTINUED... else if (input.equals("Add")) { String line; int lines = 0; while ((line = br.readLine()) != null) { lines++; //counts employees } if (lines < 1500) {//if there is space for more employees, get the details for next employee that will be added System.out.println("There are " + lines + " employees currently. "); System.out.print("Enter the first name of the new employee > "); Scanner kb = new Scanner(System.in); String firstname = kb.nextLine(); //get first name System.out.print("Enter the surname please > "); String surname = kb.nextLine(); //get surname System.out.print("Enter the email > "); String email = kb.nextLine(); //get email System.out.print("Enter the nationality > "); String nationality = kb.nextLine(); //get nationality System.out.print("Enter the gender (M/F) > "); String gender = kb.nextLine(); //get gender System.out.print("Finally, enter the language please > "); String language = kb.nextLine(); //get language System.out.println("Thanks... adding these to the database..."); //JAMES FIXED A PAST PROBLEM HERE... pw.write(firstname + "," + surname + "," + email + "," + nationality + "," + gender + "," + language); System.out.println("All done!"); pw.close(); //close print writer kb.close(); //close scanner } else{ //database is full System.out.println("We cannot add anymore employees."); } br.close(); }//END OF ALI } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement