Advertisement
Osher15151

dsfgdfgdfg

Jul 11th, 2020
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1. package com.FinalProject.view;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.PrintWriter;
  10. import java.time.LocalDate;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14. import com.FinalProject.view.ReportPanel;
  15.  
  16. import javax.swing.*;
  17.  
  18. import com.FinalProject.model.Worker;
  19. import com.FinalProject.model.Task;
  20.  
  21.  
  22.  
  23. public class AddTaskPanel extends JFrame implements ActionListener{
  24.     JFrame frame;
  25.     String str = "";
  26.     Task task1;
  27.     JLabel lblDes, lblDuration,l1,lblwname;
  28.     JTextField tfDes, tfDur,tfwname;
  29.     JButton btnsubmit, clearbtn,btnback,btnadd,reportbtn;  
  30.     List <File> files = new ArrayList <File>();
  31.     ReportPanel p = new ReportPanel() ;
  32.    
  33.     public AddTaskPanel()
  34.     {
  35.        
  36.         setVisible(true);  
  37.         setSize(600, 300);  
  38.         setLayout(null);  
  39.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  40.         setTitle("Add Task Form");
  41.         l1 = new JLabel("Add Task Form - Type Details Below:");  
  42.         l1.setForeground(Color.blue);  
  43.         l1.setFont(new Font("Serif", Font.BOLD, 20));  
  44.         lblDes = new JLabel("Add Task:");    
  45.         lblDuration = new JLabel("Task Duration:");  
  46.         lblwname = new JLabel("Worker Name: ");
  47.         p.setVisible(false);
  48.        
  49.         tfDes = new JTextField();  
  50.         tfDur = new JTextField();
  51.         tfwname = new JTextField();
  52.        
  53.        
  54.         btnsubmit = new JButton("Submit");  
  55.         clearbtn = new JButton("Clear");  
  56.         btnback = new JButton("Back");  
  57.         btnadd = new JButton("Add");
  58.         reportbtn = new JButton("Report");
  59.         btnsubmit.addActionListener(this);  
  60.         clearbtn.addActionListener(this);  
  61.         btnback.addActionListener(this);
  62.         btnadd.addActionListener(this);
  63.         reportbtn.addActionListener(this);
  64.         lblDes.setBounds(100, 70, 400, 30);  
  65.         lblDuration.setBounds(100, 110, 200, 30);
  66.         lblwname.setBounds(100, 150, 200, 30);
  67.         l1.setBounds(100, 30, 400, 30);  
  68.        
  69.         tfDes.setBounds(300, 70, 200, 30);  
  70.         tfDur.setBounds(300, 110, 200, 30);
  71.         tfwname.setBounds(300, 150, 200, 30);
  72.        
  73.         btnsubmit.setBounds(230, 230, 80, 30);
  74.         clearbtn.setBounds(360, 230, 80, 30);  
  75.         btnback.setBounds(20, 230, 80, 30);
  76.         btnadd.setBounds(130, 230, 80, 30);
  77.         reportbtn.setBounds(500, 230, 80, 30);
  78.        
  79.         add(lblDes);  
  80.         add(lblDuration);
  81.         add(tfwname);
  82.         add(l1);
  83.         add(tfDur);  
  84.         add(tfDes);  
  85.         add(lblwname);  
  86.        
  87.        
  88.         add(btnsubmit);  
  89.         add(clearbtn);
  90.         add(btnback);
  91.         add(btnadd);
  92.         add(reportbtn);
  93.        
  94.     }
  95.    
  96. public static void main(String[] args) {
  97.        
  98.         new AddTaskPanel();
  99.        
  100.  
  101. }
  102.     @Override
  103.     public void actionPerformed(ActionEvent e) {
  104.  
  105.         if(e.getSource().equals(btnsubmit)) {
  106.  
  107.            if(tfwname.getText().isEmpty() && tfDes.getText().isEmpty()&& tfDur.getText().isEmpty())
  108.                JOptionPane.showMessageDialog(frame, "Please fill the form");
  109.            
  110.            else {
  111.                File f = new File(tfwname.getText() + ".txt");
  112.                files.add(f);
  113.                try {
  114.                    PrintWriter pw = new PrintWriter(f);
  115.                   // if (f.length() == 0)
  116.                    pw.println("Worker Name: " +tfwname.getText());
  117.                    pw.println(str);
  118.                    pw.close();
  119.                    str="";
  120.                } catch (FileNotFoundException e1) {
  121.                    // TODO Auto-generated catch block
  122.                    e1.printStackTrace();
  123.                }  
  124.            }
  125.        }
  126.         else if(e.getSource().equals(btnadd)) {
  127.  
  128.             if(tfwname.getText().isEmpty() && tfDes.getText().isEmpty()&& tfDur.getText().isEmpty())
  129.                JOptionPane.showMessageDialog(frame, "Please fill the form");
  130.             else {
  131.              str += "Task: " + tfDes.getText() + "    Duration: "+ tfDur.getText()+"\n";
  132.              System.out.println(str);            
  133.             }
  134.         }
  135.      
  136.      
  137.        
  138.         else if(e.getSource().equals(clearbtn))
  139.         {
  140.             tfDur.setText("");  
  141.             tfDes.setText("");  
  142.             tfwname.setText("");
  143.  
  144.         }
  145.        
  146.         else if(e.getSource().equals(btnback))
  147.         {
  148.             //MenuPanel mp = new MenuPanel();
  149.             this.setVisible(false);
  150.             //mp.setVisible(true);
  151.         }
  152.        
  153.         else if(e.getSource().equals(reportbtn))
  154.         {
  155.             if(files.size()==0)
  156.                 JOptionPane.showMessageDialog(frame, "Please add tasks first");
  157.             else {
  158.             File f = new File("Report.txt");
  159.             String data="";
  160.                for (File file : files) {
  161.                    
  162.                    try {
  163.                           File myObj = new File(file.getName());
  164.                           Scanner myReader = new Scanner(file);
  165.                           while (myReader.hasNextLine()) {
  166.                              data += myReader.nextLine()+" \n";
  167.                             //System.out.println(data);
  168.                           }
  169.                           myReader.close();
  170.                         } catch (FileNotFoundException e1) {
  171.                           e1.printStackTrace();
  172.                    
  173.                           data+="\n";
  174.  
  175.                }
  176.                    System.out.println(data);
  177.                  
  178.                }
  179.                try {
  180.                    PrintWriter pw = new PrintWriter(f);
  181.                   // if (f.length() == 0)
  182.                    pw.println("Report\n");
  183.                    pw.println(data);
  184.                    pw.close();
  185.                    p.ta.setText(data);
  186.                    this.setVisible(false);
  187.                    p.setVisible(true);
  188.                    
  189.                    data="";
  190.                } catch (FileNotFoundException e1) {
  191.                    // TODO Auto-generated catch block
  192.                    e1.printStackTrace();
  193.         }
  194.             }
  195.        
  196.     }
  197.    
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement