package com.heima.study;
import java.util.Random;
import java.util.Scanner;
public class 点招6 {
/**
* 题目:定义一个抽象类,类名是Payment,定义抽象方法,有参,返回值类型为double,定义一个类Zfb继承抽象类,随机给价格打5-8折,
* 定义一个Cash类继承Payment
* ,定义一个Person类,创建静态方法,参数为String,键盘录入支付方式和金额,如果是Zfb就输出支付方式和折扣后的价格
* ,如果是现金就输出支付方式和金额
*/
public static void main(String[] args) {
Scanner sc = new Scanner(file:///C:\Users\haier\AppData\Local\Temp\%W@GJ$ACOF(TYDYECOKVDYB.pngSystem.in);
System.out.println("请输入付款方式,以及金额");
String s1 = sc.nextLine();
String[] arr = s1.split(",");
double x = Integer.parseInt(arr[1]);
Person p = new Person();
Payment p1 = p.buy(arr[0]);
double b = p1.pay(x);
if (arr[0].equals("Zfb")) {
System.out.println("支付方式:" + arr[0] +"\r\n"+"打"+b/x +"折"+"\r\n"+ "折后价格:"+ b);
}else {
System.out.println("支付方式:" + arr[0]+ "支付金额:"+b);
}
}
}
abstract class Payment {// 定义抽象类
public abstract double pay(double money);
}
class Zfb extends Payment {
Random r = new Random();
public double pay(double money) {
return money * (r.nextInt(4) + 5) * 0.1;
}
}
class Cash extends Payment {
public double pay(double money) {
return money;
}
}
class Person {
public static Payment buy(String s) {
if (s.equals("Zfb")) {
return new Zfb();
} else {
return new Cash();
}
}
}
|
|