Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Budgetar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package projectzero.budgetar;

import java.io.File;
import java.nio.file.Path;
import java.util.Scanner;
import java.util.logging.Logger;


public class Budgetar {
public static void main(String[] args) {
Logger LOG = Logger.getAnonymousLogger();
LOG.info("Starting Application...");
LOG.info("Initiating server....");
File filename = new File("cogsFile.csv");
new Server().initialize(8080);
Path filepath = filename.toPath();
GetData a = new GetData();
a.myFile(); //check/create file.
a.FileContent();
Scanner entry = new Scanner(System.in);
Double tfa=0.00;
a.calcBalance();

int x;
String option = "";
do {

System.out.println("\nCurrent Budget:$" + a.tfa);
System.out.println("Total Est Cost: $" + a.sumCost);
System.out.println("Account Balance: $" + (a.tfa - a.sumCost));
System.out.println("\nSelect an Option"); //working
System.out.println("1:View List | 2:New List | 3:Edit Budget| 4:Edit Ratings | 0:Exit");


x = entry.nextInt();
switch (x) {
case 1:
option = "Current list";
System.out.println("Retrieving Shopping List.....");
a.FileContent().displayData();
break;

case 2:
option = "New List";
a.ShoppingList();
a.toFile();
break;
case 3:
option = "Edit Budget";
a.GetBudget();
break;
case 4:
option = "Index Items";
System.out.println(" Retrieving Items List...");
a.IndexList();
break;
case 0:
option = "Exit";
System.out.println("Exiting... Good Bye!");
return;
default:
option = "Invalid Entry";
System.out.println("Invalid Selection..");
break;
}
} while (true);
}
}
10 changes: 10 additions & 0 deletions Budgetar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
##Project : Revature P0
##Name: Budgetar Application
##Purpose
1. Budgeting Application
- Start with a set Budget amount,
- Create a list of items that they would like to purchase.
- Index the list of Items, by a Necessity Index value rated from 1-5.
- Local Persistence (File, .txt) or local SQL
- Calculate/ Create Shopping List Recommendation based on the data on hand.
-
51 changes: 51 additions & 0 deletions BudgetarFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package projectzero.budgetar;

import java.io.*;
import java.util.HashMap;

public class BudgetarFile extends GetData {
File cogFile = new File("FileCogs.csv");
String filepath;


String CreateDataFile(String filepath){
try {
if (!(cogFile.exists())) {
cogFile.createNewFile();
filepath = cogFile.getName();
System.out.println(" New File Created: " + filepath);
}
else if(cogFile.exists()) System.out.println("File: " + filepath + " Already Exists");
}catch (FileNotFoundException e){
System.out.println("File Not Found Error");
}catch ( IOException e){
System.out.println(" Error encountered creating new File");
}finally {
System.out.println("Will output the current values to File");
}
return filepath;
}


String[] ReadDataFile(String filepath){
String xFile=filepath;
String line;
int x = 0;
System.out.println("Retrieving Shopping List:");
try{
BufferedReader brRead = new BufferedReader(new FileReader(cogFile));
while ((line = brRead.readLine()) != null) {
System.out.println(x + ". " + line);
dataList = line.split(","); //Stores a list of Items to String Array.
//System.out.println(x + ". Item: " + dataList[0]);
x++;
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return dataList;
}

}
8 changes: 8 additions & 0 deletions BudgetarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package projectzero.budgetar;

import static org.junit.jupiter.api.Assertions.*;

class BudgetarTest {


}
Empty file added Build.sh
Empty file.
206 changes: 206 additions & 0 deletions GetData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package projectzero.budgetar;

import io.javalin.Javalin;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Stream;
import io.javalin.Javalin;
//import io.netty.buffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class GetData {
//public ArrayList httpList;
File filename = new File("cogsFile.csv");
Path filepath = filename.toPath();
Scanner entry = new Scanner(System.in);
HashMap cogs = new HashMap<String, ArrayList<Object>>();
int x = 0;
double tfa = 0.00; // Total Funds Available
double sumCost=0.00; //Total Cost of Goods
double cashBalance =0.00;
String[] dataList = new String[0];
ArrayList<String> itemList= new ArrayList<>(); //contains a list of all Items osn shopping List.
ArrayList<String> httpList= new ArrayList<>(); //contains a list of all Items osn shopping List.

//update the budget value
public void GetBudget() { //get the Total Budget Size
System.out.println(" Enter Total funds Available: $");
this.tfa = entry.nextDouble();
System.out.println(" TFA Wallet Updated:... $" + this.tfa );
return this;
}

//Crete anew Shopping list and discard the old one.
public void ShoppingList() {
StringBuilder sbWrite = new StringBuilder();
int x = 0;
System.out.println("Create a Shopping List");
ArrayList<String> itemList = this.itemList;
do {
String[] itemX = entry.nextLine().split("\\s+");
try {
if (!(itemX.equals(""))) { //if the scanner entry is NOT empty, continue, else catch exception
cogs.put(itemX[0], itemX[1]); // costX); //Store the data in a hashmap
}
x++;
} catch (Exception e) {
break;
}
} while (cogs.size() < 5 || entry.nextLine().equals("")); //Limits the item list to 10 ItemsSystem.out.println(br);
MapToArray(); //Convert Hashmap to Array List
System.out.println("Rate by Necessity Index (i.e Must have): 1-5 : ");
nvIndex(); //index the items
return this;
}

//will edit the items in the File.
public void IndexList() {
String line;
String tempList="";
System.out.println("Rate by Necessity Index (Must have): 1-5 : ");
try {
BufferedReader brRead = new BufferedReader(new FileReader(this.filename)); //opens up the read from File Function
while ((line = brRead.readLine()) != null) {
System.out.println(line.split(",")[0]);
int nvIndex = entry.nextInt();
itemList.add(line.split(",")[0] + "," + line.split(",")[1] + "," + nvIndex);
x++;
}

}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){

e.printStackTrace();
}
//System.out.println(itemList);
System.out.println("Update Values? Y/N");
String y = entry.nextLine();
if (y.toUpperCase(Locale.ROOT).equals("Y")) {
toFile();
}else {
System.out.println("Discarding New List....");
}
return this;
}

//Edits the Items on the itemList Array
public void nvIndex(){
for (int x=itemList.size()-1; x >= 0; x--) {
System.out.println(itemList.get(x));
itemList.add( itemList.get(x) + "," + entry.nextInt());
}
}

//Verifies True if file Exists, or will create a new file
public void myFile(){
filename = this.filename;
try {
if (!(filename.exists())) {
filename.createNewFile();
//filepath = filepath.getName();
System.out.println(" Error:... " + filename);
}
else if(filename.exists()) System.out.println("Success: " + filename);
}catch (FileNotFoundException e){
System.out.println("File Not Found Error:..." + filename);
}catch ( IOException e){
System.out.println(" Error encountered creating new File:...." + filename);
}finally {
System.out.println("");
}
return this;
}

//Writes Data to File
public GetData toFile() {
System.out.println("Save current List ?: (Y/N)");
final String y = entry.nextLine().toString();
String line ="";
HashMap data = this.cogs;
int x = 0;
try {
System.out.println("Writing to File");
BufferedWriter brWrite = new BufferedWriter(new FileWriter(filename)); //Opens up the Write to File function
for (String t : itemList) {
brWrite.write(t + "\n"); //Write the String to the File
x++;
}
brWrite.close(); //close the File after writing **/
} catch (FileNotFoundException e) {
//Error message out
} catch (IOException e) {
//Error Message out: use logger
}
return this;
}

//display the content of the .csv file
public GetData FileContent(){
//Iterator<String> iter = itemList.iterator();
try(Stream<String> lines = Files.lines(this.filepath)){
lines.forEach(s -> itemList.add(s));
}catch (IOException e){
System.err.println("Failed to stream File");
}
return this;
}

//Data Display Method
public GetData displayData(){
itemList.forEach(s -> System.out.println((s.split(",")[0] + ", $" + s.split(",")[1])));
return this;
}

//calculate trx Balance.
public void calcBalance(){
try {
sumCost = this.itemList.stream().mapToDouble(s -> Double.parseDouble(s.split(",")[1])).sum();
}catch (Exception e){
e.printStackTrace();
}
//System.out.println("Total Est Cost: $" + sumCost);
//System.out.println("Account Balance: $" + (cashBalance = this.tfa - sumCost));
}

//convert hashmap to arrayList
public void MapToArray() { // fucntion will convert current Hashmap to Array List
Iterator iCogs = cogs.entrySet().stream().iterator();;
this.itemList.clear();
while (iCogs.hasNext()) {
Map.Entry pairs = (Map.Entry)iCogs.next();
this. itemList.add(pairs.getKey() + "," + pairs.getValue() + ",");
}
this.itemList = itemList;
System.out.println(itemList);
}

//add to the List
public void addItemToList(){
System.out.println("Add: (Item,Cost,nvValue)");
String item = entry.nextLine();
this.itemList.add(item.split("")[0] + "," + item.split("")[1] + "," + item.split("")[2] + "\n");
System.out.println("Update File: Y/N");
if(entry.nextLine().toUpperCase(Locale.ROOT) == "Y"){
this.toFile();
}
}

//Remove from list
public void rmItemFromlist(){
System.out.println("Select Item to be Deleted");
String xItem = entry.nextLine();
this.itemList.remove(xItem);
}




}

43 changes: 43 additions & 0 deletions GetDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package projectzero.budgetar;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.junit.jupiter.api.Assertions.*;

class GetDataTest {

//will test the existence of a file
@Test
@DisplayName("Test for the existence of the file")
void myFileExists() {
File fileTest = new GetData().filepath.toFile();
Assertions.assertTrue(fileTest.exists());

}

@Test
@DisplayName("Test if the file is Read enabled")
void myFileRead(){
File fileTest = new GetData().filepath.toFile();
Assertions.assertTrue(fileTest.canRead());

}

@Test
@DisplayName("Test if the File is Write Enabled")
void myFileWrite(){
File fileTest = new GetData().filepath.toFile();
Assertions.assertTrue(fileTest.canWrite());
}

@Test
@DisplayName("Test if the Budget value is reset")
void myBudget(){
double amt = new GetData().tfa;
Assertions.assertEquals(0, amt);
}
}
Loading