Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Proiect;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.StringTokenizer;
- /**
- *
- * @author Nan Mihai
- */
- public class Aplicatie extends ArraySet {
- public static void printMenu() {
- System.out.println("1. Add an item");
- System.out.println("2. Remove");
- System.out.println("3. Search");
- System.out.println("4. Print items from collection");
- System.out.println("5. Save collection data to file");
- System.out.println("6. Load data from file");
- }
- public static void main(String args[]) throws IOException{
- ArraySet vect = new ArraySet();
- int k;
- Scanner in = new Scanner(System.in);
- printMenu();
- String text;
- text = in.nextLine();
- while(text.compareTo("exit") != 0)
- {
- k = Integer.parseInt(text);
- if(k == 1) {
- Student s1;
- String name, aux;
- int age;
- float mark;
- System.out.print("Name: ");
- name = in.nextLine();
- System.out.print("Age: ");
- aux = in.nextLine();
- age = Integer.parseInt(aux);
- System.out.print("Mark: ");
- aux = in.nextLine();
- mark = Float.parseFloat(aux);
- s1 = new Student(name, age, mark);
- vect.add(s1);
- } else if(k == 2) {
- Student s1;
- String name, aux;
- int age;
- float mark;
- System.out.print("Name: ");
- name = in.nextLine();
- System.out.print("Age: ");
- aux = in.nextLine();
- age = Integer.parseInt(aux);
- System.out.print("Mark: ");
- aux = in.nextLine();
- mark = Float.parseFloat(aux);
- s1 = new Student(name, age, mark);
- vect.remove(s1);
- } else if(k == 3) {
- Student s1;
- String name, aux;
- int age;
- float mark;
- System.out.print("Name: ");
- name = in.nextLine();
- System.out.print("Age: ");
- aux = in.nextLine();
- age = Integer.parseInt(aux);
- System.out.print("Mark: ");
- aux = in.nextLine();
- mark = Float.parseFloat(aux);
- s1 = new Student(name, age, mark);
- if(vect.contains(s1) == true) {
- System.out.println("Exist!");
- } else {
- System.out.println("Not exist!");
- }
- } else if(k == 4) {
- for(Iterator i = vect.iterator(); i.hasNext(); ) {
- Student val = (Student) i.next();
- System.out.println(val);
- }
- } else if(k == 5) {
- String nume = "cale/nume_fisier.txt";
- try (RandomAccessFile raf = new RandomAccessFile(nume, "w")) {
- for(Iterator i = vect.iterator(); i.hasNext(); ) {
- Student val = (Student) i.next();
- raf.writeChars(val.toString());
- }
- raf.close();
- }
- } else if(k == 6) {
- String nume;
- nume = "cale//nume_fisier.txt";
- try (RandomAccessFile raf = new RandomAccessFile(nume, "r")) {
- String s;
- String name = "";
- int age = 20;
- float mark = 10;
- Student s1;
- while((s = raf.readLine()) != null) {
- StringTokenizer st = new StringTokenizer(s, " ");
- while(st.hasMoreTokens()) {
- String token = st.nextToken();
- s1 = new Student(name, age, mark);
- //prelucrezi token-ul cum am facut eu la prima sa scoti name / age / mark
- vect.add(s1);
- }
- }
- }
- }
- printMenu();
- text = in.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment