7.1 Java(农夫果园【2】:一个农场,专门种植销售各类水果,在这个系统中需要描述下列水果葡萄、草莓、苹果)

    技术2022-07-11  79

    【练习】

    题目要求: 项目主题: 农夫果园 一个农场,专门种植销售各类水果,在这个系统中需要描述下列水果: 葡萄:Grape 草莓:Strawberry 苹果:Apple 水果与其他的植物有很大的不同,水果最终是可以采摘食用的。 那么一个自然的做法就是建立一个各种水果都适用的接口,以便与农场里的其他植物区分开。 水果接口规定出所有的水果都必须实现的接口,包括任何水果必须具备的方法: 种植 plant(),生长 grow() ,收获 harvest() Apple类是水果中的一种,因此它实现了水果接口所声明的所有方法。另外,由于苹果是多年生植 物,因此多出一个treeAge性质,描述苹果树的树龄。Grape 类是水果类的一种,也实现Fruit接口中所声明的所有方法。 但由于葡萄分为有籽和无籽的两种,因此比通常的水果多出一个seedless 性质。 Strawberry类也是水果的一种,也实现了Fruit接口。 农场的市场调查员也是系统的一部分,也需要一个类代表,这个类是MarketInquirer, 它通过inquire()调查今年市场上哪一种水果热销。 public class MarketInquirer extends Fruit_super { static int a=0,g=0,s=0; static String max; public MarketInquirer(double season, double price, double calories) { super(); //季节 if(season>=2&&season<=6) { s ++; } if(season>=7&&season<=8) { a ++; } if(season>8&&season<=10) { g ++; } //价格 if(price>8.97&&price<=20.49) { s ++; } if(price>=1.76&&price<=3.5) { a ++; } if(price>3.5&&price<=8.97) { g ++; } //卡路里 if(calories>=32&&calories<=45) { s ++; } if(calories>45&&calories<=52) { g ++; } if(calories>=53&&calories<=57) { a ++; } if(a < g && s < g) { max = "葡萄"; System.out.println("据调查分析今年市场上水果"+max+"热销。"); } else if(a < s && g < s ) { max = "草莓"; System.out.println("据调查分析今年市场上水果"+max+"热销。"); } else { max = "苹果"; System.out.println("据调查分析今年市场上水果"+max+"热销。"); } } public MarketInquirer() { System.out.println("---------------------------------------------------------"); System.out.println("欢迎来到ASGIRLS农场总部"); System.out.println("老板:今年热销水果是什么?"); } }
    Processed: 0.010, SQL: 9