Attachment 'Teatro.java'

Download

   1 import java.io.BufferedReader;
   2 import java.io.FileNotFoundException;
   3 import java.io.FileReader;
   4 import java.io.FileWriter;
   5 import java.io.IOException;
   6 import java.io.PrintWriter;
   7 import java.util.ArrayList;
   8 
   9 import javax.swing.JOptionPane;
  10 
  11 public class Teatro {
  12 	
  13 	ArrayList<Prenotazione> prenotati=new ArrayList<Prenotazione>();
  14 	
  15 	public void aggiungiPrenotazione(String cognome, int posto)
  16 	{
  17 		prenotati.add(new Prenotazione(cognome,posto));
  18 	}
  19 	
  20 	public ArrayList<Prenotazione> cercaUnaPrenotazione(String cognome)
  21 	{
  22 		ArrayList<Prenotazione> temp=new ArrayList<Prenotazione>();
  23 		
  24 		for(Prenotazione p:prenotati)
  25 		{
  26 			if(p.getCognome().equals(cognome))
  27 				temp.add(p);
  28 		}
  29 		
  30 		return temp;
  31 		
  32 	}
  33 	
  34 	public void cancellaPrenotazione(String cognome)
  35 	{
  36 		ArrayList<Prenotazione> temp=cercaUnaPrenotazione(cognome);
  37 	
  38 		for(Prenotazione p: temp)
  39 			prenotati.remove(p);
  40 	}
  41 	
  42 	public void scriviSuFile()
  43 	{
  44 		try {
  45 			//Sostituire con un percorso valido sul proprio pc
  46 			FileWriter f = new FileWriter("/home/marco/Scrivania/prenotati/prenotati.txt");
  47 			
  48 			PrintWriter p = new PrintWriter(f);
  49 			
  50 			String cognome=JOptionPane.showInputDialog("Inserisci il nome della prenotazione. digita fine per terminare");
  51 			
  52 			while(!cognome.equals("fine"))
  53 			{
  54 				p.println(cognome);
  55 				cognome=JOptionPane.showInputDialog("Inserisci il nome della prenotazione. digita fine per terminare");
  56 			}
  57 			
  58 			p.close();
  59 			
  60 		} catch (IOException e) {
  61 			System.out.println("C'� stato un errore di I/O");
  62 		}
  63 	}
  64 	
  65 	public void aggiungiDaListaFile()
  66 	{
  67 		 try {
  68 			FileReader f = new FileReader("/home/marco/Scrivania/prenotati/prenotati.txt");
  69 
  70 			BufferedReader lettore = new BufferedReader(f);
  71 			
  72 			String riga=lettore.readLine();
  73 			int contatorePosti=1;
  74 			
  75 			while(riga!=null)
  76 			{
  77 				prenotati.add(new Prenotazione(riga,contatorePosti));
  78 				riga=lettore.readLine();
  79 			}
  80 			
  81 			f.close();
  82 			
  83 		 } catch (FileNotFoundException e) {
  84 			System.out.println("Il file non c'e'!!!");
  85 		} catch (IOException e) {
  86 			System.out.println("C'e' stato un errore di I/O");
  87 		}
  88 	}
  89 	public static void main(String[] args) {
  90 		Teatro t=new Teatro();
  91 		t.scriviSuFile();
  92 		t.aggiungiDaListaFile();
  93 		for(Prenotazione p:t.prenotati)
  94 			System.out.println(p.getCognome());
  95 	}
  96 
  97 }

Attached Files

You are not allowed to attach a file to this page.