From ab32201128a3baae9143ecda87c90a8cbfca22f9 Mon Sep 17 00:00:00 2001 From: nandinigit33 Date: Thu, 5 Sep 2024 21:36:02 -0400 Subject: [PATCH 1/6] committing datatypes studio assignment --- .../src/main/java/org/launchcode/Area.java | 43 +++++++++++++++++++ .../src/main/java/org/launchcode/Circle.java | 8 ++++ 2 files changed, 51 insertions(+) create mode 100644 datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java create mode 100644 datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java 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..404930c6f --- /dev/null +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java @@ -0,0 +1,43 @@ +package org.launchcode; + +import java.util.Scanner; + +public class Area { + + + public static void main(String[] args) { + double radius; + double pi = 3.14; + double area; + Scanner input = new Scanner(System.in); + System.out.println("Enter a radius:"); + //radius = input.nextDouble(); +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 of " + radius + " is: " + area); + } + } + else { + System.out.println("Please enter a numeric character for radius"); +} + input.close(); + + //area = pi * radius * radius; + + + /* + if(!input.hasNextDouble()){ + System.out.println("Please enter a numeric character for radius"); + input.next(); + } + radius = input.nextDouble(); + area = Circle.getArea(radius); + System.out.println("The area of a circle of " + 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..b6a6e6801 --- /dev/null +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java @@ -0,0 +1,8 @@ +package org.launchcode; + +public class Circle { + + public static Double getArea(Double radius) { + return 3.14 * radius * radius; + } +} From b41640484a08371f6f0fd5ea6d335d385b5baa8a Mon Sep 17 00:00:00 2001 From: nandinigit33 Date: Sun, 8 Sep 2024 11:22:46 -0400 Subject: [PATCH 2/6] committing update two datatypes studio assignment --- .../src/main/java/org/launchcode/Area.java | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java index 404930c6f..d0acbdc08 100644 --- a/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java @@ -12,32 +12,18 @@ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a radius:"); //radius = input.nextDouble(); -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 of " + radius + " is: " + area); - } - } - else { - System.out.println("Please enter a numeric character for radius"); -} - input.close(); - - //area = pi * radius * radius; - - - /* - if(!input.hasNextDouble()){ + 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 of " + radius + " is: " + area); + } + } else { System.out.println("Please enter a numeric character for radius"); - input.next(); } - radius = input.nextDouble(); - area = Circle.getArea(radius); - System.out.println("The area of a circle of " + radius + " is: " + area); input.close(); -*/ + } } From 1c5a2ab4fe8dbc8af6f2fd6f68ec3f0d663f070e Mon Sep 17 00:00:00 2001 From: nandinigit33 Date: Mon, 9 Sep 2024 13:28:57 -0400 Subject: [PATCH 3/6] committing final changes --- .../src/main/java/org/launchcode/Area.java | 51 +++++++++++++------ .../src/main/java/org/launchcode/Circle.java | 5 +- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java index d0acbdc08..842e6d3e5 100644 --- a/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Area.java @@ -6,23 +6,44 @@ public class Area { public static void main(String[] args) { - double radius; - double pi = 3.14; - double area; + Scanner input = new Scanner(System.in); - System.out.println("Enter a radius:"); - //radius = input.nextDouble(); - 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 of " + radius + " is: " + area); + +// 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(); } - } else { - System.out.println("Please enter a numeric character for radius"); - } + 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 index b6a6e6801..2bc399b71 100644 --- a/datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java +++ b/datatypes/datatypes-studio/src/main/java/org/launchcode/Circle.java @@ -2,7 +2,8 @@ public class Circle { - public static Double getArea(Double radius) { - return 3.14 * radius * radius; + public static double getArea(double radius) { + double PI = 3.14; + return PI * radius * radius; } } From 4b4e3034844ab7cbcb56a78ec9a30f989277395f Mon Sep 17 00:00:00 2001 From: nandinigit33 Date: Wed, 11 Sep 2024 00:36:30 -0400 Subject: [PATCH 4/6] committing studio 2 assignment --- .../studio/counting-characters/.gitignore | 38 ++++++++++++ .../studio/counting-characters/pom.xml | 17 ++++++ .../java/org/launchcode/CharacterCount.java | 44 ++++++++++++++ .../org/launchcode/CharacterCountBonus.java | 42 +++++++++++++ .../launchcode/CharacterCountSuperBonus.java | 59 +++++++++++++++++++ .../src/main/java/org/launchcode/string.txt | 1 + 6 files changed, 201 insertions(+) create mode 100644 control-flow-and-collections/studio/counting-characters/.gitignore create mode 100644 control-flow-and-collections/studio/counting-characters/pom.xml create mode 100644 control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCount.java create mode 100644 control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountBonus.java create mode 100644 control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/CharacterCountSuperBonus.java create mode 100644 control-flow-and-collections/studio/counting-characters/src/main/java/org/launchcode/string.txt 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 From e967da7315083be0486a4f4cb1ea65e1aabb88a6 Mon Sep 17 00:00:00 2001 From: nandinigit33 Date: Fri, 20 Sep 2024 00:09:41 -0400 Subject: [PATCH 5/6] updating Java web dev projects folder after resolving errors --- .../src/main/java/org/launchcode/Menu.java | 32 ------------ .../main/java/org/launchcode/MenuItem.java | 32 ------------ .../src/main/java/org/launchcode/Menu.java | 16 ++++++ .../main/java/org/launchcode/MenuItem.java | 49 +++++++++++++++++++ 4 files changed, 65 insertions(+), 64 deletions(-) delete mode 100644 classes-part-2/studio/src/main/java/org/launchcode/Menu.java delete mode 100644 classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java create mode 100644 classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/Menu.java create mode 100644 classes-part-one/studio/restaurant-menu/src/main/java/org/launchcode/MenuItem.java 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 deleted file mode 100644 index b4601316b..000000000 --- a/classes-part-2/studio/src/main/java/org/launchcode/Menu.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.launchcode; - -import java.util.ArrayList; -import java.util.Date; - -public class Menu { - private Date lastUpdated; - private ArrayList items; - - public Menu(Date d, ArrayList i) { - this.lastUpdated = d; - this.items = i; - } - - public void setLastUpdated(Date lastUpdated) { - this.lastUpdated = lastUpdated; - } - - public void setItems(ArrayList items) { - this.items = items; - } - - public Date getLastUpdated() { - return lastUpdated; - } - - public ArrayList getItems() { - return items; - } -} - - 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 deleted file mode 100644 index 22ba70e45..000000000 --- a/classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.launchcode; - -public class MenuItem { - private double price; - private String description; - private String category; - private boolean isNew; - - public MenuItem(double p, String d, String c, boolean iN) { - this.price = p; - this.description = d; - this.category = c; - this.isNew = iN; - } - - public void setPrice(double price) { - this.price = price; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setCategory(String category) { - this.category = category; - } - - public void setNew(boolean aNew) { - isNew = aNew; - } -} - 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; + } +} From f38f797f6aff3ea12f1ccf547f82d13de54c438f Mon Sep 17 00:00:00 2001 From: nandinigit33 Date: Sat, 21 Sep 2024 21:49:36 -0400 Subject: [PATCH 6/6] committing classes part 2 studio assignment --- .../src/main/java/org/launchcode/Main.java | 48 ++++++++ .../src/main/java/org/launchcode/Menu.java | 90 +++++++++++++++ .../main/java/org/launchcode/MenuItem.java | 108 ++++++++++++++++++ 3 files changed, 246 insertions(+) create mode 100644 classes-part-2/studio/src/main/java/org/launchcode/Menu.java create mode 100644 classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java 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 new file mode 100644 index 000000000..1552ba413 --- /dev/null +++ b/classes-part-2/studio/src/main/java/org/launchcode/Menu.java @@ -0,0 +1,90 @@ +package org.launchcode; + +import java.time.LocalDate; +import java.util.ArrayList; + +public class Menu { + //FIELDS + private ArrayList menuItems = new ArrayList<>(); + //private LocalDate lastUpdated; + + public LocalDate lastUpdatedMenu(){ + return LocalDate.now(); + }; + + + //Default Constructor + + //GETTERS and SETTERS + public ArrayList getMenuItems() { + return menuItems; + } + +//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 new file mode 100644 index 000000000..68a89f276 --- /dev/null +++ b/classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java @@ -0,0 +1,108 @@ +package org.launchcode; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + +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, 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 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; + } + + //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; + } + + +}