mihainan

Marius

Nov 27th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package Proiect;
  2.  
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.Scanner;
  8. import java.util.Set;
  9. import java.util.StringTokenizer;
  10.  
  11. /**
  12.  *
  13.  * @author Nan Mihai
  14.  */
  15. public class Aplicatie extends ArraySet {
  16.     public static void printMenu() {
  17.         System.out.println("1. Add an item");
  18.         System.out.println("2. Remove");
  19.         System.out.println("3. Search");
  20.         System.out.println("4. Print items from collection");
  21.         System.out.println("5. Save collection data to file");
  22.         System.out.println("6. Load data from file");
  23.     }
  24.    
  25.     public static void main(String args[]) throws IOException{
  26.         ArraySet vect = new ArraySet();
  27.         int k;
  28.         Scanner in = new Scanner(System.in);
  29.         printMenu();
  30.         String text;
  31.         text = in.nextLine();
  32.         while(text.compareTo("exit") != 0)
  33.         {
  34.             k = Integer.parseInt(text);
  35.             if(k == 1) {
  36.                 Student s1;
  37.                 String name, aux;
  38.                 int age;
  39.                 float mark;
  40.                 System.out.print("Name: ");
  41.                 name = in.nextLine();
  42.                 System.out.print("Age: ");
  43.                 aux = in.nextLine();
  44.                 age = Integer.parseInt(aux);
  45.                 System.out.print("Mark: ");
  46.                 aux = in.nextLine();
  47.                 mark = Float.parseFloat(aux);
  48.                 s1 = new Student(name, age, mark);
  49.                 vect.add(s1);
  50.             } else if(k == 2) {
  51.                 Student s1;
  52.                 String name, aux;
  53.                 int age;
  54.                 float mark;
  55.                 System.out.print("Name: ");
  56.                 name = in.nextLine();
  57.                 System.out.print("Age: ");
  58.                 aux = in.nextLine();
  59.                 age = Integer.parseInt(aux);
  60.                 System.out.print("Mark: ");
  61.                 aux = in.nextLine();
  62.                 mark = Float.parseFloat(aux);
  63.                 s1 = new Student(name, age, mark);
  64.                 vect.remove(s1);
  65.             } else if(k == 3) {
  66.                 Student s1;
  67.                 String name, aux;
  68.                 int age;
  69.                 float mark;
  70.                 System.out.print("Name: ");
  71.                 name = in.nextLine();
  72.                 System.out.print("Age: ");
  73.                 aux = in.nextLine();
  74.                 age = Integer.parseInt(aux);
  75.                 System.out.print("Mark: ");
  76.                 aux = in.nextLine();
  77.                 mark = Float.parseFloat(aux);
  78.                 s1 = new Student(name, age, mark);
  79.                 if(vect.contains(s1) == true) {
  80.                     System.out.println("Exist!");
  81.                 } else {
  82.                     System.out.println("Not exist!");
  83.                 }
  84.             } else if(k == 4) {
  85.                 for(Iterator i = vect.iterator(); i.hasNext(); ) {
  86.                     Student val = (Student) i.next();
  87.                     System.out.println(val);
  88.                 }
  89.             } else if(k == 5) {
  90.                 String nume = "cale/nume_fisier.txt";
  91.                 try (RandomAccessFile raf = new RandomAccessFile(nume, "w")) {
  92.                     for(Iterator i = vect.iterator(); i.hasNext(); ) {
  93.                         Student val = (Student) i.next();
  94.                         raf.writeChars(val.toString());
  95.                     }
  96.                     raf.close();
  97.                 }
  98.             } else if(k == 6) {
  99.                 String nume;
  100.                 nume = "cale//nume_fisier.txt";
  101.                 try (RandomAccessFile raf = new RandomAccessFile(nume, "r")) {
  102.                     String s;
  103.                     String name = "";
  104.                     int age = 20;
  105.                     float mark = 10;
  106.                     Student s1;
  107.                     while((s = raf.readLine()) != null) {
  108.                         StringTokenizer st = new StringTokenizer(s, " ");
  109.                         while(st.hasMoreTokens()) {
  110.                             String token = st.nextToken();
  111.                             s1 = new Student(name, age, mark);
  112.                             //prelucrezi token-ul cum am facut eu la prima sa scoti name / age / mark
  113.                            
  114.                             vect.add(s1);
  115.                         }
  116.                     }
  117.                 }
  118.                
  119.             }
  120.             printMenu();
  121.             text = in.nextLine();
  122.         }
  123.        
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment