主题及部分产品如下:
public class Factory {
public class Factory {
private Factory(){}
public static KFC creat(String name){
if (name.equals("汉堡包")) {
return new Humberger();
}
else if (name.equals("可乐")) {
return new Cola();
}
else if (name.equals("薯条")) {
return new Frenchfries();
}
else if (name.equals("鸡翅")) {
return new Chickenwing();
}
else if (name.equals("上校鸡块")) {
return new Chickennuggets();
}
else if (name.equals("豪华全家桶")) {
return new Familybucket();
}else if(name.equals("套餐1")) {
return new TC1();
}else if(name.equals("套餐2")){
return new TC2();
}
else if (name.equals("套餐3")) {
return new TC3();
}else{
return null;
}
}
}
public class KFC {
private String name;
private int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
public class TC1 extends KFC {
private String name = "套餐1:汉堡+可乐+鸡翅";
private int price=20;
@Override
public String getName() {
return name;
}
@Override
public int getPrice() {
return price;
}
}
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class DOS {
private ArrayList<KFC> aa = new ArrayList<>();
private int yhq = 0;
public DOS(){
zhuJieMain();
}
public void zhuJieMain(){
System.out.println("欢迎来到KFC");
System.out.println("1.点餐--------2.领取优惠券-------3.结账");
show();
}
public void jieZhang(){
Scanner sc = new Scanner(System.in);
Collect c = new Collect();
c.setKfc(aa);
c.setYhq(yhq);
c.print();
c.setMoney();
c.printXiaopiao();
}
public void show(){
Scanner sc = new Scanner(System.in);
while(true) {
int x = sc.nextInt();
if(x==1){
diancan();
}
else if(x==2){
youhuiquan();
}else if(x==3){
if(aa.size()==0) {
System.out.println("您还没有买东西,欢迎下次光临");
System.exit(0);
}
else
jieZhang();
}
else{
System.out.println("输入错误");
continue;
}
}
}
public void diancan(){
System.out.println("本店推出以下产品:1.汉堡:10元/个");
System.out.println("本店推出以下产品:2.可乐:6元/杯");
System.out.println("本店推出以下产品:3.薯条:8元/包");
System.out.println("本店推出以下产品:4.鸡翅:7元/对");
System.out.println("本店推出以下产品:5.上校鸡块:10元/份");
System.out.println("本店推出以下产品:6.豪华全家桶:88元/份");
System.out.println("------------7.套餐1:汉堡+可乐+鸡翅:20元/份");
System.out.println("------------8.套餐2:可乐+汉堡+薯条:20元/份");
System.out.println("------------9.套餐3:上校鸡块+薯条+汉堡+鸡翅:30元/份");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
xuanze(x);
gongNeng();
}
private void xuanze(int x) {
KFC a = null;
while (true) {
if (x == 1) {
a = Factory.creat("汉堡包");
}
else if (x == 2) {
a = Factory.creat("可乐");
}
else if (x == 3) {
a = Factory.creat("薯条");
}
else if (x == 4) {
a = Factory.creat("鸡翅");
}
else if (x == 5) {
a = Factory.creat("上校鸡块");
}
else if (x == 6) {
a = Factory.creat("豪华全家桶");
}
else if (x == 7) {
a = Factory.creat("套餐1");
}else if (x == 8) {
a = Factory.creat("套餐2");
} else if (x == 9) {
a = Factory.creat("套餐3");
}
else {
System.out.println("没有该套餐");
}
aa.add(a);
break;
}
}
public void gongNeng() {
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("继续点餐-Y,返回主界面-N");
String s = sc.next();
if (s.equals("N")){
zhuJieMain();
break;
}else if(s.equals("Y")){
diancan();
break;
}
else {
System.out.println("指令错误!");
continue;
}
}
}
public void youhuiquan(){
System.out.println("------1.满50减15,2.满100减40,3.放弃优惠券");
Scanner sc = new Scanner(System.in);
while(true) {
int x = sc.nextInt();
if(x==1){
yhq=50;
System.out.println("你现有优惠券"+yhq+"元");
break;
}else if(x==2){
yhq=100;
System.out.println("你现有优惠券"+yhq+"元");
break;
}else if(x==3) {
yhq=0;
break;
}
else{
System.out.println("没有该操作");
continue;
}
}
System.out.println("返回-Y");
while(true) {
String s = sc.next();
if (s.equals("Y")){
zhuJieMain();
}else{
System.out.println("请输入正确指令");
}
}
}
}
import _111.DOS;
public class Demo {
public static void main(String[]args){
DOS a= new DOS();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Collect {
private ArrayList<KFC> aa;
private int money;
private int yhq=0;
public Collect(){
}
public void setKfc(ArrayList<KFC> aa) {
this.aa = aa;
}
public void setYhq(int yhq) {
this.yhq = yhq;
}
public void setMoney() {
Scanner sc=new Scanner(System.in);
int sum=0;
for(KFC kfc:aa){
sum+=kfc.getPrice();
}
while(true) {
System.out.println("请输入支付金额:");
money = sc.nextInt();
if(money<sum) {
System.out.println("你的钱好像不够....");
continue;
}
else
break;
}
}
public int judge(){
int i=0;
int sum=0;
for(KFC kfc:aa){
sum+=kfc.getPrice();
}
if(sum>=100){
if(yhq==50) {
i = money-sum + 15;
}else if(yhq==100) {
i = money-sum +40;
}else{
i = money-sum;
}
}
else if(sum>=50){
if(yhq==50) {
i = money-sum + 20;
}else{
i = money-sum;
}
}
else{
i=money-sum;
}
if(i<=0) {
i=0;
}
return i;
}
public void print(){
System.out.println("本次消费如下:");
int sum=0;
StringBuffer s = new StringBuffer();
for(KFC kfc : aa){
sum+=kfc.getPrice();
if(aa.indexOf(kfc)!=aa.size()-1) {
s.append(kfc.getName() + ",");
}else{
s.append(kfc.getName());
}
}
System.out.println("商品:"+s);
System.out.println("共消费"+sum+"元");
}
public void printXiaopiao(){
System.out.println("小票情况如下:");
int sum=0;
StringBuffer s = new StringBuffer();
for(KFC kfc : aa){
sum+=kfc.getPrice();
if(aa.indexOf(kfc)!=aa.size()-1) {
s.append(kfc.getName() + ",");
}else{
s.append(kfc.getName());
}
}
System.out.println("您支付了"+money+"元");
if(sum>=100&&yhq!=0){
System.out.println("您使用了优惠券满100减40");
}
else if(sum>=50&&yhq!=0){
System.out.println("您使用了优惠券满50减15");
}
else{
System.out.println("您没有使用优惠券");
}
System.out.println("找零" +judge()+ "元");
}
}