前言
内部购物劵:手机专卖店为了促销自己的产品,决定发现内部购物劵,但是其他商场不能发行该购物劵;编写一个MobileShop类(模拟手机店);该类中有个名字为InnerPurchaseMoney的内部类(模拟内部购物劵)。
代码
package Dome01;
public class MobileShop {
InnerPurchaseMoney IPM = new InnerPurchaseMoney();
InnerPhoneMoney IPN = new InnerPhoneMoney();
public String phoneShop; //卖手机的商店
public void setPhoneShop(String phoneShop) {
this.phoneShop = phoneShop;
}
public String getPhoneShop() {
return phoneShop;
}
class InnerPurchaseMoney{ //售价
double discountCoupon = 0; //优惠券
public void setDiscountCoupon(double discountCoupon) {
this.discountCoupon = discountCoupon;
}
public double getDiscountCoupon() {
return discountCoupon;
}
}
class InnerPhoneMoney{
private int phoneMoney; //每个手机店里的手机的定价
public int getPhoneMoney() {
return phoneMoney;
}
public void setPhoneMoney(int phoneMoney) {
this.phoneMoney = phoneMoney;
}
}
}
代码解释:
package Dome01;
这个就是代表这个文件在包里面
InnerPurchaseMoney IPM = new InnerPurchaseMoney();
InnerPhoneMoney IPN = new InnerPhoneMoney();
这个就是对象
public void setPhoneShop(String phoneShop) {
this.phoneShop = phoneShop;
}
public String getPhoneShop() {
return phoneShop;
}
这里就是得到phoneShop也就是手机店的名字,在其他类使用的话,就使用get方法
其实也可以写在一个方法里,但为了标准化,这个就先不说了
class InnerPurchaseMoney{ //售价
double discountCoupon = 0; //优惠券
public void setDiscountCoupon(double discountCoupon) {
this.discountCoupon = discountCoupon;
}
public double getDiscountCoupon() {
return discountCoupon;
}
}
这个就是内部类InnerPurchaseMoney,获得优惠券
类名懒得修改了
class InnerPhoneMoney{
private int phoneMoney; //每个手机店里的手机的定价
public int getPhoneMoney() {
return phoneMoney;
}
public void setPhoneMoney(int phoneMoney) {
this.phoneMoney = phoneMoney;
}
这个就是另一个内部类InnerPhoneMoney,获得手机售价的
接下来就是主函数了,我稍微玩了一下
package Dome01;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws InterruptedException {
MobileShop mobileShop = new MobileShop();
MobileShop.InnerPhoneMoney innerPhoneMoney = mobileShop.new InnerPhoneMoney();
MobileShop.InnerPurchaseMoney innerPurchaseMoney = mobileShop.new InnerPurchaseMoney();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你想要去的手机店(只有华为、苹果、小米):\n");
String phoneShopName = scanner.next();
mobileShop.setPhoneShop(phoneShopName);
if(Objects.equals(mobileShop.getPhoneShop(), "华为"))
{ innerPhoneMoney.setPhoneMoney(5000);
System.out.println("该手机店的手机定价为:" + innerPhoneMoney.getPhoneMoney());
} else if (Objects.equals(mobileShop.getPhoneShop(), "小米")) {
innerPhoneMoney.setPhoneMoney(1999);
System.out.println("该手机店的手机定价为:" + innerPhoneMoney.getPhoneMoney());
} else if (Objects.equals(mobileShop.getPhoneShop(), "苹果")) {
innerPhoneMoney.setPhoneMoney(9000);
System.out.println("该手机店的手机定价为:" + innerPhoneMoney.getPhoneMoney());
}
else {
System.out.println("你输错了,哈哈哈哈哈");
}
System.out.println("你猜猜他有没有优惠券???^-^");
String discountCouponJudge = scanner.next();
if(Objects.equals(discountCouponJudge, "有"))
{
if (Objects.equals(mobileShop.getPhoneShop(), "华为") || Objects.equals(mobileShop.getPhoneShop(), "苹果"))
{
System.out.println("sorry,没有,略略略");
}
else if(Objects.equals(mobileShop.getPhoneShop(), "小米"))
{
System.out.println("哈哈,有\n");
TimeUnit.SECONDS.sleep(1);
System.out.println("3\n");
TimeUnit.SECONDS.sleep(1);
System.out.println("2\n");
TimeUnit.SECONDS.sleep(1);
System.out.println("1\n");
System.out.println("恭喜你,您是本店的第37位顾客!!!\n");
System.out.println("这是您的优惠券。");
System.out.println("7折");
innerPurchaseMoney.setDiscountCoupon(0.7);
System.out.println("这台手机只需要" + innerPurchaseMoney.getDiscountCoupon() * innerPhoneMoney.getPhoneMoney());
}
}
else
{
if (Objects.equals(mobileShop.getPhoneShop(), "华为") || Objects.equals(mobileShop.getPhoneShop(), "苹果"))
{
System.out.println("对!他就是没有");
}
else if (Objects.equals(mobileShop.getPhoneShop(), "小米"))
{
System.out.println("猜错了,不信就没有了");
}
}
}
}
这个就不解释了,不是很难
这个还是挺有意思,推荐自己运行一下