700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java品尝饮料代码_求解各位高手:用java解决品尝饮料问题

java品尝饮料代码_求解各位高手:用java解决品尝饮料问题

时间:2022-04-25 07:27:38

相关推荐

java品尝饮料代码_求解各位高手:用java解决品尝饮料问题

展开全部

e68a84e8a2ad62616964757a686964616f31333264653432import java.util.ArrayList;

import java.util.List;

import javax.swing.JOptionPane;

import javax.swing.SwingUtilities;

public class Question {

class Drink{

protected String type;//苏打,果汁?

protected String name;//名字?

protected double price;//价格

public String getType() {

return type;

}

public double getPrice() {

return price;

}

public String getName() {

return name;

}

}

class Cole extends Drink{

public Cole(){

name = "可口可乐";

type = "苏打";

price = 6.0d;

}

}

class Putaoduo extends Drink{

public Putaoduo(){

name = "葡萄多";

type = "果汁";

price = 3.0d;

}

}

class Person{

private double money;

private List drinks = new ArrayList();

public void buyDrink(Drink drink) throws QianBuGouException, Exception{

if(drink != null){

double price = drink.getPrice();

if(money >= price){

drinks.add(drink);

if(price == 0){

throw new Exception("竟然有免费的饮料,好爽呀!");

}else if(price < 0){

throw new Exception("竟然有倒贴的饮料,好爽呀!");

}

money -= price;

}else{

throw new QianBuGouException();

}

}

}

public void drink(Drink drink) throws DrinkNotFoundException{

if(drinks.contains(drink)){

drinks.remove(drink);

}else{

throw new DrinkNotFoundException("这瓶饮料不是我的!");

}

}

public double getMoney() {

return money;

}

public void setMoney(double money) {

this.money = money;

}

public List getDrinks() {

return drinks;

}

}

class QianBuGouException extends Exception{

public QianBuGouException(){

super("我的钱不够买饮料的啦,再买就破产了!");

}

}

class DrinkNotFoundException extends Exception{

public DrinkNotFoundException(String message){

super(message);

}

}

public Question(){

SwingUtilities.invokeLater(new Runnable() {

public void run() {

Person you = new Person();

you.setMoney(10d);

Cole cole = new Cole();

try{

you.drink(cole);

JOptionPane.showMessageDialog(null, "我喝了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

try{

you.buyDrink(new Cole());

JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

try{

you.drink(new Cole());

JOptionPane.showMessageDialog(null, "我喝了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

try{

you.buyDrink(new Putaoduo());

JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

try{

you.buyDrink(new Putaoduo());

JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

try{

you.buyDrink(new Putaoduo());

JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

try{

you.buyDrink(new Putaoduo());

JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

List drinks = you.getDrinks();

int all = drinks.size();

for(int i = 0; i < all; i ++){

Drink drink = drinks.get(0);

try{

you.drink(drink);

JOptionPane.showMessageDialog(null, "我喝了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);

}catch(Exception e){

JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);

}

}

JOptionPane.showMessageDialog(null, "我还有" + drinks.size() + "瓶饮料。", "提示信息", JOptionPane.ERROR_MESSAGE);

}

});

}

public static void main(String args[]){

new Question();

}

}

本回答由提问者推荐

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。