diff --git a/classes-part-2/studio/src/main/java/org/launchcode/Main.java b/classes-part-2/studio/src/main/java/org/launchcode/Main.java index a284b3822..3f064cd53 100644 --- a/classes-part-2/studio/src/main/java/org/launchcode/Main.java +++ b/classes-part-2/studio/src/main/java/org/launchcode/Main.java @@ -1,8 +1,56 @@ package org.launchcode; +import java.time.LocalDate; + public class Main { public static void main(String[] args) { // write your code here +//II Step + //create 5 menu items + MenuItem item1 = new MenuItem("Gaucamole", "Dips and Spreads Recipes", 15.00, "appetizers", LocalDate.now()); + MenuItem item2 = new MenuItem("Bruschetta", "an Italian dish that starts with a slice of rustic Italian bread brushed with olive oil", 24.99, "appetizers", LocalDate.now()); + MenuItem item3 = new MenuItem("Queso Mac and Cheese", "This ia a creamy, spicy mac cheese", 30.99, "main course", LocalDate.now()); + MenuItem item4 = new MenuItem("Sesame Tofu and Broccoli", "Oven-baked tofu gets tossed in a savory, sweet, and slightly spicy sauce along with crisp-tender broccoli", 29.99, "main course", LocalDate.now()); + MenuItem item5 = new MenuItem("Cheesecake Ice Cream", "Caramel apple cheesecake dip makes a great fall party dessert ", 19.99, "desserts", LocalDate.now()); + MenuItem item7 = new MenuItem("Ice Cream", "Cold and Delicious", 20.99, "desserts", LocalDate.now()); + System.out.println(item1.isNew()); + + //III Step + //TODO: Print first item + System.out.println(item1); + //this is because of the builtin toString() method + //org.launchcode.MenuItem@3d4eac69 + + //TODO: Create menu + Menu menu = new Menu(); + + //TODO: Add items to menu and print it + menu.addItem(item1); + menu.addItem(item2); + menu.addItem(item3); + menu.addItem(item4); + menu.addItem(item5); +//sout enter + System.out.println(menu); + + + //TODO: Remove an item and print print menu + menu.removeItem(item3); + System.out.println(menu); + //TODO: Test equals method + System.out.println(item1.equals(item2)); + + MenuItem item6 = new MenuItem("Gaucamole", "Dips and Spreads Recipes", 15.00, "appetizers", LocalDate.now()); + System.out.println(item1.equals(item6)); + //BONUS MISSION + + //TODO: Attempt to add a duplicate item, print menu to confirm it wasn't added + menu.addItem(item6); + System.out.println(menu); + + menu.addItem(item7); + System.out.println(menu); + } } diff --git a/classes-part-2/studio/src/main/java/org/launchcode/Menu.java b/classes-part-2/studio/src/main/java/org/launchcode/Menu.java index b4601316b..1552ba413 100644 --- a/classes-part-2/studio/src/main/java/org/launchcode/Menu.java +++ b/classes-part-2/studio/src/main/java/org/launchcode/Menu.java @@ -1,32 +1,90 @@ package org.launchcode; +import java.time.LocalDate; import java.util.ArrayList; -import java.util.Date; public class Menu { - private Date lastUpdated; - private ArrayList items; + //FIELDS + private ArrayList menuItems = new ArrayList<>(); + //private LocalDate lastUpdated; - public Menu(Date d, ArrayList i) { - this.lastUpdated = d; - this.items = i; - } + public LocalDate lastUpdatedMenu(){ + return LocalDate.now(); + }; - public void setLastUpdated(Date lastUpdated) { - this.lastUpdated = lastUpdated; - } - public void setItems(ArrayList items) { - this.items = items; - } + //Default Constructor - public Date getLastUpdated() { - return lastUpdated; + //GETTERS and SETTERS + public ArrayList getMenuItems() { + return menuItems; } - public ArrayList getItems() { - return items; +//SPECIAL METHODS + + //TODO: Define custom toString() method +//List menu items, grouped by category + //Use StringBuilder class in order to loop through multiple items + + //V Step + @Override + public String toString() { + StringBuilder appetizers = new StringBuilder(); + for (MenuItem item : menuItems) { + if (item.getCategory().equals("appetizers")) { + appetizers.append("\n").append(item.toString()).append("\n"); + } + } + + StringBuilder mainCourse = new StringBuilder(); + for (MenuItem item : menuItems) { + if (item.getCategory().equals("main course")) { + mainCourse.append("\n").append(item.toString()).append("\n"); + } + } + + StringBuilder desserts = new StringBuilder(); + for (MenuItem item : menuItems) { + if (item.getCategory().equals("desserts")) { + desserts.append("\n").append(item.toString()).append("\n"); + } + } + return "\nPalm Restaurant MENU\n" + + "APPETIZERS: " + appetizers.toString() + "\n" + + "MAIN COURSES: " + mainCourse.toString() + "\n" + + "DESSERTS: " + desserts.toString() + "\n"; } -} +//IV Step + //INSTANCE METHODS + + //TODO: Define addItem() + //Make sure to update lastUpdates anytime a new menu item is added + //BONUS MISSION: prevent addition of duplicates +void addItem(MenuItem newItem){ + String message = "That item has already been added to the menu"; + if(menuItems.contains(newItem)){ + System.out.println(message); + return; + } + for(MenuItem item : menuItems){ + if(item.equals(newItem)){ + System.out.println(message); + return; + } + } + + menuItems.add(newItem); + //lastUpdated = LocalDate.now(); + lastUpdatedMenu(); +} + //TODO: Define removeItem() + //Make sure to update lastUpdated anytime a menu item is removed +void removeItem(MenuItem item){ + menuItems.remove(item); + // lastUpdated = LocalDate.now(); + lastUpdatedMenu(); +} + +} diff --git a/classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java b/classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java index 22ba70e45..68a89f276 100644 --- a/classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java +++ b/classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java @@ -1,32 +1,108 @@ package org.launchcode; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + public class MenuItem { - private double price; + + private String name; private String description; + private double price; + private String category; - private boolean isNew; + private final LocalDate dateAdded; - public MenuItem(double p, String d, String c, boolean iN) { - this.price = p; - this.description = d; - this.category = c; - this.isNew = iN; + public MenuItem(String name, String description, double price, String category, LocalDate dateAdded) { + this.name = name; + this.description = description; + this.price = price; + this.category = category; + //this.dateAdded = LocalDate.now(); + //To test isNew() to be false + //this.dateAdded = LocalDate.parse("2023-09-21"); + this.dateAdded = dateAdded; } - public void setPrice(double price) { - this.price = price; + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; } public void setDescription(String description) { this.description = description; } + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getCategory() { + + return category; + } + public void setCategory(String category) { + this.category = category; } - public void setNew(boolean aNew) { - isNew = aNew; + //Only getter for dateAdded + + public LocalDate getDateAdded() { + return dateAdded; + } + +//I Step + //Special Methods + + //ToDo: Define custom toString() method + //Format name, description, price and conditional "NEW" + @Override + public String toString() { + String newText = isNew() ? " - NEW! " : ""; + return name + newText + "\n" + category + "\n" + description + " | $" + price; } -} + + //TODO: Define custom equals method +//whether the menuitem is repeated or not + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MenuItem menuItem = (MenuItem) o; + //return Objects.equals(name, menuItem.name); + return this.name.equals(menuItem.getName()); + } + +// @Override +// public int hashCode() { +// return Objects.hash(name); +// } + + + //INSTANCE METHODS + + //TODO: Define instance method is New() + //return true if item added within last 100 days + boolean isNew() { + LocalDate todayDate = LocalDate.now(); + //getDateAdded gets startDate, todayDate-endDate, ChronoUnit.Days-Unit + double daysBetween = getDateAdded().until(todayDate, ChronoUnit.DAYS); + return daysBetween < 100; + } + + +} diff --git a/classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/Menu.java b/classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/Menu.java new file mode 100644 index 000000000..17268d107 --- /dev/null +++ b/classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/Menu.java @@ -0,0 +1,16 @@ +package org.launchcode; + +import java.time.LocalDate; +import java.util.ArrayList; + +public class Menu { + + private ArrayList menuItems = new ArrayList<>(); + private LocalDate lastUpdated; + +//Default Constructor +public ArrayList getMenuItems() { + return menuItems; +} + +} diff --git a/classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/MenuItem.java b/classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/MenuItem.java new file mode 100644 index 000000000..5b51953cb --- /dev/null +++ b/classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/MenuItem.java @@ -0,0 +1,49 @@ +package org.launchcode; + +import java.time.LocalDate; + +public class MenuItem { + private String name; + private String description; + private double price; + private String category; + private final LocalDate dateAdded; + + public MenuItem(String name, String description, double price, String category) { + this.name = name; + this.description = description; + this.price = price; + this.category = category; + this.dateAdded = LocalDate.now(); + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + //Only getter for dateAdded + + public LocalDate getDateAdded() { + return dateAdded; + } +} diff --git a/control-flow-and-collections/studio/counting-characters/.gitignore b/control-flow-and-collections/studio/counting-characters/.gitignore new file mode 100644 index 000000000..5ff6309b7 --- /dev/null +++ b/control-flow-and-collections/studio/counting-characters/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/control-flow-and-collections/studio/counting-characters/pom.xml b/control-flow-and-collections/studio/counting-characters/pom.xml new file mode 100644 index 000000000..792c4d470 --- /dev/null +++ b/control-flow-and-collections/studio/counting-characters/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.example + counting-characters + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + \ No newline at end of file diff --git a/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCount.java b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCount.java new file mode 100644 index 000000000..5a1ee325b --- /dev/null +++ b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCount.java @@ -0,0 +1,44 @@ +package org.launchcode; + +import java.util.HashMap; +import java.util.Map; + +public class CharacterCount { + + //Steps for character count +// Loop through the string one character at a time, +// Store and/or update the count for a given character using an appropriate data structure. +// Loop through the data structure to print the results (one character and its count per line). + public static void main(String[] args) { + String phrase = "If the product of two terms is zero then common sense says at least one of the two terms has to be zero to start with. So if you move all the terms over to one side, you can put the quadratics into a form that can be factored allowing that side of the equation to equal zero. Once you’ve done that, it’s pretty straightforward from there."; + + char[] charArray = phrase.toCharArray(); + + HashMap charCounts = new HashMap<>(); + + for (char letter : charArray) { + if (charCounts.containsKey(letter)) { + //get(object key)-gets the current value of the object key and adding 1 to existing value + charCounts.put(letter, charCounts.get(letter) + 1); + } else { + //checking the key exists or not, if exists setting its value to 1 + charCounts.put(letter, 1); + } + } +// System.out.println(charCounts); + //The class Map.Entry is specifically constructed to represent key/value pairs within HashMaps. +//Each Map.Entry object has a getKey method and a getValue method, +// which represent, the key and value of the map item. + //entrySet() returns an array of key, value pairs + for (Map.Entry charCount : charCounts.entrySet()) { + System.out.println(charCount.getKey() + ": " + charCount.getValue()); + } + + +//If you only need to access the key of each item in a map, +// you can construct a simpler loop: +// for (String student : students.keySet()) { +// System.out.println(student); +// } + } +} \ No newline at end of file diff --git a/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountBonus.java b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountBonus.java new file mode 100644 index 000000000..34b6bf1bd --- /dev/null +++ b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountBonus.java @@ -0,0 +1,42 @@ +package org.launchcode; + +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class CharacterCountBonus { + + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + + System.out.println("Please enter a string : "); + // String userString = input.nextLine(); + + String userString = "If the product of two terms is zero then common sense says at least one of the two terms has to be zero to start with. So if you move all the terms over to one side, you can put the quadratics into a form that can be factored allowing that side of the equation to equal zero. Once you’ve done that, it’s pretty straightforward from there."; + + char[] charArray = userString.toLowerCase().toCharArray(); + + HashMap charCounts = new HashMap<>(); + + for (char letter : charArray) { + //Remember it is Character + if (Character.isLetter(letter)) { + if (charCounts.containsKey(letter)) { + //get(object key)-gets the current value of the object key and adding 1 to existing value + charCounts.put(letter, charCounts.get(letter) + 1); + } else { + //checking the key exists or not, if exists setting its value to 1 + charCounts.put(letter, 1); + } + } + } +// System.out.println(charCounts); + //The class Map.Entry is specifically constructed to represent key/value pairs within HashMaps. +//Each Map.Entry object has a getKey method and a getValue method, +// which represent, the key and value of the map item. + //entrySet() returns an array of key, value pairs + for (Map.Entry charCount : charCounts.entrySet()) { + System.out.println(charCount.getKey() + ": " + charCount.getValue()); + } + } +} diff --git a/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountSuperBonus.java b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountSuperBonus.java new file mode 100644 index 000000000..7b0a93d43 --- /dev/null +++ b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountSuperBonus.java @@ -0,0 +1,59 @@ +package org.launchcode; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class CharacterCountSuperBonus { + + public static void main(String[] args) { + + String stringFromFile = ""; + try { + File textFromFile = new File("src/main/java/org/launchcode/string.txt"); + Scanner myReader = new Scanner(textFromFile); + if(myReader.hasNextLine()) { + stringFromFile = myReader.nextLine(); + } + myReader.close(); + }catch(FileNotFoundException e) { + System.out.println("An error occured while reading string from a file"); + } + + + // System.out.println("Please enter a string : "); + // String userString = input.nextLine(); + + // String userString = "If the product of two terms is zero then common sense says at least one of the two terms has to be zero to start with. So if you move all the terms over to one side, you can put the quadratics into a form that can be factored allowing that side of the equation to equal zero. Once you’ve done that, it’s pretty straightforward from there."; + + //char[] charArray = userString.toLowerCase().toCharArray(); + char[] charArray = stringFromFile.toLowerCase().toCharArray(); + + HashMap charCounts = new HashMap<>(); + + for (char letter : charArray) { + //Remember it is Character + if (Character.isLetter(letter)) { + if (charCounts.containsKey(letter)) { + //get(object key)-gets the current value of the object key and adding 1 to existing value + charCounts.put(letter, charCounts.get(letter) + 1); + } else { + //checking the key exists or not, if exists setting its value to 1 + charCounts.put(letter, 1); + } + } + } +// System.out.println(charCounts); + //The class Map.Entry is specifically constructed to represent key/value pairs within HashMaps. +//Each Map.Entry object has a getKey method and a getValue method, +// which represent, the key and value of the map item. + //entrySet() returns an array of key, value pairs + for (Map.Entry charCount : charCounts.entrySet()) { + System.out.println(charCount.getKey() + ": " + charCount.getValue()); + } + } +} + + diff --git a/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/string.txt b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/string.txt new file mode 100644 index 000000000..7b6fe6993 --- /dev/null +++ b/control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/string.txt @@ -0,0 +1 @@ +If the product of two terms is zero then common sense says at least one of the two terms has to be zero to start with. So if you move all the terms over to one side, you can put the quadratics into a form that can be factored allowing that side of the equation to equal zero. Once you’ve done that, it’s pretty straightforward from there. \ No newline at end of file diff --git a/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java new file mode 100644 index 000000000..842e6d3e5 --- /dev/null +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java @@ -0,0 +1,50 @@ +package org.launchcode; + +import java.util.Scanner; + +public class Area { + + + public static void main(String[] args) { + + Scanner input = new Scanner(System.in); + +// System.out.println("Please enter a radius: "); +// double radius = input.nextDouble();; +// double PI = 3.14; +// double area = PI * radius * radius; +// double area = Circle.getArea(radius); +// System.out.println("The area of a circle with radius " + radius + " is: " + area); + + //Bonus Mission 1 +// System.out.println("Please enter a radius: "); +// if (input.hasNextDouble()) { +// radius = input.nextDouble(); +// if (radius < 0) { +// System.out.println("Please enter a valid positive number for radius"); +// } else { +// area = Circle.getArea(radius); +// System.out.println("The area of a circle with a radius " + radius + " is: " + area); +// } +// } else { +// System.out.println("Please enter a numeric character for radius"); +// } + + //Bonus Mission 2 + + double radius ; + do{ + System.out.println("Please enter a positive number for radius"); + while(!input.hasNextDouble()){ + System.out.println("Please enter a numeric character for radius: "); + input.next(); + } + radius = input.nextDouble(); + }while(radius <= 0); + + double area = Circle.getArea(radius); + System.out.println("The area of a circle with a radius " + radius + " is: " + area); + input.close(); + + } +} diff --git a/datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java b/datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java new file mode 100644 index 000000000..2bc399b71 --- /dev/null +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java @@ -0,0 +1,9 @@ +package org.launchcode; + +public class Circle { + + public static double getArea(double radius) { + double PI = 3.14; + return PI * radius * radius; + } +}