English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Write a program to implement pizza making. Requirements description Write a program to receive user input information and select the pizza to be made. Available pizzas include: bacon pizza and seafood pizza.
Implementation ideas and key code
1)Analyze bacon pizza and seafood pizza
2)Define the pizza class
3)Properties: Name, Price, Size
4)Method: Display
5)Define bacon pizza and seafood pizza inheriting from the pizza class
6)Define the pizza factory class, generate specific pizza objects based on input information
Pizza.java
package zuoye; import java.util.Scanner; //Superclass 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 pizza size:"); size=sc.nextInt(); System.out.println("Please enter 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("Enter bacon weight:"); 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("Enter seasoning 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 + "\nPrice:" + pg.price + "\nSize:" + pg.size + "\nBacon weight:" +b); } else if (n == 2) { SeaFood sf = new SeaFood("Seafood Pizza"); String a=sf.seafood(); sf.display(); System.out.println("Name" + sf.name + "\nPrice:" + sf.price + "\nSize:" + sf.size + "\nIngredients:" +a); } } }
Result Display:
Summary
That is all about the inheritance of java subclass from superclass in this article-The full code for the implementation of pizza selection, hoping it will be helpful to everyone. Those who are interested can continue to refer to this site:
Java Complete Code Example of Login System Interface Using JFrame
Understanding of Subclass Overriding Parent Class Method in Java Programming
Detailed Explanation of Container (JFrame) in Java GUI Design
If there are any shortcomings, please leave a message to point them out. Thank you friends for your support to this site!
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)