小明家新开了两个工厂用来生产家具,一个生产现代风格的沙发和椅子,一个生产古典风格的沙发和椅子,现在工厂收到了一笔订单,请你帮他设计一个系统,描述订单需要生产家具的信息。
输入的第一行是一个整数 N(1 ≤ N ≤ 100),表示订单的数量。
接下来的 N 行,每行输入一个字符串,字符串表示家具的类型。家具类型分为 "modern" 和 "classical" 两种。
对于每笔订单,输出字符串表示该订单需要生产家具的信息。
modern订单会输出下面两行字符串
modern chair
modern sofa
classical订单会输出下面两行字符串
classical chair
classical soft
- 3
- modern
- classical
- modern
- modern chair
- modern sofa
- classical chair
- classical sofa
- modern chair
- modern sofa
在示例中,工厂收到了3笔订单,其中有2笔要求生产modern风格,1笔要求生产classical风格。根据输入的类型,每次订单生产的家具信息被输出到控制台上。
- package abstractFactory;
-
- import java.util.Scanner;
-
- // 抽象椅子接口
- interface Chair {
- void showInfo();
- }
-
- // 具体现代风格椅子
- class ModernChair implements Chair {
-
- @Override
- public void showInfo() {
- System.out.println("modern chair");
- }
- }
-
- // 具体古典风格椅子
- class ClassicalChair implements Chair {
-
- @Override
- public void showInfo() {
- System.out.println("classical chair");
- }
- }
-
- // 抽象沙发接口
- interface Sofa {
- void displayInfo();
- }
-
- // 具体现代风格沙发
- class ModernSofa implements Sofa {
- @Override
- public void displayInfo() {
- System.out.println("modern sofa");
- }
- }
-
- // 具体古典风格沙发
- class ClassicalSofa implements Sofa {
- @Override
- public void displayInfo() {
- System.out.println("classical sofa");
- }
- }
-
- // 抽象家居工厂接口
- interface FurnitureFactory {
- Chair createChair();
- Sofa createSofa();
- }
-
- // 具体现代风格家居工厂
- class ModernFurnitureFactory implements FurnitureFactory {
- @Override
- public Chair createChair() {
- return new ModernChair();
- }
-
- @Override
- public Sofa createSofa() {
- return new ModernSofa();
- }
- }
-
- // 具体古典风格家居工厂
- class ClassicalFurnitureFactory implements FurnitureFactory {
- @Override
- public Chair createChair() {
- return new ClassicalChair();
- }
-
- @Override
- public Sofa createSofa() {
- return new ClassicalSofa();
- }
- }
-
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
-
- // 读取订单数量
- int N = scanner.nextInt();
-
- // 处理每个订单
- for (int i = 0; i < N; i++) {
- // 读取家具类型
- String furnitureType = scanner.next();
-
- // 创建相应风格的家居装饰品工厂
- FurnitureFactory factory = null;
- if (furnitureType.equals("modern")) {
- factory = new ModernFurnitureFactory();
- } else if (furnitureType.equals("classical")) {
- factory = new ClassicalFurnitureFactory();
- }
-
- // 根据工厂生产椅子和沙发
- Chair chair = factory.createChair();
- Sofa sofa = factory.createSofa();
-
- // 输出家具信息
- chair.showInfo();
- sofa.displayInfo();
- }
- }
- }