Write programs to implement pizza production. Requirements description write a program, receive information entered by the user, and select the pizza you want to make. Pizza available are: Bacon Pizza and Seafood Pizza.
Implementation ideas and key codes
1) Analysis of bacon pizza and seafood pizza
2) Define pizza classes
3) Attributes: name, price, size
4) Method: Display
5) Definition Bacon Pizza and Seafood Pizza inherit from pizza
6) Define the pizza factory class and generate specific pizza objects based on the input information
Pizza.java
package zuoye;import java.util.Scanner;//Premium public class Pizza {String name;double price;int size;public Pizza(String name){this.name=name;}public void display(){Scanner sc=new Scanner(System.in);System.out.println("Please enter the pizza size:");size=sc.nextint();System.out.println("Please enter the pizza price:");price=sc.nextdouble();}}PeiGen.java
package zuoye;import java.util.Scanner;//Subclass bacon pizza public class PeiGen extends Pizza {// double weight;public PeiGen(String name) {super(name);}public double peigen() {System.out.println("Input bacon grams:");Scanner s = new Scanner(System.in);return s.nextdouble();}}SeaFood.java
package zuoye;import java.util.Scanner;//Subclass seafood pizza public class SeaFood extends Pizza{public SeaFood(String name) {super(name);}public String seafood() {System.out.println("Input ingredient information:");Scanner s=new Scanner(System.in);String peiliao=s.next();return peiliao;}}Work.java
package zuoye;import java.util.Scanner;public class Work {public static void main(String[] args) {System.out.println("Please select the pizza you want (1. Bacon pizza 2. Seafood pizza)");Scanner s = new Scanner(System.in);int n = s.nextint();if (n == 1) {PeiGen pg = new PeiGen("Bacon pizza");double b=pg.peigen();pg.display();System.out.println("name" + pg.name + "/n price:" + pg.price + "/n size: " + pg.size + "/n bacon grams: " +b);} else if (n == 2) {SeaFood sf = new SeaFood("Seafood Pizza");String a=sf.seafood();sf.display();System.out.println("name" + sf.name + "/n price:" + sf.price + "/n size:" + sf.size + "/n ingredients:" + a);}}}Results show:
Summarize
The above is the entire content of this article about the selection implementation code of the java subclass inheritance parent class instance - pizza. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
Java uses JFrame to create a complete code example for logging into the system interface
Java Programming Understanding of the Issue of Rewriting Parent Class Methods in Subclasses
Detailed explanation of Java graphical interface design container (JFrame)
If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!