diff --git a/Budgetar.java b/Budgetar.java new file mode 100644 index 0000000..34b0d53 --- /dev/null +++ b/Budgetar.java @@ -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); + } +} diff --git a/Budgetar.md b/Budgetar.md new file mode 100644 index 0000000..9da3f1c --- /dev/null +++ b/Budgetar.md @@ -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. +- \ No newline at end of file diff --git a/BudgetarFile.java b/BudgetarFile.java new file mode 100644 index 0000000..8b6e1db --- /dev/null +++ b/BudgetarFile.java @@ -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; + } + +} diff --git a/BudgetarTest.java b/BudgetarTest.java new file mode 100644 index 0000000..4d22251 --- /dev/null +++ b/BudgetarTest.java @@ -0,0 +1,8 @@ +package projectzero.budgetar; + +import static org.junit.jupiter.api.Assertions.*; + +class BudgetarTest { + + +} \ No newline at end of file diff --git a/Build.sh b/Build.sh new file mode 100644 index 0000000..e69de29 diff --git a/GetData.java b/GetData.java new file mode 100644 index 0000000..b84b801 --- /dev/null +++ b/GetData.java @@ -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>(); + 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 itemList= new ArrayList<>(); //contains a list of all Items osn shopping List. + ArrayList 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 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 iter = itemList.iterator(); + try(Stream 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); + } + + + + +} + diff --git a/GetDataTest.java b/GetDataTest.java new file mode 100644 index 0000000..74267e2 --- /dev/null +++ b/GetDataTest.java @@ -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); + } +} \ No newline at end of file diff --git a/Item.java b/Item.java new file mode 100644 index 0000000..b20f71e --- /dev/null +++ b/Item.java @@ -0,0 +1,9 @@ +package projectzero.budgetar; + + +class Item { + String name; + Double price; + int iValue; + +} diff --git a/ItemTest.java b/ItemTest.java new file mode 100644 index 0000000..4e8280a --- /dev/null +++ b/ItemTest.java @@ -0,0 +1,6 @@ +package projectzero.budgetar; + + +class ItemTest { + +} \ No newline at end of file diff --git a/LanguageServer.java b/LanguageServer.java new file mode 100644 index 0000000..df3dabb --- /dev/null +++ b/LanguageServer.java @@ -0,0 +1,12 @@ +import java.nio.file.Paths; + +public class LanguageServer { + public static void main(String[] args) { + System.out.println("Watching.."); + Paths watchTarget = Paths.get(args[0]).toAbsolutePath().normalize(); + + + } + + } +} \ No newline at end of file diff --git a/NettyServer.java b/NettyServer.java new file mode 100644 index 0000000..f000d77 --- /dev/null +++ b/NettyServer.java @@ -0,0 +1,61 @@ +package projectzero.budgetar; + +import io.netty.bootstrap.ServerBootstrap; + +import io.netty.channel.*; +import io.netty.channel.nio.NioEventLoopGroup; + import io.netty.channel.socket.SocketChannel; + import io.netty.channel.socket.nio.NioServerSocketChannel; + +import java.awt.*; + +/** + * Discards any incoming data. + */ +public class NettyServer { + + private int port=8080; + + public NettyServer(int port) { + this.port = port; + return; + } + + public void run() throws Exception { + EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) + EventLoopGroup workerGroup = new NioEventLoopGroup(); + try { + ServerBootstrap b = new ServerBootstrap(); // (2) + b.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) // (3) + .childHandler(new ChannelInitializer() { // (4) + @Override + public void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new ChannelInboundHandlerAdapter()); + } + }) + .option(ChannelOption.SO_BACKLOG, 128) // (5) + .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) + + // Bind and start to accept incoming connections. + ChannelFuture f = b.bind(port).sync(); // (7) + + // Wait until the server socket is closed. + // In this example, this does not happen, but you can do that to gracefully + // shut down your server. + f.channel().closeFuture().sync(); + } finally { + workerGroup.shutdownGracefully(); + bossGroup.shutdownGracefully(); + } + } + + public static void main(String[] args) throws Exception { + int port = 8080; + if (args.length > 0) { + port = Integer.parseInt(args[0]); + } + + new NettyServer(port).run(); + } +} \ No newline at end of file diff --git a/OptionList.java b/OptionList.java new file mode 100644 index 0000000..1c4d0f4 --- /dev/null +++ b/OptionList.java @@ -0,0 +1,43 @@ +package projectzero.budgetar; + +import java.util.*; + +public class OptionList { + + + + + + public static void main(String[] args) { + HashMap cogs = new HashMap<>(); + ArrayList itemList = new ArrayList(); + ArrayList itemCost = new ArrayList(); + Scanner entry = new Scanner(System.in); + cogs.put("Bread", 5.25); + cogs.put("Sugar ", 3.25); + cogs.put("Flour", 2.99); + cogs.put("Eggs", 4.50); + cogs.put("Butter", 3.15); + cogs.put("Milk", 4.25); + cogs.put("Vanilla", 1.25); + cogs.put("Coffee", 5.25); + cogs.put("Water", 1.50); + cogs.put("Sausage", 6.99); + + + Iterator iCogs = cogs.entrySet().stream().iterator(); + while (iCogs.hasNext()) { + Map.Entry pairs = (Map.Entry) iCogs.next(); + System.out.println(pairs.getKey() + ": $" + pairs.getValue()); + itemList.add(pairs.getKey() + ":$" + pairs.getValue()); + } + System.out.println(itemList); + + int x=0; + //while(itemList.stream().iterator().hasNext()) + + + System.out.println("Indexed List:" + itemList); + + } +} diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..6755bb8 --- /dev/null +++ b/Readme.md @@ -0,0 +1,32 @@ +#Revature Project Zero +## Budgetar tool +1. Purpose: + to create a shopping list application that can be Edited, and updated by the user, and issue some form of budgeting functionality i.e Cost Budgeting + +# Actors / Features: +## A User can: +1. List Items by Necessity +2. Add Items to the Shopping List. +3. Edit Item on the elist +4. Create new list +5. Update Total Budget amount + + +# The System can +1. Pull Data from a .csv file +2. Write Data to a .csv File +3. Send Data to a server +4. Claculate Budget costs +5. Create new list + +# The Application +1. CLI based Interaction +2. .CSV File Persistence +3. Web server Display + + +## Functionality Requirements +1. Able to Retrieve File Data +2. Able to Write to File +3. Able to update Data +4. Able to Display Data. diff --git a/Server.java b/Server.java new file mode 100644 index 0000000..6f3000d --- /dev/null +++ b/Server.java @@ -0,0 +1,23 @@ +package projectzero.budgetar; + +import io.javalin.Javalin; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.stream.Stream; +import org.slf4j.*; +import org.slf4j.LoggerFactory; + +public class Server { + + public void initialize(int port) { + int x = 0; + ArrayList xlist = new GetData().FileContent().itemList; // .HttpDisplayList().httpList; + Iterator xList = xlist.stream().iterator(); + Javalin app = Javalin.create().start(port); + //ArrayList finalX = x; + app.get("/", ctx -> ctx.result("Success: Connection Initiated :\n" + xlist)); + + } + } + diff --git a/ShoppingList.java b/ShoppingList.java new file mode 100644 index 0000000..a551c20 --- /dev/null +++ b/ShoppingList.java @@ -0,0 +1,43 @@ +package projectzero.budgetar; +/** + import javax.xml.transform.sax.SAXSource; +import java.io.IOException; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.stream.Stream; + +import com.sun.net.httpserver.HttpServer; +import io.javalin.Javalin; +import reactor.netty.disposableServer; +import reactor.netty.tcp.TccpServer; + + +public class ShoppingList extends GetData { + HashMap cogsList = cogs; + HashMap nvList = nv; + + public ShoppingList() { + //Object[] xList = new Object[]; + try (Stream String = Files.lines(streamDictionary)) { + } catch (IOException e) { + + } + } + + //Create Servers, + // Netty Server + public static void server(){ + HttpServer.create().route(8080).route( + route -> route.get(path: "/" (req, res) -> res.sendString (Mono.just("Hello Kanyigis!"))) + .get(path: "/Hello/{param})", + (request, response) -> response.sendString( Mono.just("Hello, " + request.parm)"param")) + ).bindNow().onDispose().block(); + } + + //Javalin Server + public static void javalinServer(){ + Javalin server = Javalin.create().start(8080); + server.get("/hello", context -> context.result("

Hello

Atech24

from Javalin ").contentType("text/html")); + } +} + **/ \ No newline at end of file diff --git a/cogsFile.csv b/cogsFile.csv new file mode 100644 index 0000000..fb332dc --- /dev/null +++ b/cogsFile.csv @@ -0,0 +1,25 @@ +Bread,5.25,4 +Milk,3.19,3 +Eggs,3.99,5 +Coffee,11.99,2 +Sugar,3.99,4 +Flour,5.29,3 +Butter,3.99,2 +Cheese,6.99,5 +Yogurt,6.69,5 +Cereal,3.99,4 +Onions,4.26,2 +Garlic,0.56,4 +Apples,7.25,1 +Bananas,3.2,4 +Salt,3.25,2 +Honey,12.98,1 +Chicken,5.55,4 +Fish,6.75,4 +Tomatoes,3.66,5 +Cellery,1.79,4 +Beer,15.99,5 +Wine,16.39,3 +Mangoes,3.25,5 +Honey,2.15,4 +Paper-Towel,4.45,5 \ No newline at end of file diff --git a/getData.java b/getData.java new file mode 100644 index 0000000..8aebf61 --- /dev/null +++ b/getData.java @@ -0,0 +1,62 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Scanner; + +public class getData { + + Scanner entry = new Scanner(System.in); + HashMap cogs = new HashMap(); + HashMap nv = new HashMap(); + int x = 0; + double tfa = 100.00; // start value to prove action performed + + + double GetBudget(double tfa) { //get the Total Budget Size + Scanner entry = new Scanner(System.in); + System.out.println(" Enter Total fund Available: $"); + tfa = entry.nextDouble(); + System.out.println("My Budget: $" + tfa); + return tfa; + } + + HashMap GetShoppingList() { + Scanner entry = new Scanner(System.in); + HashMap cogs = new HashMap(); + int x = 0; + System.out.println("Create a Shopping List"); + do { + String[] itemX = entry.nextLine().split("\\s+"); + //double costX = entry.nextDouble(); + x++; + try { + if (itemX.equals("")) { //if the scanner entry is NOT empty, continue, else catch exception + break; + } else { + cogs.put(itemX[0], itemX[1]); //, costX); + } + } catch (Exception e) { + System.out.println(" Shopping List" + cogs); + break; + } finally { + System.out.println(cogs); + } + } while (cogs.size() < 5 || entry.nextLine().equals("")); //Limits the item list to 10 Items + // Items + System.out.println("Shopping List: " + cogs); + //String itemXlist[] = cogs.keySet(); + return cogs; + } + + + String[] IndexShoppingList() { + String[] itemZ = cogs.keySet().toArray(new String[0]); + HashMap nv = new HashMap(); + System.out.println("Shopping List Items : " + itemZ); + return itemZ; + } + +} + +