当前位置 : 主页 > 编程语言 > java >

javaHomework/CocaCola.java

来源:互联网 收集:自由互联 发布时间:2021-06-28
CocaCola.java package Drink;import java.time.LocalDate;import java.util.*;/*public class Drink {private String id;private String name;private double price;private double capacity;//容量private LocalDate productionDate;//生产日期*/publ
CocaCola.java
package Drink;
import java.time.LocalDate;
import java.util.*;

/*public class Drink {
	private String id;
	private String name;
	private double price;
	private double capacity;//容量
	private LocalDate productionDate;//生产日期
	*/

public class CocaCola extends Drink{
	private String color;//cocacola自己独立的成员参数
	private int sweetness;//甜度分1-5
	
	public String getColor(){
		return this.color;
	}
	public void setColor(String color){
		this.color=color;
	}
	public int getSweetness(){
		return this.sweetness;
	}
	public void setSweetness(int sweetness){
		if(sweetness>=1&&sweetness<=5){
			this.sweetness=sweetness;
		}else{//需要处理>5的甜度输入
			throw new DrinkException("甜度",sweetness);
		}
	}

	//下面书写构造方法
	public CocaCola(){}//无参构造方法,方便继承
	public CocaCola(String name,double price){
		super(name,price);//调用父类构造方法一
	}

	public CocaCola(String id,String name,double price,int capacity,LocalDate day){
		super(id,name,price,capacity,day);//调用父类构造方法二
	}
	public CocaCola(String id,String name,double price,int capacity,LocalDate day,String color,int sweetness){
		this( id,name, price, capacity,day);
		this.color=color;
		this.sweetness=sweetness;
		
	}

	/*
	 * public void disDrink() {
		System.out.println("条形码:"+this.getId());
		System.out.println("名称:"+this.getname());
		System.out.println("价格:"+this.getPrice());
		System.out.println("容量"+this.getCapacity());
	}
	*/
	public void disDrink(){//重写父类的输出对象信息
		super.disDrink();
		System.out.println("颜色:"+this.getColor());
		System.out.println("甜度(1-5):"+this.getSweetness());
	}
	public void inputDrink(Scanner sc){//重写父类设置对象参数
		super.inputDrink(sc);
		System.out.print("颜色:");
		//this.setColor(sc.next());
		this.setColor(sc.next());
		System.out.print("甜度(1-5):");
		//this.setSweetness(Commodity.inputInt(sc));
		try {
			this.setSweetness(Commodity.inputInt(sc));
		}catch(DrinkException se) {
			System.out.println("甜度输入大于默认最大值5,系统自动设置为默认值:3");
			this.setSweetness(3);
		}
		/*甜度输入抛错,    <0 || >5, Drink.DrinkException: 甜度异常
        字母 ,NumberFormatException
         符号,NumberFormatException*/
		
	}
/*
	public void disCocaCola(){//子类自己的输出对象信息
		System.out.println("条形码:"+this.getId());
		System.out.println("名称:"+this.getname());
		System.out.println("价格:"+this.getPrice());
		System.out.println("容量"+this.getCapacity());
		System.out.println("颜色:"+this.getColor());
	}
	public void inputCocaCola(Scanner sc){//子类自己的设置对象参数
		System.out.print("条形码:");
		this.setId(sc.next());
		System.out.print("名称:");
		this.setName(sc.next());
		System.out.print("价格:");
		this.setPrice(sc.nextDouble());
		System.out.print("容量:");
		this.setCapacity(sc.nextInt());
		System.out.print("颜色:");
		this.setColor(sc.next());
	}
*/
	public void modifyDrink(Scanner sc) {//重写父类的modify函数
		//sc.nextLine();//此行代码目的死消除缓冲去现有空字符对当前输入的影响    喵喵喵喵??
		super.modifyDrink(sc);
		String stringIn;
		System.out.print("颜色为:"+this.color+"\t修改为:");
		stringIn=sc.nextLine();
		if(!stringIn.equals(""))//与空字符比较对应上面直接回车不修改
			this.setColor(stringIn);
		System.out.print("甜度为:(1-5)"+this.sweetness+"\t修改为(1-5):");
		stringIn=sc.nextLine();
		if(!stringIn.equals("")) {
			try{//与空字符比较对应上面直接回车不修改
			this.setSweetness(Integer.parseInt(stringIn));
			}catch(DrinkException se) {
				System.out.println("甜度输入非法,系统不予修改");
			}catch(NumberFormatException se) {
				System.out.println("甜度输入非法,系统不予修改");
			}
		}
		/*甜度输入抛错,    <0 || >5, Drink.DrinkException: 甜度异常
        字母 ,NumberFormatException
         符号,NumberFormatException*/
		
	}

	/*public class Drink {
	private String id;
	private String name;
	private double price;
	private int capacity;容量
	private LocalDate productionDate;//生产日期
	private String color;
	private int sweetness;//甜度分1-5
	*/
	public String toString(){//没看懂这段代码在干嘛
		return "条形码:"+this.getId()+" 名称:"+this.getname()
		+" 价格:"+Double.toString(this.getPrice())+" 容量:"+
		Integer.toString(this.getCapacity())+" 颜色:"+this.color;
	}
	
	
	public String getResult(String course) {
		if(course.equals("颜色")) {
			return this.color;
		}else {
			return super.getResult(course);
		}
	}
}
网友评论