从 Java 中的列表中显示特定类型披萨的方法
Method to display particular types of pizza from a list in Java
我在 Java 中有一个简单的披萨程序,使用工厂模式制作。
基本上,当工厂作为参数给出时,它会创建一个特定的比萨饼,然后将其添加到 PizzaShop 的比萨饼列表中。
我想创建一个方法来显示我有多少个特定的比萨饼。例如,当调用该方法时,我希望它显示类似“我们有 5 个 PizzaChicago 和 3 个 PizzaNewYork”的内容。我不知道该怎么做。
这是我的代码。
public interface Pizza {
String name();
}
public class PizzaChicago implements Pizza{
public Integer price;
public PizzaChicago(Integer price){
this.price = price;
}
@Override
public String name() {
return this.getClass().getSimpleName();
}
}
public class PizzaNewYork implements Pizza{
public Integer price;
public PizzaNewYork(Integer price){
this.price = price;
}
@Override
public String name() {
return this.getClass().getSimpleName();
}
}
public interface PizzaFactory {
public Pizza createPizza(Integer price);
}
public class PizzaNewYorkFactory implements PizzaFactory{
@Override
public Pizza createPizza(Integer price) {
return new PizzaNewYork(6);
}
}
public class PizzaChicagoFactory implements PizzaFactory{
@Override
public Pizza createPizza(Integer price) {
return new PizzaChicago(8);
}
}
import java.util.ArrayList;
import java.util.List;
public class PizzaShop {
List<Pizza> pizzaList = new ArrayList<>();
public void createPizza(PizzaFactory factory, Integer price){
Pizza pizza = factory.createPizza(price);
System.out.println(pizza.name() + " " + "was created");
pizzaList.add(pizza);
}
}
`
您需要做的是迭代列表并检查每个对象的类型。
int countPizzaNewYork = 0, countPizzaChicago = 0;
for(Pizza p: pizzaList){
if(p instanceOf PizzaNewYork)
{
countPizzaNewYork++;
}
else
{
countPizzaChicago++;
}
}
System.out.println("We have "+ countPizzaChicago+" PizzaChicago and "+countPizzaNewYork+" PizzaNewYork");
替代方法(使用 Map 而不是 List):
public class PizzaShop {
Map<String, Integer> pizzaDiary = new HashMap<>();
public void createPizza(PizzaFactory factory, Integer price) {
Pizza pizza = factory.createPizza(price);
System.out.println(pizza.name() + " " + "was created");
int previousCount = pizzaDiary.getOrDefault(pizza.name(), 0);
pizzaDiary.put(pizza.name(), previousCount + 1);
}
}
使用 pizzaDiary
地图打印出您的披萨数量。
每个披萨单独 class 可能会出现问题并且有些麻烦。并且对于某些使用 class name
和 instanceof
不是要走的路。您是否考虑过使用枚举来表示比萨饼?这是它可能如何工作的示例。
public class PizzaShopDemo {
enum Pizza {
CHICAGOPIZZA(8, "Chicago Pizza"),
NEWYORKPIZZA(6, "New York Pizza");
private double price;
private String name;
private int count = 0;
private Pizza(double price, String name) {
this.price = price;
this.name = name;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public void update() {
count++;
}
public int getCount() {
return count;
}
}
public static void main(String[] args) {
new PizzaShopDemo().pizzaShop();
}
public void pizzaShop() {
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.NEWYORKPIZZA);
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.NEWYORKPIZZA);
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.NEWYORKPIZZA);
for (Pizza p : Pizza.values()) {
System.out.println(p.getName() + ", " + p.getPrice()
+ ", " + p.getCount());
}
}
public void createPizza(Pizza type) {
type.update();
// other stuff here.
}
}
版画
Chicago Pizza, 8.0, 4
New York Pizza, 6.0, 3
我在 Java 中有一个简单的披萨程序,使用工厂模式制作。 基本上,当工厂作为参数给出时,它会创建一个特定的比萨饼,然后将其添加到 PizzaShop 的比萨饼列表中。
我想创建一个方法来显示我有多少个特定的比萨饼。例如,当调用该方法时,我希望它显示类似“我们有 5 个 PizzaChicago 和 3 个 PizzaNewYork”的内容。我不知道该怎么做。
这是我的代码。
public interface Pizza {
String name();
}
public class PizzaChicago implements Pizza{
public Integer price;
public PizzaChicago(Integer price){
this.price = price;
}
@Override
public String name() {
return this.getClass().getSimpleName();
}
}
public class PizzaNewYork implements Pizza{
public Integer price;
public PizzaNewYork(Integer price){
this.price = price;
}
@Override
public String name() {
return this.getClass().getSimpleName();
}
}
public interface PizzaFactory {
public Pizza createPizza(Integer price);
}
public class PizzaNewYorkFactory implements PizzaFactory{
@Override
public Pizza createPizza(Integer price) {
return new PizzaNewYork(6);
}
}
public class PizzaChicagoFactory implements PizzaFactory{
@Override
public Pizza createPizza(Integer price) {
return new PizzaChicago(8);
}
}
import java.util.ArrayList;
import java.util.List;
public class PizzaShop {
List<Pizza> pizzaList = new ArrayList<>();
public void createPizza(PizzaFactory factory, Integer price){
Pizza pizza = factory.createPizza(price);
System.out.println(pizza.name() + " " + "was created");
pizzaList.add(pizza);
}
}
`
您需要做的是迭代列表并检查每个对象的类型。
int countPizzaNewYork = 0, countPizzaChicago = 0;
for(Pizza p: pizzaList){
if(p instanceOf PizzaNewYork)
{
countPizzaNewYork++;
}
else
{
countPizzaChicago++;
}
}
System.out.println("We have "+ countPizzaChicago+" PizzaChicago and "+countPizzaNewYork+" PizzaNewYork");
替代方法(使用 Map 而不是 List):
public class PizzaShop {
Map<String, Integer> pizzaDiary = new HashMap<>();
public void createPizza(PizzaFactory factory, Integer price) {
Pizza pizza = factory.createPizza(price);
System.out.println(pizza.name() + " " + "was created");
int previousCount = pizzaDiary.getOrDefault(pizza.name(), 0);
pizzaDiary.put(pizza.name(), previousCount + 1);
}
}
使用 pizzaDiary
地图打印出您的披萨数量。
每个披萨单独 class 可能会出现问题并且有些麻烦。并且对于某些使用 class name
和 instanceof
不是要走的路。您是否考虑过使用枚举来表示比萨饼?这是它可能如何工作的示例。
public class PizzaShopDemo {
enum Pizza {
CHICAGOPIZZA(8, "Chicago Pizza"),
NEWYORKPIZZA(6, "New York Pizza");
private double price;
private String name;
private int count = 0;
private Pizza(double price, String name) {
this.price = price;
this.name = name;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public void update() {
count++;
}
public int getCount() {
return count;
}
}
public static void main(String[] args) {
new PizzaShopDemo().pizzaShop();
}
public void pizzaShop() {
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.NEWYORKPIZZA);
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.NEWYORKPIZZA);
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.CHICAGOPIZZA);
createPizza(Pizza.NEWYORKPIZZA);
for (Pizza p : Pizza.values()) {
System.out.println(p.getName() + ", " + p.getPrice()
+ ", " + p.getCount());
}
}
public void createPizza(Pizza type) {
type.update();
// other stuff here.
}
}
版画
Chicago Pizza, 8.0, 4
New York Pizza, 6.0, 3