类与对象作业(简单工厂模式)

一、自助饮品系统(作业要求)

二、代码内容

1、代码逻辑关系

2、饮品,Coco类

package naicha;

import java.util.Scanner;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 14:39
 */
public class Coco extends all{
    public void se(){

        //int  price = 0;
        System.out.println("请录入您需要的可乐信息:");
        System.out.println("备注可口可乐还是百事可乐,是否加冰: ");

        Scanner sc = new Scanner(System.in);
        String r = sc.nextLine();
        System.out.println("选择1、中杯2、大杯3、超大杯:");
        Scanner scan = new Scanner(System.in);
        int bei = scan.nextInt();
        System.out.println("购买数量:");
        Scanner scane = new Scanner(System.in);
        int a = scane.nextInt();

        if(bei == 1){
            System.out.println("您购买了可乐:"+r+",购买的中杯,购买数量"+a+"杯"+"价格:"+a+"*"+8+"="+a*8);
        }
        if(bei == 2){
            System.out.println("您购买了可乐:"+r+",购买的大杯,购买数量"+a+"杯"+"价格:"+a+"*"+9+"="+a*9);
        }
        if(bei == 3){
            System.out.println("您购买了可乐:"+r+",购买的超大杯,购买数量"+a+"杯"+"价格:"+a+"*"+10+"="+a*10);
        }

    }
}

3、饮品、Ntea类

package naicha;

import java.util.Scanner;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 14:39
 */
public class Ntea extends all{
    public void se(){
        //int  price = 0;
        System.out.println("请录入您需要的奶茶信息:");
        System.out.println("备注奶茶中是否加入椰果、红豆、布丁,是否加冰: ");

        Scanner sc = new Scanner(System.in);
        String r = sc.nextLine();
        System.out.println("选择1、中杯2、大杯3、超大杯:");
        Scanner scan = new Scanner(System.in);
        int bei = scan.nextInt();
        System.out.println("购买数量:");
        Scanner scane = new Scanner(System.in);
        int a = scane.nextInt();

        if(bei == 1){
            System.out.println("您购买了奶茶:配料:"+r+",购买的中杯,购买数量"+a+"杯"+"价格:"+a+"*"+8+"="+a*8);
        }
        if(bei == 2){
            System.out.println("您购买了奶茶:配料:"+r+",购买的大杯,购买数量"+a+"杯"+"价格:"+a+"*"+9+"="+a*9);
        }
        if(bei == 3){
            System.out.println("您购买了奶茶:配料:"+r+",购买的超大杯,购买数量"+a+"杯"+"价格:"+a+"*"+10+"="+a*10);
        }
    }
}

4、饮品、coffe类

package naicha;

import java.util.Scanner;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 14:39
 */
public class Coffe extends all{
    public void se(){
        //int  price = 0;
        System.out.println("请录入您需要的咖啡信息:");
        System.out.println("备注加糖、加奶还是什么都不加,是否加冰: ");

        Scanner sc = new Scanner(System.in);
        String r = sc.nextLine();
        System.out.println("选择1、中杯2、大杯3、超大杯:");
        Scanner scan = new Scanner(System.in);
        int bei = scan.nextInt();
        System.out.println("购买数量:");
        Scanner scane = new Scanner(System.in);
        int a = scane.nextInt();

        if(bei == 1){
            System.out.println("您购买了咖啡:"+r+",购买的中杯,购买数量"+a+"杯"+"价格:"+a+"*"+8+"="+a*8);
        }
        if(bei == 2){
            System.out.println("您购买了咖啡:"+r+",购买的大杯,购买数量"+a+"杯"+"价格:"+a+"*"+9+"="+a*9);
        }
        if(bei == 3){
            System.out.println("您购买了咖啡:"+r+",购买的超大杯,购买数量"+a+"杯"+"价格:"+a+"*"+10+"="+a*10);
        }
    }
}

5、父类、方法(all)

package naicha;

import java.util.Scanner;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 14:50
 */
abstract public class all {
    public void se(){

    }
}

6、购买者对象(Byer)

package naicha;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 15:52
 */
public class Byer {
    public void takeseld(all er){
        er.se();
    }
}

7、售卖商店(selStore)

package naicha;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 14:45
 */
public class SelStore {
    public static all getall(String allsname) {
        all er = null;
        String SelStore;
        if( allsname == "1" ){
            er = new Coco();
        }
        if (allsname == "2"){
            er = new Ntea();
        }
        if (allsname == "3"){
            er = new Coffe();
        }
        return er;
    }
}

8、测试类(主方法)Test

package naicha;

import java.util.Scanner;

/**
 * @Auther: Mujy
 * @Date: 2022/09/20 - 09 - 20 - 15:56
 */
public class Test {
    public static void main(String[] args) {
        Byer b = new Byer();
        System.out.println("请选择您要购买的饮品:1、可乐,2、奶茶,3、咖啡");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        if(a == 1){
            all er =  SelStore.getall("1");
            b.takeseld(er);
        }
        if(a == 2){
            all er =  SelStore.getall("2");
            b.takeseld(er);
        }
        if(a == 3){
            all er =  SelStore.getall("3");
            b.takeseld(er);
        }
    }
}

三、运行结果

四、交流

有疑问,或者想了解更多。讨论,JAVA类与对象,简单工厂模式的同学可发邮件

1507225369@qq.com有时间会回复!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在摆烂的小母

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值