Java实例学习1 小型点餐系统
一.题目
二.解决步骤
项目结构分析
先自己写…(发现目前学的是真的拉)
写出来就这…呜呜呜😭
错误一堆,逻辑不清…呜呜呜
-
父类 -
子类
-
培根披萨 -
水果披萨 -
打算用于输出的函数
呜呜新建个模块重写跟着敲
-
新建模块 -
新建包和父类
1.父类Pizza类
-
先写好属性 package com.practice.test001;
public class Pizza {
private String name;
private int size;
private int price;
}
-
方法
教程里面那个快捷键我去查了一下:IntelliJ IDEA如何生成构造器及快捷键
-
每个属性都要setter getter方法,用快捷键alt + insert -
点击之后,全部选中 -
点击OK ,就自动生成了🐮! -
加一个展示Pizza信息的方法 public String showPizza(){
return "匹萨的名字是 :"+name+",匹萨的大小是 :"+size+",匹萨的价格是 :"+price;
}
-
添加构造器
-
还是用快捷键alt+insert
-
无参构造器 -
含三个参数的构造器 -
父类完整代码 package com.practice.test001;
public class Pizza {
private String name;
private int size;
private int price;
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 + ",匹萨的大小是 :" + size + ",匹萨的价格是 :" + price;
}
public Pizza() {
}
public Pizza(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
}
2.子类
-
水果匹萨
可以用快捷键🔥速生成
快捷键:alt+insert 或者选中属性之后alt+enter
package com.practice.test001;
public class FruitPizza extends Pizza {
private String ingredient;
public String getIngredient() {
return ingredient;
}
public void setIngredient(String ingredient){
this.ingredient = ingredient;
}
public FruitPizza() {
}
public FruitPizza(String name, int size, int price, String ingredient) {
super(name, size, price);
this.ingredient = ingredient;
}
}
-
培根匹萨 package com.practice.test001;
public class BaconPizza extends Pizza {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public BaconPizza() {
}
public BaconPizza(String name, int size, int price, int weight) {
super(name, size, price);
this.weight = weight;
}
}
-
目前层次结构
3.业务逻辑
小技巧:代码块的使用
注意case后面要用英文格式的冒号
封装对象
-
把输入的东西作为参数传入构造器,再调用showPizza() 方法 package com.practice.test001;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请选择你想要购买的匹萨类型(1.培根披萨 2.水果匹萨)");
int choice = scan.nextInt();
switch (choice) {
case 1: {
System.out.print("请输入培根的克数:");
int weight = scan.nextInt();
System.out.print("请输入匹萨的大小:");
int size = scan.nextInt();
System.out.print("请输入披萨的价格:");
int price = scan.nextInt();
BaconPizza bp = new BaconPizza("培根披萨",size,price,weight);
System.out.println(bp.showPizza());
}
break;
case 2: {
System.out.print("请输入你想要加入的水果:");
String ingredient = scan.next();
System.out.print("请输入匹萨的大小:");
int size = scan.nextInt();
System.out.print("请输入披萨的价格:");
int price = scan.nextInt();
FruitPizza fp = new FruitPizza("水果披萨",size,price,ingredient);
System.out.println(fp.showPizza());
}
break;
}
}
}
-
测试一下
在子类中重写showPizza() 方法
因为两个子类的输出有点不同,所以重写一下
4.优化业务逻辑,增加工厂类
-
前面Test类里面 -
选择和创建对象其实可以划分到两块 -
添加PizzaStore类 package com.practice.test001;
import java.util.Scanner;
public class PizzaStore {
public static Pizza getPizza(int choice) {
Scanner scan = new Scanner(System.in);
Pizza p = null;
switch (choice) {
case 1: {
System.out.print("请输入培根的克数:");
int weight = scan.nextInt();
System.out.print("请输入匹萨的大小:");
int size = scan.nextInt();
System.out.print("请输入披萨的价格:");
int price = scan.nextInt();
BaconPizza bp = new BaconPizza("培根披萨", size, price, weight);
p = bp;
}
break;
case 2: {
System.out.print("请输入你想要加入的水果:");
String ingredient = scan.next();
System.out.print("请输入匹萨的大小:");
int size = scan.nextInt();
System.out.print("请输入披萨的价格:");
int price = scan.nextInt();
FruitPizza fp = new FruitPizza("水果披萨", size, price, ingredient);
p = fp;
}
break;
}
return p;
}
}
-
修改Test类 此时Test类中的main方法更加清晰了 package com.practice.test001;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请选择你想要购买的匹萨类型(1.培根披萨 2.水果匹萨)");
int choice = scan.nextInt();
Pizza pizza = PizzaStore.getPizza(choice);
System.out.println(pizza.showPizza());
}
}
三.完整代码
-
Pizza类 package com.practice.test001;
public class Pizza {
private String name;
private int size;
private int price;
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匹萨的大小是 :" + size + "寸,\n匹萨的价格是 :" + price+"元";
}
public Pizza() {
}
public Pizza(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
}
-
BaconPizza子类 package com.practice.test001;
public class BaconPizza extends Pizza {
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String showPizza(){
return super.showPizza()+"\n培根的克数是:"+weight+"克";
}
public BaconPizza() {
}
public BaconPizza(String name, int size, int price, int weight) {
super(name, size, price);
this.weight = weight;
}
}
-
FruitPizza子类 package com.practice.test001;
public class FruitPizza extends Pizza {
private String ingredient;
public String getIngredient() {
return ingredient;
}
public void setIngredient(String ingredient){
this.ingredient = ingredient;
}
@Override
public String showPizza(){
return super.showPizza()+"\n加入的配料是:"+ingredient+".";
}
public FruitPizza() {
}
public FruitPizza(String name, int size, int price, String ingredient) {
super(name, size, price);
this.ingredient = ingredient;
}
}
-
Test类 package com.practice.test001;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请选择你想要购买的匹萨类型(1.培根披萨 2.水果匹萨)");
int choice = scan.nextInt();
Pizza pizza = PizzaStore.getPizza(choice);
System.out.println(pizza.showPizza());
}
}
-
PizzaStore类 package com.practice.test001;
import java.util.Scanner;
public class PizzaStore {
public static Pizza getPizza(int choice) {
Scanner scan = new Scanner(System.in);
Pizza p = null;
switch (choice) {
case 1: {
System.out.print("请输入培根的克数:");
int weight = scan.nextInt();
System.out.print("请输入匹萨的大小:");
int size = scan.nextInt();
System.out.print("请输入披萨的价格:");
int price = scan.nextInt();
BaconPizza bp = new BaconPizza("培根披萨", size, price, weight);
p = bp;
}
break;
case 2: {
System.out.print("请输入你想要加入的水果:");
String ingredient = scan.next();
System.out.print("请输入匹萨的大小:");
int size = scan.nextInt();
System.out.print("请输入披萨的价格:");
int price = scan.nextInt();
FruitPizza fp = new FruitPizza("水果披萨", size, price, ingredient);
p = fp;
}
break;
}
return p;
}
}
四.基础知识补充(用到啥学啥)
1.scanner类
java.util.Scanner 是 Java5 的新特征,我们可以通过 Scanner 类来获取用户的输入
-
创建Scanner 对象的基本语法 Scanner s = new Scanner(System.in);
-
先来看一个简单的输入示例 package com.scanner.test001;
import java.util.Scanner;
public class TestScann {
public static void main(String[] args) {
int age;
double weight;
Scanner scan = new Scanner(System.in);
System.out.print("yours age:");
age = scan.nextInt();
System.out.print("yours weight:");
weight = scan.nextDouble();
System.out.println("age:"+age+";weight:"+weight);
}
}
直观感受了下,也大概指导scan类的那几个方法是啥意思 nextInt() 应该就是接收整数 nextDouble 是接收小数
现在去runoob 上看看
next方法和nextLine方法
next方法
package com.scanner.test001;
import java.util.Scanner;
public class TestScann {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("next方式接收:");
if (scan.hasNext()) {
String str1 = scan.next();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
nextLine方法
package com.scanner.test001;
import java.util.Scanner;
public class TestScann {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("nextLine方式接收:");
if (scan.hasNextLine()) {
String str1 = scan.nextLine();
System.out.println("输入的数据为:" + str1);
}
scan.close();
}
}
俩方法的区别
-
next()
- 读到有效字符后才结束输入
- 有效字符前面的空白,next()方法会自动将其去掉
- 只有输入有效字符后才将其后面输入的空白作为分隔符或结束符
- 不能得到带空格的字符串
-
nextLine()
- 以Enter为结束符(返回输入回车之前的所有字符)
- 可获得空白
再来个实例
总结一下
package com.scanner.test001;
import java.util.Scanner;
public class AddTest {
public static void main(String[] args){
System.out.println("请输入数字:");
Scanner scan = new Scanner(System.in);
double sum = 0;
int m = 0;
while(scan.hasNextDouble()) {
double x = scan.nextDouble();
m = m + 1;
sum = sum + x;
}
System.out.println(m+"个数和为:"+sum);
System.out.println(m+"个数的平均值为:" + (sum/m));
scan.close();
}
}
|