Guest User

Untitled

a guest
Oct 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.82 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.IOException;
  3. import java.io.BufferedReader;
  4. import java.io.PrintWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.util.ArrayList;
  9. public class manager
  10. {
  11.     private String titlos;
  12.     private String year;
  13.     private String thema;
  14.     private String siggrafeas;
  15.     private double timi;
  16.     private int temaxia;
  17.    
  18.     public void menu() throws IOException
  19.     {
  20.         System.out.println("Plhktrologhse kapoion apo tous epomenous ari8mous gia epeksergasia");
  21.         System.out.printf("%s\t%s\n","1:Anazhthsh arxeiou","2:Diagrafh arxeiou");
  22.        
  23.          
  24.         System.out.printf("%s\t%s\n","3:Dimiourgia arxeiou ana thematikh perioxh","4:Antigrafh arxeiou");
  25.         System.out.printf("%s\t%s\n","5:Metonomasia arxeiou","6:Eksodos");
  26.         Scanner input=new Scanner(System.in);
  27.         int epilogi=input.nextInt();
  28.         while(epilogi<1||epilogi>6)
  29.         {
  30.             System.out.println("Plhktrologhse apo to 1-6");
  31.             epilogi=input.nextInt();
  32.         }
  33.         try
  34.         {
  35.         if(epilogi==1)
  36.         {
  37.             int i=anazitisi_arxeiou();
  38.            
  39.             if(i==0)
  40.             {
  41.                 retry();
  42.                 menu();
  43.             }
  44.         }
  45.         else if(epilogi==2)
  46.         {
  47.             diagrafh_arxeiou();
  48.         }
  49.         else if(epilogi==3)
  50.         {
  51.             int done=dimiourgia_arxeiou();
  52.             if(done==1)
  53.             {
  54.                 menu();
  55.             }
  56.            
  57.         }
  58.         else if(epilogi==4)
  59.         {
  60.            
  61.                
  62.            
  63.             int done=antigrafi_arxeiou();
  64.            
  65.              if(done==1)
  66.             {
  67.                 System.exit(0);
  68.             }
  69.            
  70.             else if(done==0)
  71.             {
  72.                 retry();
  73.                 menu();
  74.             }
  75.         }
  76.         else if(epilogi==5)
  77.         {
  78.            
  79.            
  80.             int i=metonomasia();
  81.            
  82.              if(i==0)
  83.             {
  84.                 retry();
  85.                 menu();
  86.             }
  87.             menu();
  88.         }
  89.        
  90.         else if(epilogi==6)
  91.         {
  92.             System.exit(0);
  93.         }
  94.         }
  95.         catch(SecurityException secException)
  96.         {
  97.             System.err.println("cannot find the file");
  98.            
  99.         }
  100.         catch(IOException e)
  101.         {
  102.             System.err.println(e.getMessage());
  103.             return;
  104.         }
  105.         retry();
  106.         menu();
  107.     }
  108.  
  109.  
  110.    
  111.     private int metonomasia()
  112.     {
  113.         Scanner input=new Scanner(System.in);
  114.         String line;
  115.        
  116.         try
  117.         {
  118.            
  119.         while((line=anazitisi_2()).equals(null))
  120.         {
  121.            
  122.         }
  123.         File f=new File(line);
  124.        
  125.         System.out.println("Plhktrologhse to neo arxeio me thn kataliksi tou");
  126.         f.renameTo(new File(input.nextLine()));
  127.         return 1;
  128.         }
  129.    
  130.         catch(IOException e)
  131.         {
  132.             System.err.println(e.getMessage());
  133.             return 0;
  134.         }
  135.     }
  136.    
  137.        
  138.        
  139.    
  140.  
  141.  
  142.  
  143.     private int antigrafi_arxeiou() throws IOException
  144.     {
  145.         int i=0;
  146.         String line;
  147.         BufferedReader reader=null;
  148.         PrintWriter writer=null;
  149.         try
  150.         {
  151.         while((line=anazitisi_2()).equals(null))
  152.         {
  153.            
  154.         }
  155.        
  156.         Scanner input=new Scanner(System.in);
  157.          reader=new BufferedReader(new FileReader(line));
  158.         System.out.println("Plhktrologhse to onoma kai th kataliksi tou neou arxeiou");
  159.         writer=new PrintWriter(new FileWriter(input.nextLine()));
  160.         while((line=reader.readLine())!=null)
  161.         {
  162.             writer.println(line);
  163.         }
  164.         return 1;
  165.         }
  166.         catch(IOException e)
  167.         {
  168.             System.err.println(e.getMessage());
  169.             return i;
  170.         }
  171.         finally
  172.         {
  173.             if(reader!=null)
  174.             {
  175.                 reader.close();
  176.             }
  177.             if(writer!=null)
  178.             {
  179.                 writer.close();
  180.             }
  181.            
  182.         }
  183.     }
  184.        
  185.        
  186.    
  187.  
  188.  
  189.     private String anazitisi_2() throws IOException
  190.     {
  191.        
  192.         Scanner input=new Scanner(System.in);
  193.         System.out.println("Plhktrologhse to onoma tou arxeiou me thn kataliksi");
  194.         String filename=input.nextLine();
  195.         File f=new File(filename);
  196.        
  197.         if(!f.exists())
  198.         {
  199.             System.err.println("File does not exist");
  200.             System.exit(1);
  201.         }
  202.         if(!f.canWrite())
  203.         {
  204.             throw new IllegalArgumentException("Write protected"+f);
  205.         }
  206.         if(!f.isFile())
  207.         {
  208.             throw new IOException("It's not a file.");
  209.         }
  210.         if(!f.canRead())
  211.         {
  212.             throw new IllegalArgumentException("Cannot read the file"+f);
  213.         }
  214.    
  215.         if(f.exists())
  216.         {
  217.             return filename;
  218.         }
  219.         return null;
  220.        
  221.     }
  222.        
  223.    
  224.  
  225.  
  226.  
  227.     private int dimiourgia_arxeiou() throws IOException
  228.     {
  229.         Scanner input=new Scanner(System.in);
  230.         System.out.println("Plhktrologhse to onoma tou arxeiou me kataliksi gia dhmiourgia");
  231.         String filename=input.nextLine();
  232.         File f=new File(filename);
  233.         if(!f.exists())
  234.         {
  235.             f.createNewFile();
  236.             System.out.println("New file"+filename+" has been created");
  237.         }
  238.         else
  239.         {
  240.             System.out.println("The file already exists");
  241.            
  242.         }
  243.         System.out.println("Gia eksodo pata 0.Alliws pata 1");
  244.         int i=input.nextInt();
  245.         if(i==0)
  246.         {
  247.             System.exit(0);
  248.         }
  249.         return i;
  250.     }
  251.  
  252.  
  253.  
  254.     private void diagrafh_arxeiou() throws IOException
  255.     {
  256.         Scanner input=new Scanner(System.in);
  257.         String line;
  258.         while((line=anazitisi_2()).equals("null"))
  259.         {
  260.            
  261.         }
  262.         File f=new File(line);
  263.         if(!f.canWrite())
  264.         {
  265.             throw new IllegalArgumentException("The file is write protected!");
  266.         }
  267.         boolean success=f.delete();
  268.         if(!success)
  269.         {
  270.             throw new IllegalArgumentException("Deletion failed");
  271.         }
  272.         if(success)
  273.         {
  274.             System.out.println("Deletion completed!");
  275.         }
  276.        
  277.     }
  278.  
  279.  
  280.  
  281.     private void submenu(String filename) throws IOException
  282.     {
  283.         Scanner input=new Scanner(System.in);
  284.         System.out.println("Plhktrologhse: 1:Eisagwgi vivliwn");
  285.         System.out.println("2:Diagrafh vivliwn");
  286.         System.out.println("3:Enimerwsi vivliwn");
  287.         System.out.println("4:Eksodos");
  288.         int epilogi=input.nextInt();
  289.         if(epilogi==1)
  290.         {
  291.             eisagwgi(filename);
  292.         }
  293.         else if(epilogi==2)
  294.         {
  295.             diagrafh_vivliwn(filename);
  296.         }
  297.         else if(epilogi==3)
  298.         {
  299.             enimerwsi(filename);
  300.         }
  301.         else if(epilogi==4)
  302.         {
  303.             return;
  304.         }
  305.         else
  306.         {
  307.             while(epilogi<1&&epilogi>4)
  308.             {
  309.                 System.out.println("Plhktrologhse: 1:Eisagwgi vivliwn");
  310.                 System.out.println("2:Diagrafh vivliwn");
  311.                 System.out.println("3:Enimerwsi vivliwn");
  312.                 System.out.println("4:Eksodos");
  313.                 epilogi=input.nextInt();
  314.             }
  315.         }
  316.         retry();
  317.         submenu(filename);
  318.     }
  319.    
  320.    
  321.  
  322.  
  323.  
  324.    
  325.  
  326.  
  327.  
  328.    
  329.  
  330.  
  331.  
  332.     private void enimerwsi(String filename) {
  333.         // TODO Auto-generated method stub
  334.        
  335.     }
  336.  
  337.  
  338.  
  339.     private void diagrafh_vivliwn(String filename) throws IOException
  340.     {
  341.         Scanner input=new Scanner(System.in);
  342.         ArrayList<String> books=new ArrayList<String>();
  343.         System.out.println("Plhtkrologhse tous titlous twn vivliwn gia diagrafh.Gia eksodo pata #");
  344.         int i=0;
  345.         books.add(i,input.nextLine());
  346.         while(!books.contains("#"))
  347.         {
  348.             i++;
  349.             System.out.println("Plhtkrologhse tous titlous twn vivliwn gia diagrafh.Gia eksodo pata #");
  350.             books.add(i,input.nextLine());
  351.         }
  352.        
  353.         BufferedReader reader=null;
  354.         PrintWriter writer=null;
  355.         String line;
  356.         manager book=new manager();
  357.         File from=new File(filename);
  358.         File to=new File("mytemp123.txt");
  359.        
  360.         try
  361.         {
  362.        
  363.         reader=new BufferedReader(new FileReader(from));
  364.         writer=new PrintWriter(new FileWriter(to));
  365.         while((line=reader.readLine())!=null)
  366.         {
  367.             if(books.contains(book.getTitlos()))
  368.             {
  369.                 continue;
  370.             }
  371.             writer.println(line);
  372.         }
  373.         }
  374.         finally
  375.         {
  376.             if(reader!=null)
  377.             {
  378.                 reader.close();
  379.             }
  380.             if(writer!=null)
  381.             {
  382.                 writer.close();
  383.             }
  384.         }
  385.         to.renameTo(from);
  386.     }
  387.  
  388.  
  389.  
  390.     private void eisagwgi(String filename) throws IOException
  391.     {
  392.        
  393.         PrintWriter writer=null;
  394.         manager book=new manager();
  395.         writer=new PrintWriter(new FileWriter(filename,true));
  396.         while(eggrafes()!=0)
  397.         {
  398.             writer.println(book.toString());
  399.         }
  400.         writer.close();
  401.        
  402.     }
  403.        
  404.  
  405.     public int eggrafes()
  406.     {
  407.         System.out.println("Dose titlo,etos eksodhs,siggrafea,thematikh perioxh,timi kai temaxia.Gia eksodo pata 0");
  408.         Scanner input=new Scanner(System.in);
  409.         setTitlos(input.nextLine());
  410.         setYear(input.nextLine());
  411.         setSiggrafeas(input.nextLine());
  412.         setThema(input.nextLine());
  413.         setTimi(input.nextDouble());
  414.         setTemaxia(input.nextInt());
  415.         if(getTitlos().equals("0"))
  416.         {
  417.             return 0;
  418.         }
  419.         else if(getYear().equals("0"))
  420.         {
  421.             return 0;
  422.         }
  423.         else if(getSiggrafeas().equals("0"))
  424.         {
  425.             return 0;
  426.         }
  427.         else if(getThema().equals("0"))
  428.         {
  429.             return 0;
  430.         }
  431.         else if(getTimi()==0)
  432.         {
  433.             return 0;
  434.         }
  435.         else if(getTemaxia()==0)
  436.         {
  437.             return 0;
  438.         }
  439.         return 1;
  440.        
  441.        
  442.     }
  443.  
  444.     private int anazitisi_arxeiou() throws IOException
  445.     {
  446.         Scanner input=new Scanner(System.in);
  447.         int i=0;
  448.         System.out.println("Plhtkrologhse onoma arxeiou me kataliksi");
  449.         String filename=input.nextLine();
  450.         File f=new File(filename);
  451.         if(!f.exists())
  452.         {
  453.             throw new SecurityException("File does not exist");
  454.            
  455.         }
  456.         if(!f.isFile())
  457.         {
  458.             throw new IllegalArgumentException(filename+"is not a file");
  459.            
  460.         }
  461.         if(!f.canRead())
  462.         {
  463.             throw new IllegalArgumentException("Cannot read the file");
  464.         }
  465.         if(f.exists())
  466.         {
  467.             if(f.isFile())
  468.             {
  469.            
  470.                 i=1;
  471.                 submenu(filename);
  472.                 return i;
  473.             }
  474.         }
  475.         return i;
  476.     }
  477.  
  478.  
  479.  
  480.     public void retry()
  481.     {
  482.         System.out.println("An epi8umeite eksodo pathste 0.Gia na ksanaprospa8hsete pathste 1");
  483.         Scanner input=new Scanner(System.in);
  484.         int epilogi=input.nextInt();
  485.         while(epilogi!=0 && epilogi!=1)
  486.         {
  487.             System.out.println("An epi8umeite eksodo pathste 0.Gia na ksanaprospa8hsete pathste 1");
  488.             epilogi=input.nextInt();
  489.         }
  490.         if(epilogi==0)
  491.         {
  492.             System.exit(0);
  493.         }
  494.        
  495.     }
  496.  
  497.     public void setTitlos(String title)
  498.     {
  499.         titlos=title;
  500.     }
  501.     public String getTitlos()
  502.     {
  503.         return titlos;
  504.     }
  505.     public void setYear(String year)
  506.     {
  507.         this.year=year;
  508.     }
  509.     public String getYear()
  510.     {
  511.         return year;
  512.     }
  513.     public void setSiggrafeas(String writer)
  514.     {
  515.         siggrafeas=writer;
  516.     }
  517.     public String getSiggrafeas()
  518.     {
  519.         return siggrafeas;
  520.     }
  521.     public void setThema(String subject)
  522.     {
  523.         thema=subject;
  524.     }
  525.     public String getThema()
  526.     {
  527.         return thema;
  528.     }
  529.     public void setTimi(double price)
  530.     {
  531.         timi=price;
  532.     }
  533.     public double getTimi()
  534.     {
  535.         return timi;
  536.     }
  537.     public void setTemaxia(int kommatia)
  538.     {
  539.         temaxia=kommatia;
  540.     }
  541.     public int getTemaxia()
  542.     {
  543.         return temaxia;
  544.     }
  545.     @Override
  546.     public String toString()
  547.     {
  548.         return String.format("%s %s %s %s %f %d",getTitlos(),getYear(),getSiggrafeas(),getThema(),getTimi(),getTemaxia());
  549.        
  550.     }
  551. }
Add Comment
Please, Sign In to add comment