求解各位高手:用java解决品尝饮料问题
具体:1、建立一个Java抽象类Drink,,2、建立Drink的具体子类3、建立异常类DrinkNotFoundException4、建立Test测试类,测试以上内容的...
具体:1、建立一个Java抽象类Drink,,2、建立Drink的具体子类
3、建立异常类DrinkNotFoundException
4、建立Test测试类,测试以上内容的正确性
5、编译程序,并运行 展开
3、建立异常类DrinkNotFoundException
4、建立Test测试类,测试以上内容的正确性
5、编译程序,并运行 展开
展开全部
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();
}
}
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |