Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

classes-part-2-studio-Nandini #29

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
48 changes: 48 additions & 0 deletions classes-part-2/studio/src/main/java/org/launchcode/Main.java
Original file line number Diff line number Diff line change
@@ -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);

}
}
94 changes: 76 additions & 18 deletions classes-part-2/studio/src/main/java/org/launchcode/Menu.java
Original file line number Diff line number Diff line change
@@ -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<MenuItem> items;
//FIELDS
private ArrayList<MenuItem> menuItems = new ArrayList<>();
//private LocalDate lastUpdated;

public Menu(Date d, ArrayList<MenuItem> 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<MenuItem> items) {
this.items = items;
}
//Default Constructor

public Date getLastUpdated() {
return lastUpdated;
//GETTERS and SETTERS
public ArrayList<MenuItem> getMenuItems() {
return menuItems;
}

public ArrayList<MenuItem> 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();
}

}
100 changes: 88 additions & 12 deletions classes-part-2/studio/src/main/java/org/launchcode/MenuItem.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.launchcode;

import java.time.LocalDate;
import java.util.ArrayList;

public class Menu {

private ArrayList<MenuItem> menuItems = new ArrayList<>();
private LocalDate lastUpdated;

//Default Constructor
public ArrayList<MenuItem> getMenuItems() {
return menuItems;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
38 changes: 38 additions & 0 deletions control-flow-and-collections/studio/counting-characters/.gitignore
Original file line number Diff line number Diff line change
@@ -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
Loading