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

具体:1、建立一个Java抽象类Drink,,2、建立Drink的具体子类3、建立异常类DrinkNotFoundException4、建立Test测试类,测试以上内容的... 具体:1、建立一个Java抽象类Drink,,2、建立Drink的具体子类
3、建立异常类DrinkNotFoundException
4、建立Test测试类,测试以上内容的正确性
5、编译程序,并运行
展开
 我来答
mrchr
2011-09-14 · TA获得超过411个赞
知道小有建树答主
回答量:156
采纳率:0%
帮助的人:150万
展开全部
  import 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<Drink> drinks = new ArrayList<Drink>();
  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<Drink> 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<Drink> 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();
  }
  }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式