Categories
Available Project

Accounting Cheat Code

I challenged myself today to make something in 4 hours to be able to use at work to make moving files easier. I was told that more than likely it would not be used because of it needing approval. I decided that even with no approval I would just make something that made me practice some sort of coding.

The rules I set forth to try to make this get accepted/approved:
1. No 3rd party Libraries
2. Have it open source
3. Only using variable storage
4. Make it so that someone else could use it

I rushed this together in 4 hours as a challenge. Code I completed in 4 hours I would rate a 4/10 because I could have made it easier to read and more object oriented, but I feel like making it more object oriented should only be done after you get it working. I by the way definitely didn’t rate it for the meme of 4 hours 4 points. I had fun making this a goal to complete. Anyways here is the code below, followed by the compiled download.

package tsj9834;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

import javax.swing.JFileChooser;

/**
 *
 * @author Travis Jernigan
 * 
 * This was made in 4 hours. Improvements and cleanup is needed!
 * This is so much better and quicker for moving files than manually doing it.
 * 
 */
@SuppressWarnings("serial")
public class GUIBase extends javax.swing.JFrame {
   
    protected String mode = "contains";
    private File rootFolder;
    private File moveTarget;
    private File targetLocation;
    private ArrayList<File> sList = new ArrayList<File>();
    
    /**
     * Creates new form UI
     */
    public GUIBase() {
        initComponents();
    }

    /**
     * 
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jTextField2 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox<>();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox<>();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setSize(1200, 800);
        jTabbedPane1.setToolTipText("");

        //jLabel6.setText("Find Mode");
        jLabel6.setFocusable(false);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Contains", "Invoice Number", "Vendor Name" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jButton3.setText("Select Folder");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Move To Here");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Clear Settings");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Move");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel7.setText("Search This File Tree");
        jLabel7.setFocusable(false);

        jLabel8.setText("Unique Idenifier for File");
        jLabel8.setFocusable(false);

        jLabel9.setText("Additional Options");
        jLabel9.setFocusable(false);

        jButton7.setText("Black List Folder");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel10.setForeground(new java.awt.Color(255, 51, 51));
        jLabel10.setText("Nothing was Found!");
        jLabel10.setFocusable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel9))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jButton5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton6))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField2)
                                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel10))
                            .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(100, Short.MAX_VALUE))
        );

        jButton3.getAccessibleContext().setAccessibleName("selectFolder");
        jButton5.getAccessibleContext().setAccessibleName("clearSettings");
        jButton6.getAccessibleContext().setAccessibleName("move");
        jButton7.getAccessibleContext().setAccessibleName("ignoreFolder");
        jLabel10.getAccessibleContext().setAccessibleName("notFound");

        jTabbedPane1.addTab("Mover", jPanel2);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel2.setText("File Renamer");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("rename");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("File Picker");

        jLabel4.setText("Rename To");

        jButton2.setText("refresh");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextField1)
                            .addComponent(jComboBox1, 0, 101, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jButton1))))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(62, 62, 62)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jButton1))
                .addContainerGap(209, Short.MAX_VALUE))
        );
        //TODO: put this back in.
        //jTabbedPane1.addTab("Renamer", jPanel1);
        
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        jLabel1.setText("Made By: Travis");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jTabbedPane1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        jButton5.setVisible(false);
        jLabel10.setVisible(false);
        jComboBox1.setVisible(false);
        jComboBox2.setVisible(false);
        jButton4.setVisible(false);
        jButton7.setVisible(false);
        pack();
    }// </editor-fold>   

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {   
    	// TODO Move Function!
    	if(jTextField2.getText() != null && fileFound(jTextField2.getText())) {
    		if(jLabel10.isVisible()) {
    			jLabel10.setVisible(false);
    		}
    		try {
    			
				Files.move(Paths.get(moveTarget.getPath()), Paths.get(targetLocation.getPath() +"\\"+ moveTarget.getName()));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	
    }
    
    //TODO Add rename files and buttons!
    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        //mode = jComboBox2.getSelectedItem().toString().toLowerCase().replaceAll("\\s", "");
        //System.out.println(jComboBox2.getSelectedItem().toString().toLowerCase().replaceAll("\\s", ""));
    }                                          

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO Clear Settings!
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO rename button
    	
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO refresh button
    }                                        

    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        /** TODO select folder search
         *  Limited the search to 2 folders in. Should not need more than that for my use.
         *  Cleanup
    	*/
    	
    	try {
			JFileChooser chooser = new JFileChooser();
			if(rootFolder != null) {
				chooser.setCurrentDirectory(rootFolder);
			}else {
				chooser.setCurrentDirectory(new java.io.File("."));
			}
			 
			    chooser.setDialogTitle("Pick The Folder to start searching for the file you want.");
			    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			    chooser.setAcceptAllFileFilterUsed(false);
			    if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
			    	rootFolder = chooser.getSelectedFile();
			    	File[] directories = chooser.getSelectedFile().listFiles(File::isDirectory);
			    	//System.out.println(chooser.getSelectedFile());
			    	for(int i = 0; i < directories.length; i++) {
			    		sList.add(directories[i]);
			    		File[] secondDirectory = directories[i].listFiles(File::isDirectory);
			    		for(int j = 0; j < secondDirectory.length; j++) {
			    			sList.add(secondDirectory[j]);
			    		}
			    	}
			    }
			    jButton4.setVisible(true);
			    jButton7.setVisible(true);
			chooser.setSize(800, 400);
			chooser.setVisible(true);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO Cleanup!
    	try {
    		//Brings up the File Chooser UI that is built into java.
    		JFileChooser chooser = new JFileChooser();
    		chooser.setCurrentDirectory(rootFolder);
		    chooser.setDialogTitle("Moves the File Here!");
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    chooser.setAcceptAllFileFilterUsed(false);
		    
		    if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
		    	//System.out.println(chooser.getSelectedFile());
		    	targetLocation = chooser.getSelectedFile();
		    	
		    		chooser.setSize(800, 400);
		    		chooser.setVisible(true);
		    	}
		    } catch (Exception e1) {
		    	// TODO Auto-generated catch block
		    	e1.printStackTrace();
		    }
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO blacklist folder to exclude in search
    	
    	
    	try {
    		//Brings up the File Chooser UI that is built into java.
    		JFileChooser chooser = new JFileChooser();
    		chooser.setCurrentDirectory(rootFolder);
		    chooser.setDialogTitle("The selected folder will be excluded from any searches");
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    chooser.setAcceptAllFileFilterUsed(false);
		    
		    if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) {
		    	//System.out.println(chooser.getSelectedFile());
		    	if(sList.contains(chooser.getSelectedFile())) {
		    		//ArrayList simple to find and remove a 
		    		sList.remove(chooser.getSelectedFile());
		    	}else {
		    	
		    	}
		    		chooser.setSize(800, 400);
		    		chooser.setVisible(true);
		    	}
		    } catch (Exception e1) {
		    	// TODO Auto-generated catch block
		    	e1.printStackTrace();
		    }
    }
    
    
    private boolean fileFound(String name) {
    	
    	for(int i = 0; i < sList.size(); i++) {
    		File[] fileLists = sList.get(i).listFiles(File::isFile);
    		for(int j = 0; j < fileLists.length; j++) {
    			
    			if(fileLists[j].toString().contains(name)) {
    				moveTarget = fileLists[j];
    				return true;
    			}
    		}
    	}
    	jLabel10.setVisible(true);
		return false;
    	
    }
   
    //TODO: UI meaningful usage
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JComboBox<String> jComboBox1;
    private javax.swing.JComboBox<String> jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
                     
    public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIBase().setVisible(true);
            }
        });
	}

}

Leave a Reply