JAVA_继承演示04_FoodManagerDeamo.java____补充

/**
 实现一个超市的食品管理
 1.定义一个食品类(Food)
 2.定义一个食品管理类(FoodManager)
	(1)实现进货功能
	(2)可以输出所有的食品信息功能
 3.使用继承实现一个可以按单价排序输出所有食品的功能
 4.使用继承实现一个只输出进口食品的功能
 */
 import java.util.Arrays;
public class FoodManagerDeamo{
	public static void main(String[] args){
		/*
		FoodManager fm = new FoodManager();
		fm.add(new Food("桃李",10));
		fm.add(new Food("樱花",8));
		fm.add(new Food("小熊",12));
		fm.add(new Food("美卡",15));
		
		fm.printInfo();
		*/
		
		/*
		SortFoodManager fm = new SortFoodManager();
		fm.add(new Food("桃李",10));
		fm.add(new Food("樱花",8));
		fm.add(new Food("小熊",12));
		fm.add(new Food("美卡",15));
		
		fm.printInfo();
		*/
		
		ImportFoodManager fm = new ImportFoodManager();
		fm.add(new Food("桃李",10,"国产"));
		fm.add(new Food("樱花",8,"进口"));
		fm.add(new Food("小熊",12,"国产"));
		fm.add(new Food("美卡",15,"进口"));
		
		fm.printInfo();
		
		
	}
}

//4.使用继承实现一个只输出进口食品的功能
//写法1
/*
class ImportFoodManager extends FoodManager{
	public void printInfo(){
		for(int i=0;i<super.count;i++){
			if(super.foods[i].getType().equals("进口")){
				System.out.println(foods[i].geInfo());
			}
		}
	}
}
*/
//4.使用继承实现一个只输出进口食品的功能
//写法2 推荐写法2
class ImportFoodManager extends FoodManager{
	public void printInfo(){
		String type =null;
		for(int i=0;i<super.count;i++){
			type = super.foods[i].getType();
			if(type.equals("进口")){
				System.out.println(foods[i].geInfo());
			}
		}
	}
}


//子类 
//3.使用继承实现一个可以按单价排序输出所有食品的功能
class SortFoodManager extends FoodManager{
	//重写父类的打印方法,实现排序功能
	public void printInfo(){
		//通过super把foods[]数组赋值给fs[]
		
		
		//Food[] fs = super.foods;
		Food[] fs = new Food[super.count];
		for(int i=0;i<fs.length;i++){
			fs[i] = super.foods[i];
		}
		
		
		//冒泡排序
		Food temp = null;
		for(int i=0;i<fs.length-1;i++){
			for(int j=0;j<fs.length-1-i;j++){
				if(fs[j].getPrice()>fs[j+1].getPrice()){
					temp = fs[j];
					fs[j] = fs[j+1];
					fs[j+1] = temp;
				}
			}
		}
		//
		for(int i=0;i<count;i++){
			System.out.println(fs[i].geInfo());
		}
	}
}

//类 食品管理类
//2.定义一个食品管理类(FoodManager)
class FoodManager{
	protected Food[] foods = new Food[3];
	protected int count = 0;
	
	//添加食品
	public void add(Food f){
		if(count>=foods.length){
			int newLen = foods.length*3/2+1;
			//import java.util.Arrays;
			foods = Arrays.copyOf(foods,newLen);
		}
		foods[count] = f;
		count++;
	}
		
	//输出所有食品
	public void printInfo(){
		for(int i=0;i<count;i++){
			System.out.println(foods[i].geInfo());
		}
	}
}
	
//类 食品类
// 1.定义一个食品类(Food)
class Food{
	private String name;
	private float price;
	
	//4.使用继承实现一个只输出进口食品的功能
	private String type;//国产或进口
	
	public Food(){};
	public Food(String name,float price,String type){
		this.name = name;
		this.price = price;
		this.type = type;
	}
	
	//name
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
	//price
	public void setPrice(float price){
		this.price = price;
	}
	public float getPrice(){
		return price;
	}
	//type
	public void setType(String type){
		this.type = type;
	}
	public String getType(){
		return type;
	}
	
	public String geInfo(){
		return "食品名称:"+name+",价格:"+price+",类型为"+type;
	}
}

你可能感兴趣的:(JAVA_继承演示04_FoodManagerDeamo.java____补充)