Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.IOException;
- import java.io.BufferedReader;
- import java.io.PrintWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.ArrayList;
- public class manager
- {
- private String titlos;
- private String year;
- private String thema;
- private String siggrafeas;
- private double timi;
- private int temaxia;
- public void menu() throws IOException
- {
- System.out.println("Plhktrologhse kapoion apo tous epomenous ari8mous gia epeksergasia");
- System.out.printf("%s\t%s\n","1:Anazhthsh arxeiou","2:Diagrafh arxeiou");
- System.out.printf("%s\t%s\n","3:Dimiourgia arxeiou ana thematikh perioxh","4:Antigrafh arxeiou");
- System.out.printf("%s\t%s\n","5:Metonomasia arxeiou","6:Eksodos");
- Scanner input=new Scanner(System.in);
- int epilogi=input.nextInt();
- while(epilogi<1||epilogi>6)
- {
- System.out.println("Plhktrologhse apo to 1-6");
- epilogi=input.nextInt();
- }
- try
- {
- if(epilogi==1)
- {
- int i=anazitisi_arxeiou();
- if(i==0)
- {
- retry();
- menu();
- }
- }
- else if(epilogi==2)
- {
- diagrafh_arxeiou();
- }
- else if(epilogi==3)
- {
- int done=dimiourgia_arxeiou();
- if(done==1)
- {
- menu();
- }
- }
- else if(epilogi==4)
- {
- int done=antigrafi_arxeiou();
- if(done==1)
- {
- System.exit(0);
- }
- else if(done==0)
- {
- retry();
- menu();
- }
- }
- else if(epilogi==5)
- {
- int i=metonomasia();
- if(i==0)
- {
- retry();
- menu();
- }
- menu();
- }
- else if(epilogi==6)
- {
- System.exit(0);
- }
- }
- catch(SecurityException secException)
- {
- System.err.println("cannot find the file");
- }
- catch(IOException e)
- {
- System.err.println(e.getMessage());
- return;
- }
- retry();
- menu();
- }
- private int metonomasia()
- {
- Scanner input=new Scanner(System.in);
- String line;
- try
- {
- while((line=anazitisi_2()).equals(null))
- {
- }
- File f=new File(line);
- System.out.println("Plhktrologhse to neo arxeio me thn kataliksi tou");
- f.renameTo(new File(input.nextLine()));
- return 1;
- }
- catch(IOException e)
- {
- System.err.println(e.getMessage());
- return 0;
- }
- }
- private int antigrafi_arxeiou() throws IOException
- {
- int i=0;
- String line;
- BufferedReader reader=null;
- PrintWriter writer=null;
- try
- {
- while((line=anazitisi_2()).equals(null))
- {
- }
- Scanner input=new Scanner(System.in);
- reader=new BufferedReader(new FileReader(line));
- System.out.println("Plhktrologhse to onoma kai th kataliksi tou neou arxeiou");
- writer=new PrintWriter(new FileWriter(input.nextLine()));
- while((line=reader.readLine())!=null)
- {
- writer.println(line);
- }
- return 1;
- }
- catch(IOException e)
- {
- System.err.println(e.getMessage());
- return i;
- }
- finally
- {
- if(reader!=null)
- {
- reader.close();
- }
- if(writer!=null)
- {
- writer.close();
- }
- }
- }
- private String anazitisi_2() throws IOException
- {
- Scanner input=new Scanner(System.in);
- System.out.println("Plhktrologhse to onoma tou arxeiou me thn kataliksi");
- String filename=input.nextLine();
- File f=new File(filename);
- if(!f.exists())
- {
- System.err.println("File does not exist");
- System.exit(1);
- }
- if(!f.canWrite())
- {
- throw new IllegalArgumentException("Write protected"+f);
- }
- if(!f.isFile())
- {
- throw new IOException("It's not a file.");
- }
- if(!f.canRead())
- {
- throw new IllegalArgumentException("Cannot read the file"+f);
- }
- if(f.exists())
- {
- return filename;
- }
- return null;
- }
- private int dimiourgia_arxeiou() throws IOException
- {
- Scanner input=new Scanner(System.in);
- System.out.println("Plhktrologhse to onoma tou arxeiou me kataliksi gia dhmiourgia");
- String filename=input.nextLine();
- File f=new File(filename);
- if(!f.exists())
- {
- f.createNewFile();
- System.out.println("New file"+filename+" has been created");
- }
- else
- {
- System.out.println("The file already exists");
- }
- System.out.println("Gia eksodo pata 0.Alliws pata 1");
- int i=input.nextInt();
- if(i==0)
- {
- System.exit(0);
- }
- return i;
- }
- private void diagrafh_arxeiou() throws IOException
- {
- Scanner input=new Scanner(System.in);
- String line;
- while((line=anazitisi_2()).equals("null"))
- {
- }
- File f=new File(line);
- if(!f.canWrite())
- {
- throw new IllegalArgumentException("The file is write protected!");
- }
- boolean success=f.delete();
- if(!success)
- {
- throw new IllegalArgumentException("Deletion failed");
- }
- if(success)
- {
- System.out.println("Deletion completed!");
- }
- }
- private void submenu(String filename) throws IOException
- {
- Scanner input=new Scanner(System.in);
- System.out.println("Plhktrologhse: 1:Eisagwgi vivliwn");
- System.out.println("2:Diagrafh vivliwn");
- System.out.println("3:Enimerwsi vivliwn");
- System.out.println("4:Eksodos");
- int epilogi=input.nextInt();
- if(epilogi==1)
- {
- eisagwgi(filename);
- }
- else if(epilogi==2)
- {
- diagrafh_vivliwn(filename);
- }
- else if(epilogi==3)
- {
- enimerwsi(filename);
- }
- else if(epilogi==4)
- {
- return;
- }
- else
- {
- while(epilogi<1&&epilogi>4)
- {
- System.out.println("Plhktrologhse: 1:Eisagwgi vivliwn");
- System.out.println("2:Diagrafh vivliwn");
- System.out.println("3:Enimerwsi vivliwn");
- System.out.println("4:Eksodos");
- epilogi=input.nextInt();
- }
- }
- retry();
- submenu(filename);
- }
- private void enimerwsi(String filename) {
- // TODO Auto-generated method stub
- }
- private void diagrafh_vivliwn(String filename) throws IOException
- {
- Scanner input=new Scanner(System.in);
- ArrayList<String> books=new ArrayList<String>();
- System.out.println("Plhtkrologhse tous titlous twn vivliwn gia diagrafh.Gia eksodo pata #");
- int i=0;
- books.add(i,input.nextLine());
- while(!books.contains("#"))
- {
- i++;
- System.out.println("Plhtkrologhse tous titlous twn vivliwn gia diagrafh.Gia eksodo pata #");
- books.add(i,input.nextLine());
- }
- BufferedReader reader=null;
- PrintWriter writer=null;
- String line;
- manager book=new manager();
- File from=new File(filename);
- File to=new File("mytemp123.txt");
- try
- {
- reader=new BufferedReader(new FileReader(from));
- writer=new PrintWriter(new FileWriter(to));
- while((line=reader.readLine())!=null)
- {
- if(books.contains(book.getTitlos()))
- {
- continue;
- }
- writer.println(line);
- }
- }
- finally
- {
- if(reader!=null)
- {
- reader.close();
- }
- if(writer!=null)
- {
- writer.close();
- }
- }
- to.renameTo(from);
- }
- private void eisagwgi(String filename) throws IOException
- {
- PrintWriter writer=null;
- manager book=new manager();
- writer=new PrintWriter(new FileWriter(filename,true));
- while(eggrafes()!=0)
- {
- writer.println(book.toString());
- }
- writer.close();
- }
- public int eggrafes()
- {
- System.out.println("Dose titlo,etos eksodhs,siggrafea,thematikh perioxh,timi kai temaxia.Gia eksodo pata 0");
- Scanner input=new Scanner(System.in);
- setTitlos(input.nextLine());
- setYear(input.nextLine());
- setSiggrafeas(input.nextLine());
- setThema(input.nextLine());
- setTimi(input.nextDouble());
- setTemaxia(input.nextInt());
- if(getTitlos().equals("0"))
- {
- return 0;
- }
- else if(getYear().equals("0"))
- {
- return 0;
- }
- else if(getSiggrafeas().equals("0"))
- {
- return 0;
- }
- else if(getThema().equals("0"))
- {
- return 0;
- }
- else if(getTimi()==0)
- {
- return 0;
- }
- else if(getTemaxia()==0)
- {
- return 0;
- }
- return 1;
- }
- private int anazitisi_arxeiou() throws IOException
- {
- Scanner input=new Scanner(System.in);
- int i=0;
- System.out.println("Plhtkrologhse onoma arxeiou me kataliksi");
- String filename=input.nextLine();
- File f=new File(filename);
- if(!f.exists())
- {
- throw new SecurityException("File does not exist");
- }
- if(!f.isFile())
- {
- throw new IllegalArgumentException(filename+"is not a file");
- }
- if(!f.canRead())
- {
- throw new IllegalArgumentException("Cannot read the file");
- }
- if(f.exists())
- {
- if(f.isFile())
- {
- i=1;
- submenu(filename);
- return i;
- }
- }
- return i;
- }
- public void retry()
- {
- System.out.println("An epi8umeite eksodo pathste 0.Gia na ksanaprospa8hsete pathste 1");
- Scanner input=new Scanner(System.in);
- int epilogi=input.nextInt();
- while(epilogi!=0 && epilogi!=1)
- {
- System.out.println("An epi8umeite eksodo pathste 0.Gia na ksanaprospa8hsete pathste 1");
- epilogi=input.nextInt();
- }
- if(epilogi==0)
- {
- System.exit(0);
- }
- }
- public void setTitlos(String title)
- {
- titlos=title;
- }
- public String getTitlos()
- {
- return titlos;
- }
- public void setYear(String year)
- {
- this.year=year;
- }
- public String getYear()
- {
- return year;
- }
- public void setSiggrafeas(String writer)
- {
- siggrafeas=writer;
- }
- public String getSiggrafeas()
- {
- return siggrafeas;
- }
- public void setThema(String subject)
- {
- thema=subject;
- }
- public String getThema()
- {
- return thema;
- }
- public void setTimi(double price)
- {
- timi=price;
- }
- public double getTimi()
- {
- return timi;
- }
- public void setTemaxia(int kommatia)
- {
- temaxia=kommatia;
- }
- public int getTemaxia()
- {
- return temaxia;
- }
- @Override
- public String toString()
- {
- return String.format("%s %s %s %s %f %d",getTitlos(),getYear(),getSiggrafeas(),getThema(),getTimi(),getTemaxia());
- }
- }
Add Comment
Please, Sign In to add comment