Created
September 23, 2018 03:20
-
-
Save luchtech/f1e2b73e7d3ae8b3cc796358c3b7d99b to your computer and use it in GitHub Desktop.
Universal TextFile Manipulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class Database { | |
/* | |
* This Universal Database aims to perform all kinds of operations when it comes to 2D Arrays. | |
* Here are the list of functions so far: | |
* | |
* (b) keyChecker (boolean) --- checks existence of a "key" in the desired "header column" | |
* | |
* (c) isValid (boolean) [One SearchKey] --- checks existence of a "username" then checks the "password" | |
* (d) isValid (boolean) [Multiple Validations] --- checks existence of "something" then checks the other "keys" too | |
* | |
* (e) addAccount (void) [One ArrayList] --- adds a new account to the Text File | |
* (f) removeAccount (void) [One SearchKey] --- removes an account then archives it | |
* (g) updateAccount (void) [One SearchKey] --- edits details of an account | |
* | |
* (h) addCategory (void) [One Category] --- adds a new category to the Text File | |
* (i) removeCategory (void) [One Category] --- removes a category from the Text File | |
* (j) updateCategory (void) [One Category, One Update Value] --- edits the name of the category | |
* | |
* (k) getTableHeader (String[]) [] --- for JTable Header | |
* (l) getTableArray (String[][]) [Default] --- for JTable | |
* (m) getTableArray (String[][]) [One SearchKey] --- for JTable | |
* (n) getTableArray (String[][]) [Multiple SearchKeys] --- for JTable | |
*/ | |
private ArrayList<ArrayList<String>> mother, temp2D; | |
private ArrayList<String> temp, temp2, header; | |
private ArrayList<Integer> indexColumns; | |
private String fName, archiveName; | |
private String info[]; | |
private String hold; | |
private String tableArray[][], tableHeader[]; | |
private int length, width, index, index2; | |
private File f; | |
private FileReader fRead; | |
private FileWriter fWrite; | |
private Scanner scan; | |
private boolean match; | |
private Database archiveDB; | |
//Default Constructor | |
public Database() throws IOException { | |
fName = "Accounts.txt"; | |
archiveName = "Archive.txt"; | |
f = new File(fName); | |
if(!f.exists()) { //check first if the file is existing | |
f.createNewFile(); //if not, then create one | |
} | |
if(f.length() == 0) { | |
fWrite = new FileWriter(f); | |
fWrite.write("Username#Password#First Name#Last Name#\n"); | |
fWrite.close(); | |
} | |
} | |
//Constructor with File Name as Parameter | |
public Database(String fName) throws IOException { | |
this.fName = fName; | |
archiveName = "Archive.txt"; | |
f = new File(fName); | |
if(!f.exists()) { //check first if the file is existing | |
f.createNewFile(); //if not, then create one | |
} | |
if(f.length() == 0) { | |
fWrite = new FileWriter(f); | |
fWrite.write("Username#Password#First Name#Last Name#\n"); | |
fWrite.close(); | |
} | |
} | |
//Constructor with File Name as Parameter | |
public Database(String fName, String archiveName) throws IOException { | |
this.fName = fName; | |
this.archiveName = archiveName; | |
f = new File(fName); | |
if(!f.exists()) { //check first if the file is existing | |
f.createNewFile(); //if not, then create one | |
} | |
if(f.length() == 0) { | |
fWrite = new FileWriter(f); | |
fWrite.write("Username#Password#First Name#Last Name#\n"); | |
fWrite.close(); | |
} | |
} | |
//Constructor with File Name as Parameter | |
public Database(String fName, ArrayList<String> header) throws IOException { | |
this.fName = fName; | |
archiveName = "Archive.txt"; | |
f = new File(fName); | |
if(!f.exists()) { //check first if the file is existing | |
f.createNewFile(); //if not, then create one | |
} | |
if(f.length() == 0) { | |
hold = ""; | |
for (int i = 0; i < header.size(); i++) { | |
hold+=header.get(i)+"#"; | |
} | |
fWrite = new FileWriter(f); | |
fWrite.write(hold+"\n"); | |
fWrite.close(); | |
} | |
} | |
//Method to Load All Data from Text File to 2D Array List (mother) | |
public void openFile() throws IOException { | |
//Instantiate the Mother ArrayList as 2D ArrayList | |
header = new ArrayList<String>(); | |
mother = new ArrayList<ArrayList<String>>(); | |
//Instantiate FileReader and Scanner | |
fRead = new FileReader(f); | |
scan = new Scanner(fRead); | |
//Create Loop to Insert Data from Text File to Mother ArrayList | |
info = scan.nextLine().split("#"); | |
for (int i = 0; i < info.length; i++) { | |
header.add(info[i]); | |
mother.add(new ArrayList<String>()); | |
} | |
while(scan.hasNext()) { | |
info = scan.nextLine().split("#"); | |
for (int i = 0; i < info.length; i++) { | |
mother.get(i).add(info[i]); | |
} | |
} | |
//Close File Reader and Scanner | |
fRead.close(); scan.close(); | |
} | |
//Method to Save All Data to Text File from 2D Array List (mother) | |
public void saveFile() throws IOException { | |
//Instantiate File Writer and Clear Hold Variable | |
fWrite = new FileWriter(f); | |
hold = ""; | |
//Get the Length of Mother ArrayList and the Length of One Child ArrayList of Mother | |
length = mother.size(); | |
width = mother.get(0).size(); | |
//Create Loop to Insert All Data from Mother List to Text File | |
for (int i = 0; i < header.size(); i++) { | |
hold+=header.get(i)+"#"; | |
} | |
hold+="\n"; | |
for (int j = 0; j < mother.get(0).size(); j++) { | |
for (int i = 0; i < mother.size(); i++) { | |
hold+=mother.get(i).get(j)+"#"; | |
} | |
hold+="\n"; | |
} | |
//Write Hold to Text File then Close File Writer | |
fWrite.write(hold); | |
fWrite.close(); | |
} | |
//Method to Check Existence of a Search Key inside the Desired ArrayList | |
public boolean keyChecker(String searchKey, String searchHeader) throws IOException { | |
openFile(); //Instantiate Mother List before choosing One Child ArrayList | |
setTempArray(mother.get(Valid.isDuplicate(searchHeader, header))); //Reset Temp Array then Load all Data from Desired Child ArrayList | |
if(Valid.isDuplicate(searchKey, temp) != -1) return true; //Check Existence of the Search Key from the Desired Child ArrayList | |
return false; | |
} | |
//Method to Check if Username and Password Matches | |
public boolean isValid(String username, String password) throws IOException { | |
openFile(); | |
setTempArray(mother.get(Valid.isDuplicate("Username", header))); | |
setTemp2Array(mother.get(Valid.isDuplicate("Password", header))); | |
index = Valid.isDuplicate(username, temp); | |
if(index != -1) { | |
if(password.equals(temp2.get(index))) return true; | |
} | |
return false; | |
} | |
//Method for Multiple Validation | |
public boolean isValid(ArrayList<String> searchKeys, ArrayList<String> searchHeaders) throws IOException { | |
openFile(); | |
try { | |
setTempArray(mother.get(Valid.isDuplicate(searchHeaders.get(0), header))); | |
index = Valid.isDuplicate(searchKeys.get(0), temp); | |
if(index != -1) { | |
for (int i = 1; i < searchKeys.size(); i++) { | |
setTemp2Array(mother.get(Valid.isDuplicate(searchHeaders.get(i), header))); | |
if(!searchKeys.get(i).equals(temp2.get(index))) return false; | |
} | |
return true; | |
} | |
else return false; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
//Method to Add Account to Text File | |
public void addAccount(ArrayList<String> accDetails) throws IOException { | |
openFile(); | |
length = mother.size(); | |
for (int i = 0; i < length; i++) { | |
try { | |
mother.get(i).add(accDetails.get(i)); | |
} catch (Exception e) { | |
mother.get(i).add("null"); | |
} | |
} | |
saveFile(); | |
} | |
//Method to Add Account to Text File | |
public void removeAccount(String searchKey, String searchHeader) throws IOException { | |
openFile(); | |
try { | |
length = mother.size(); | |
} catch (Exception e) { | |
return; | |
} | |
do { | |
archiveDB = new Database(archiveName, header); | |
try { | |
setTempArray(mother.get(Valid.isDuplicate(searchHeader, header))); | |
temp2 = new ArrayList<String>(); | |
index = Valid.isDuplicate(searchKey, temp); | |
if(index != -1) { | |
for (int i = 0; i < length; i++) { | |
temp2.add(mother.get(i).get(index)); | |
mother.get(i).remove(index); | |
} | |
archiveDB.addAccount(temp2); | |
} | |
} catch (Exception e) { | |
return; | |
} | |
} while (index != -1); | |
saveFile(); | |
} | |
//Method to Add Account to Text File | |
public void updateAccount(String searchKey, String updateValue, String searchHeader) throws IOException { | |
openFile(); | |
try { | |
length = mother.size(); | |
} catch (Exception e) { | |
return; | |
} | |
do { | |
try { | |
// temp2 = new ArrayList<String>(); | |
index = Valid.isDuplicate(searchHeader, header); | |
setTempArray(mother.get(index)); | |
index2 = Valid.isDuplicate(searchKey, temp); | |
if(index != -1 && index2 != -1) { | |
// for (int i = 0; i < length; i++) { | |
// temp2.add(mother.get(i).get(index)); | |
// } | |
mother.get(index).set(index2, updateValue); | |
} | |
} catch (Exception e) { | |
return; | |
} | |
} while (index2 != -1); | |
saveFile(); | |
} | |
//Method to Add Account to Text File | |
public void addCategory(String category) throws IOException { | |
openFile(); | |
length = mother.size(); | |
if(length == 0) { | |
for (int i = 0; i < header.size(); i++) { | |
mother.add(new ArrayList<String>()); | |
} | |
length = mother.size(); | |
} | |
//Add New Category | |
if(Valid.isDuplicate(category, header) == -1) { | |
header.add(category); | |
mother.add(new ArrayList<String>()); | |
for (int i = 0; i < mother.get(0).size(); i++) { | |
mother.get(header.size()-1).add("null"); | |
} | |
saveFile(); | |
archiveDB = new Database(archiveName, header); | |
archiveDB.addCategory(category); | |
} | |
else return; | |
} | |
//Method to Add Account to Text File | |
public void removeCategory(String category) throws IOException { | |
openFile(); | |
length = mother.size(); | |
if(length == 0) { | |
for (int i = 0; i < header.size(); i++) { | |
mother.add(new ArrayList<String>()); | |
} | |
length = mother.size(); | |
} | |
//Add New Category | |
index = Valid.isDuplicate(category, header); | |
if(index != -1) { | |
header.remove(index); | |
mother.remove(index); | |
saveFile(); | |
archiveDB = new Database(archiveName, header); | |
archiveDB.removeCategory(category); | |
} | |
else return; | |
} | |
//Method to Add Account to Text File | |
public void updateCategory(String category, String updateValue) throws IOException { | |
openFile(); | |
length = mother.size(); | |
if(length == 0) { | |
for (int i = 0; i < header.size(); i++) { | |
mother.add(new ArrayList<String>()); | |
} | |
length = mother.size(); | |
} | |
//Add New Category | |
index = Valid.isDuplicate(category, header); | |
if(index != -1) { | |
header.set(index, updateValue); | |
saveFile(); | |
archiveDB = new Database(archiveName, header); | |
archiveDB.updateCategory(category, updateValue); | |
} | |
else return; | |
} | |
//Method to Get JTable Header | |
public String[] getTableHeader() { | |
if(header.size() == 0) { | |
tableHeader = new String[1]; | |
tableHeader[0] = "Nothing to show."; | |
} | |
else { | |
tableHeader = new String[header.size()]; | |
for (int i = 0; i < tableHeader.length; i++) { | |
tableHeader[i] = header.get(i); | |
} | |
} | |
return tableHeader; | |
} | |
//Method to Get 2D Array for JTable Without Search Key | |
public String[][] getTableArray() throws IOException { | |
openFile(); | |
length = mother.size(); | |
width = mother.get(0).size(); | |
if(width == 0) { | |
tableArray = new String[1][getTableHeader().length]; | |
tableArray[0][0] = "Nothing to show."; | |
} | |
else { | |
tableArray = new String[width][length]; | |
for (int i = 0; i < width; i++) { | |
for (int j = 0; j < length; j++) { | |
tableArray[i][j] = mother.get(j).get(i); | |
} | |
} | |
} | |
return tableArray; | |
} | |
//Method to Get 2D Array for JTable With Search Key | |
public String[][] getTableArray(String searchKey, String searchHeader) throws IOException { | |
openFile(); | |
length = mother.size(); | |
try { | |
setTempArray(mother.get(Valid.isDuplicate(searchHeader, header))); | |
width = Valid.dupCounter(searchKey, temp); | |
} catch (Exception e) { | |
width = 0; | |
} | |
if(width == 0) { | |
tableArray = new String[1][getTableHeader().length]; | |
tableArray[0][0] = "Nothing to show."; | |
} | |
else{ | |
tableArray = new String[width][length]; | |
for (int i = 0, j = 0; j < width; i++) { | |
if(temp.get(i).equalsIgnoreCase(searchKey)) { | |
for (int k = 0; k < length; k++) { | |
tableArray[j][k] = mother.get(k).get(i); | |
} | |
j++; | |
} | |
} | |
} | |
return tableArray; | |
} | |
//Method to Get 2D Array for JTable With Multiple Search Keys | |
public String[][] getTableArray(ArrayList<String> searchKeys, ArrayList<String> searchHeaders) throws IOException { | |
openFile(); | |
indexColumns = new ArrayList<Integer>(); | |
length = mother.size(); | |
width = mother.get(0).size(); | |
if(width == 0) { | |
tableArray = new String[1][getTableHeader().length]; | |
tableArray[0][0] = "Nothing to show."; | |
} | |
else { | |
temp2D = new ArrayList<ArrayList<String>>(); | |
for (int i = 0; i < searchHeaders.size(); i++) { | |
indexColumns.add(Valid.isDuplicate(searchHeaders.get(i), header)); | |
} | |
for (int i = 0; i < width; i++) { | |
match = true; | |
for (int j = 0; j < indexColumns.size(); j++) { | |
if(!mother.get(indexColumns.get(j)).get(i).equalsIgnoreCase(searchKeys.get(j))) { | |
match = false; | |
break; | |
} | |
} | |
if(match) { | |
temp = new ArrayList<String>(); | |
for (int j = 0; j < mother.size(); j++) { | |
temp.add(mother.get(j).get(i)); | |
} | |
temp2D.add(temp); | |
} | |
} | |
try { | |
tableArray = new String[temp2D.size()][temp2D.get(0).size()]; | |
for (int i = 0; i < tableArray.length; i++) { | |
for (int j = 0; j < tableArray[0].length; j++) { | |
tableArray[i][j] = temp2D.get(i).get(j); | |
} | |
} | |
} catch (Exception e) { | |
tableArray = new String[1][getTableHeader().length]; | |
tableArray[0][0] = "Nothing to show."; | |
} | |
} | |
return tableArray; | |
} | |
// Load ArrayList to Temporary Array 1 | |
public void setTempArray(ArrayList<String> arr) { | |
temp = new ArrayList<String>(); | |
for (String s : arr) { | |
temp.add(s); | |
} | |
} | |
// Load ArrayList to Temporary Array 2 | |
public void setTemp2Array(ArrayList<String> arr) { | |
temp2 = new ArrayList<String>(); | |
for (String s : arr) { | |
temp2.add(s); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.Period; | |
import java.time.ZoneId; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.Vector; | |
public class Valid { | |
/* | |
* This user-defined class is made by James Carlo Luchavez. | |
* This class is composed of the following Data Validating Methods: | |
* (a) "isDuplicate" returns the index of the given data from the array - Vector, ArrayList, Normal Array | |
* (b) "removeExcessSpace" returns a lowercase string with only one space from each word or element | |
* (c) "firstLetterUp" returns a string with first letters of each word capitalized (for names) | |
* (d) "toInteger" converts a string value to integer | |
* (e) "toDouble" converts a string value to double | |
* (f) "" | |
*/ | |
// Vector Array isDuplicate | |
static int isDuplicate(String s, Vector<String> arr) { // String Array | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(int s, Vector<Integer> arr) { // Integer Array | |
for(int x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(double s, Vector<Double> arr) { // Double Array | |
for(double x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(char s, Vector<Character> arr) { // Character Array | |
for(char x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
// ArrayList isDuplicate | |
static int isDuplicate(String s, ArrayList<String> arr) { // String Array | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(int s, ArrayList<Integer> arr) { // Integer Array | |
for(int x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(double s, ArrayList<Double> arr) { // Double Array | |
for(double x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
static int isDuplicate(char s, ArrayList<Character> arr) { // Character Array | |
for(char x: arr) { | |
if(x == s) { | |
return arr.indexOf(s); | |
} | |
} | |
return -1; | |
} | |
// Normal Array isDuplicate | |
static int isDuplicate(String s, String arr[]) { // String Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i].equals(s)) | |
return i; | |
} | |
return -1; | |
} | |
static int isDuplicate(int s, int arr[]) { // Integer Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i] == s) | |
return i; | |
} | |
return -1; | |
} | |
static int isDuplicate(double s, double arr[]) { // Double Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i] == s) | |
return i; | |
} | |
return -1; | |
} | |
static int isDuplicate(char s, char arr[]) { // Character Array | |
for (int i = 0; i < arr.length; i++) { | |
if(arr[i] == s) | |
return i; | |
} | |
return -1; | |
} | |
// Vector Duplicate Counter | |
static int dupCounter(String s, Vector<String> arr) { // String Array | |
int count = 0; | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) count++; | |
} | |
return count; | |
} | |
static int dupCounter(int s, Vector<Integer> arr) { // Integer Array | |
int count = 0; | |
for(int x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
static int dupCounter(double s, Vector<Double> arr) { // Double Array | |
int count = 0; | |
for(double x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
static int dupCounter(char s, Vector<Character> arr) { // Character Array | |
int count = 0; | |
for(char x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
// ArrayList Duplicate Counter | |
static int dupCounter(String s, ArrayList<String> arr) { // String Array | |
int count = 0; | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) count++; | |
} | |
System.out.println("Length: "+count); | |
return count; | |
} | |
static int dupCounter(int s, ArrayList<Integer> arr) { // Integer Array | |
int count = 0; | |
for(int x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
static int dupCounter(double s, ArrayList<Double> arr) { // Double Array | |
int count = 0; | |
for(double x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
static int dupCounter(char s, ArrayList<Character> arr) { // Character Array | |
int count = 0; | |
for(char x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
// Normal Array isDuplicate | |
static int dupCounter(String s, String[] arr) { // String Array | |
int count = 0; | |
for(String x: arr) { | |
if(x.equalsIgnoreCase(s)) count++; | |
} | |
return count; | |
} | |
static int dupCounter(int s, int[] arr) { // Integer Array | |
int count = 0; | |
for(int x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
static int dupCounter(double s, double[] arr) { // Double Array | |
int count = 0; | |
for(double x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
static int dupCounter(char s, char[] arr) { // Character Array | |
int count = 0; | |
for(char x: arr) { | |
if(x == s) count++; | |
} | |
return count; | |
} | |
// Excess Whitespace Remover | |
static String removeExcessSpace(String s){ | |
if(s.equals("")) return s; | |
if(allSpace(s)) return ""; | |
String hold = ""; | |
char c; | |
for (int i = 0; i < s.length(); i++) { // REMOVES INAPPROPRIATE WHITESPACES | |
c = s.charAt(i); | |
if(Character.isLetter(c)) { | |
hold+=c; | |
} | |
else if(Character.isWhitespace(c) && hold.length() > 0 && !Character.isWhitespace(hold.charAt(hold.length()-1))) { | |
hold+=" "; | |
} | |
} | |
return hold; | |
} | |
// First Letter to Uppercase | |
static String firstLetterUp(String s) { | |
s = removeExcessSpace(s); | |
if(s.equals("")) return s; | |
String hold = ""; | |
String[] arr = s.split(" "); | |
for (int i = 0; i < arr.length; i++) { // CONVERTS FIRST LETTER FOR EACH WORD | |
arr[i] = arr[i].toUpperCase().charAt(0)+arr[i].toLowerCase().substring(1); | |
hold+=arr[i]; | |
if(i != arr.length-1) { | |
hold+=" "; | |
} | |
} | |
return hold; | |
} | |
// String to Integer | |
static int toInteger(String s) { | |
if(s == null) return -1; | |
String hold = ""; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(Character.isDigit(c)) { | |
hold+=c; | |
} | |
if(c == '-' && hold.equals("")){ | |
hold+=c; | |
} | |
} | |
if(hold.equals("")) return 0; | |
else return Integer.parseInt(hold); | |
} | |
// String to Double | |
static Double toDouble(String s) { | |
if(s == null) return (double) -1; | |
String hold = ""; | |
int ctr = 0; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(Character.isDigit(c)) { | |
hold+=c; | |
} | |
else if(c == '-' && hold.equals("")) { | |
hold+=c; | |
} | |
else if(c == '.' && ctr != 1) { | |
hold+=c; | |
ctr++; | |
} | |
} | |
if(hold.equals("")) return 0.0; | |
else return Double.parseDouble(hold); | |
} | |
// Checks if a string contains only whitespaces | |
static boolean allSpace(String s) { | |
if(s == null) return true; | |
char c; | |
for(int i = 0; i < s.length(); i++) { | |
c = s.charAt(i); | |
if(!Character.isWhitespace(c)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// Computes for Age | |
static int ageCalc(Date date) { | |
return Period.between(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), LocalDate.now()).getYears(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment