首页 > 其他分享 >题目:披萨商店

题目:披萨商店

时间:2022-10-08 11:26:05浏览次数:45  
标签:题目 商店 int price 披萨 System public size

题目:

普通实现:

package com.gao.Project.Pro2;

public class Pizza {
    //父类:共同的属性:名称,大小,价格
          //共同的方法:展示披萨的信息

    //属性:
    private String name;  //名称
    private int size;     //大小
    private int price;    //价格

    //方法:

    //为属性添加Setter,Getter方法   快捷键:alt+insert

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    //展示披萨信息:
    public String showpizza(){
        return "名称:"+name+"\n价格:"+price + "元\n大小:"+size +"寸\n";
    }

    //构造器:
    //保证空参构造器的存在:
    //(1)便于通过反射,创建运行时类的对象
    //(2)便于子类继承此运行实类是,默认调用空参构造器时,保证父类有此构造器

    public Pizza() {
    }
    //包括全部参数的构造器
    public Pizza(String name, int size, int price) {
        this.name = name;
        this.size = size;
        this.price = price;
    }
}

package com.gao.Project.Pro2;

public class FruitsPizza extends Pizza{
    //属性:配料
    private String burdening;

    public String getBurdening() {
        return burdening;
    }

    public void setBurdening(String burdening) {
        this.burdening = burdening;
    }
    //构造器
    //空参构造器

    public FruitsPizza() {
    }

    //包括全部参数构造器

    public FruitsPizza(String name, int size, int price, String burdening) {
        super(name, size, price);
        this.burdening = burdening;
    }
    //重写父类showpizza方法

    @Override
    public String showpizza() {
        return super.showpizza()+"配料水果:"+burdening;
    }
}

package com.gao.Project.Pro2;

public class BaconPizza extends Pizza{
    //属性:克数
    private double weight;

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    //构造器
    //空参构造器
    public BaconPizza() {
    }
    //包括全部参数的构造器
    public BaconPizza(String name, int size, int price, double weight) {
        super(name, size, price);
        this.weight = weight;
    }

    //重写父类showpizza方法

    @Override
    public String showpizza() {
        return super.showpizza()+"培根克数:"+weight+"g";
    }
}

package com.gao.Project.Pro2;

import java.net.Socket;
import java.sql.SQLOutput;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //选择购买披萨
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择想要制作的披萨(1.培根披萨  2.水果披萨):");
        int choice = sc.nextInt();//选择
        switch (choice){
            case 1:
                {
                    System.out.println("请输入培根的克数:");
                    double weight = sc.nextDouble();
                    System.out.println("请输入披萨的大小;");
                    int size = sc.nextInt();
                    System.out.println("请输入披萨的价格:");
                    int price = sc.nextInt();
                    //将录入的信息封装成培根披萨的信息
                    BaconPizza bp = new BaconPizza("培根披萨",size,price,weight);
                    System.out.println(bp.showpizza());
               }
               break;
            case 2:
               {
                    System.out.println("请输入你想要加入的水果:");
                    String burdening = sc.next();
                    System.out.println("请输入披萨的大小:");
                    int size = sc.nextInt();
                    System.out.println("请输入披萨的价格:");
                    int price = sc.nextInt();
                    //将录入的信息封装成水果披萨的信息
                   FruitsPizza fp = new FruitsPizza("水果披萨",size,price,burdening);
                   System.out.println(fp.showpizza());
                  //加代码块,作用在不同的作用域
               }
               break;
        }
    }
}

利用工厂加强:

新加了PizzaStore类

package com.gao.Project.Pro2;

import java.util.Scanner;

public class PizzaStore {
    public static Pizza getPizza(int choice){
        Pizza p = null;
        Scanner sc = new Scanner(System.in);
        switch (choice){
            case 1:
            {
                System.out.println("请输入培根的克数:");
                double weight = sc.nextDouble();
                System.out.println("请输入披萨的大小;");
                int size = sc.nextInt();
                System.out.println("请输入披萨的价格:");
                int price = sc.nextInt();
                //将录入的信息封装成培根披萨的信息
                BaconPizza bp = new BaconPizza("培根披萨",size,price,weight);
                p = bp;
            }
            break;
            case 2:
            {
                System.out.println("请输入你想要加入的水果:");
                String burdening = sc.next();
                System.out.println("请输入披萨的大小:");
                int size = sc.nextInt();
                System.out.println("请输入披萨的价格:");
                int price = sc.nextInt();
                //将录入的信息封装成水果披萨的信息
                FruitsPizza fp = new FruitsPizza("水果披萨",size,price,burdening);
                p = fp;
                //加代码块,作用在不同的作用域
            }
            break;
        }
        return p;
    }
}

只修改了原代码的Test类

package com.gao.Project.Pro2;

import java.net.Socket;
import java.sql.SQLOutput;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //选择购买披萨
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择想要制作的披萨(1.培根披萨  2.水果披萨):");
        int choice = sc.nextInt();//选择
        //从工厂获取披萨
        Pizza pizza = PizzaStore.getPizza(choice);
        System.out.println(pizza.showpizza());

    }
}

标签:题目,商店,int,price,披萨,System,public,size
From: https://www.cnblogs.com/gaoxiaocuo/p/16768341.html

相关文章

  • Shell及Linux常见易错题目题库-Shell/Linux-选择、简答、判断、编程
    1、以下不合法的shell头是(不合法指运行会报错)(   )A.#!/bin/bashB.#-/bin/bashC.!#/bin/bash答案:C 2、if[$2-a$2="test"]中-a是什么意思(  )A.大于B.减C.......
  • 题目的解法(思路正确但是时间复杂度过高的错误解法)
    前言:仅为了纪念那个不甘的心(悲)1.P1002[NOIP2002普及组]过河卒(洛谷)#include<stdio.h>#include<iostream>usingnamespacestd;intans;//答案intx_end,y_end......
  • 贤鱼的刷题日常--P1022 [NOIP2000 普及组] 计算器的改良--题目详解
    ......
  • 记录一个 Full of accomplishment & 纯手调了一天 的题目
    P1018乘积最大艰辛历程AC&未去除调试注释(不用你数,141行)#include<iostream>#include<cstdio>#include<string>#include<cstring>#definelllonglong......
  • 顺序表题目
    1.顺序表上的查找题目描述:定义函数,传入要被查找的顺序表和要查找的值,如果找到返回值的索引,不存在则返回-1,并输出"不存在"即可。intsearch(RankListran,intval){......
  • 9.29题目大赏
    2022-9-29前两道真的很水,然而我写挂了QAQT1智力大冲浪简单得不能再简单的贪心。先将每个游戏按扣钱为第一关键字降序,时刻为第二关键字升序排列。因为我们希望无法完成......
  • LeetCode - 字符串类型题目
    剑指Offer05.替换空格请实现一个函数,把字符串 s 中的每个空格替换成"%20"。方法:双指针首先统计空格数量count,然后为原字符串数组扩展出2*count的空间,用来存......
  • 题目
    题目1.列举你所了解的计算机存储设备类型?答:随机存储器RAMSRAM、DRAM(SDRAM、RDRAM、CDRAM等)只读存储器ROMMROM、PROM、EPROM、EEPROM2.一般代码存储在计算机的哪个设备中?......
  • Atcoder 题目选做
    ABC257G直接考虑\(\rmKMP\)的过程。\(\rmKMP\)可以帮助我们求出\(S\)的\(border\),并找到\(T\)中每一个位置能匹配上的\(S\)的最长前缀。那么我们就可以很......
  • 宽带可以上网,但是无法打开微软商店
    宽带可以正常上网,但是无法打开microsoftstore,使用wifi又可以打开,解决办法:设置->拨号->高级选项->网络配置文件->公用来源:https://answers.microsoft.com/zh-hans/window......