Java实现计算饮料价格GUI版(入门)

287 阅读1分钟

对于该系统,主要是针对购买咖啡和奶茶时,添加不同的配料的价格计算,其运行结果如下图

该实现一个Coffee类,里面包含各种饮料和配料

package cn.edu.shengda;


//饮料
abstract class Beverage{
    String description="Unknow Beverage";
    public abstract  int cost();
    public String getDescription() {
        return description;
    }
}

//配料
abstract class CondimentDecorator extends Beverage{
    Beverage beverage;
}

//蒸馏咖啡
class Espresso extends Beverage{
    private final int ESPRESSO_PRICE=25;
    public Espresso(){ description="奶茶";}
    public int cost(){return ESPRESSO_PRICE;}
}

//深度烘培咖啡
class DarkRoast extends Beverage{
    private final int DARKROAST_PRICE=20;
    public DarkRoast(){description="咖啡";}
    public int cost(){return DARKROAST_PRICE;}
}

//摩卡
class Mocha extends CondimentDecorator{
    private final int MOCHA_PRICE=10;
    public Mocha(Beverage beverage){
        this.beverage=beverage;
    }
    public String getDescription(){
        return beverage.getDescription()+", 摩卡";
    }
    public int cost(){
        return MOCHA_PRICE + beverage.cost();
    }
}

//珍珠
class Pearl extends CondimentDecorator{
    private final int PEARL_PRICE=15;
    public Pearl(Beverage beverage){
        this.beverage=beverage;
    }
    public String getDescription(){
        return beverage.getDescription()+", 珍珠";
    }
    public int cost(){
        return PEARL_PRICE + beverage.cost();
    }
}

//巧克力
class Chocolate extends CondimentDecorator{
    private final int CHOCOLATE_PRICE=15;
    public Chocolate(Beverage beverage){
        this.beverage=beverage;
    }
    public String getDescription(){
        return beverage.getDescription()+", 巧克力";
    }
    public int cost(){
        return CHOCOLATE_PRICE + beverage.cost();
    }
}

//奶泡
class Whip extends CondimentDecorator{
    private final int WHIP_PRICE=8;
    public Whip(Beverage beverage){
        this.beverage=beverage;
    }
    public String getDescription(){
        return beverage.getDescription()+", 奶泡";
    }
    public int cost(){
        return WHIP_PRICE + beverage.cost();
    }
}

一个是主程序CoffeeFrame类,实现了界面

package cn.edu.shengda;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class CoffeeFrame extends JFrame implements ActionListener {
    public static void main(String[] args){
        CoffeeFrame coffeeFrame =new CoffeeFrame();
        coffeeFrame.setLayout(new FlowLayout());
    }

    CoffeeFrame(){
        init();
        this.setTitle("咖啡");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(0,0,500,350);
        this.setVisible(true);
    }
    public void init(){
        radioButtonCoffe=new JRadioButton("咖啡 20");
        radioButtonCoffe.setSelected(true);
        radioButtonMilkTea=new JRadioButton("奶茶 25");
        checkBoxMocha=new JCheckBox("摩卡 10");
        checkBoxPearl=new JCheckBox("珍珠 15");
        checkBoxWhip=new JCheckBox("奶泡 8");
        checkBoxChocolate=new JCheckBox("巧克力 20");
        button = new JButton("计算");
        labelPrice=new JLabel("价格:");
        sumPrice = new JLabel("咖啡¥20");
        radioButtonCoffe.addActionListener(this);
        radioButtonMilkTea.addActionListener(this);
        checkBoxMocha.addActionListener(this);
        checkBoxPearl.addActionListener(this);
        checkBoxWhip.addActionListener(this);
        checkBoxChocolate.addActionListener(this);
        button.addActionListener(this);
        add(radioButtonCoffe);
        add(radioButtonMilkTea);
        add(checkBoxMocha);
        add(checkBoxPearl);
        add(checkBoxWhip);
        add(checkBoxChocolate);
        add(button);
        add(labelPrice);
        add(sumPrice);
        group=new ButtonGroup();
        group.add(radioButtonCoffe);
        group.add(radioButtonMilkTea);
    }
    JRadioButton radioButtonCoffe;
    JRadioButton radioButtonMilkTea;
    JCheckBox checkBoxMocha;
    JCheckBox checkBoxPearl;
    JCheckBox checkBoxWhip;
    JCheckBox checkBoxChocolate;
    JLabel labelPrice;
    ButtonGroup group;
    Beverage beverage;
    JButton button;
    JLabel sumPrice;
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(button)) {
            beverage=new DarkRoast();
            if(radioButtonCoffe.isSelected()){
                beverage=new DarkRoast();
            }
            if(radioButtonMilkTea.isSelected()){
                beverage=new Espresso();
            }
            if(checkBoxMocha.isSelected()) {
                beverage=new Mocha(beverage);
            }
            if(checkBoxPearl.isSelected()){

                beverage=new Pearl(beverage);
            }
            if(checkBoxWhip.isSelected()){

                beverage=new Whip(beverage);
            }
            if(checkBoxChocolate.isSelected()){
                beverage=new Chocolate(beverage);
            }
            sumPrice.setText(beverage.getDescription()+"¥"+Integer.toString(beverage.cost()));
        }
    }
}