I need help creating a java program. It is for a restraint and I need classes fo
ID: 3588102 • Letter: I
Question
I need help creating a java program. It is for a restraint and I need classes for objects like sandwich chips and drink. How would I make an object or data structure to hold multiple kinds of that object like someone could get 2 drinks 3 different sandwiches and some chips. Any help would be appreciated. I need help creating a java program. It is for a restraint and I need classes for objects like sandwich chips and drink. How would I make an object or data structure to hold multiple kinds of that object like someone could get 2 drinks 3 different sandwiches and some chips. Any help would be appreciated.Explanation / Answer
Hi,
You can write different classes for Drink, Sandwich and Chips like below,
Drink.java
public class Drink {//class for drink type food
private String name;//name
private double price;//price
public String getName() {
return name;
}
public Drink(double price) {
super();
this.name="Drink";
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
2. Chips.java
public class Chips {//class for drink type food
private String name;//name
private double price;//price
public String getName() {
return name;
}
public Chips(double price) {//constructor to set variables
super();
this.name="chips";
this.price = price;
}
/* getters and setters */
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
3.sandwich.java
public class Sandwich {//class for drink type food
private String name;//name
private double price;//price
public String getName() {
return name;
}
public Sandwich(double price) {//constructor to set variables
super();
this.name="Sandwich";
this.price = price;
}
/* getters and setters */
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
4.Restaurant.java
import java.util.ArrayList;
import java.util.List;
public class Restaurant {
private String name;//name
List<Sandwich> sandwiches= new ArrayList<>();//list to hold sandwiches
List<Chips> chips= new ArrayList<>();//list to hold chips
List<Drink> drinks= new ArrayList<>();//list to hold drinks
/* getters and setters */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Sandwich> getSandwiches() {
return sandwiches;
}
public void setSandwiches(List<Sandwich> sandwiches) {
this.sandwiches = sandwiches;
}
public List<Chips> getChips() {
return chips;
}
public void setChips(List<Chips> chips) {
this.chips = chips;
}
public List<Drink> getDrinks() {
return drinks;
}
public void setDrinks(List<Drink> drinks) {
this.drinks = drinks;
}
}
you can use list of the sandwiches, drinks anc chips to hold multiple objects as shown above
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.