池塘问题(Java版)

问题

一个池塘很多种类鱼,鱼吃东西,养大之后卖,卖了可以再买鱼。

前提

代码以 ts小练习(池塘问题)为基础在上面添加自己的想法。在线运行:http://java.jsrun.net/

思路

  1. 定义鱼类Fish,弄几种鱼来继承类:鱼(种类、年龄、卖出价格、买入价格、数量、最大年龄)
     
     

    鳙鱼

    草鱼

    鲫鱼

    买入 15 12 10
    卖出 35 25 20
  2. 定义市场类Macket,有三个方法:预测收益、卖鱼、买鱼。预测收益:作为终止代码运行的方法。
  3. 预测收益方法的具体思路:【在规定月份数内】
    1. 判断鱼塘有没有成熟的鱼,有则卖,没有则跳2

    2. 当余额还能买最便宜的鱼并且鱼塘里还有空位,则买,没有则跳3

    3. 如果预测的月份数还有,则等鱼成熟(自动判断)

  4. 卖鱼方法的具体思路:

    1. 判断鱼是否达到最大年龄,达到则拿去卖,同时更新余额和池塘鱼数。否则跳2

    2. 只更新鱼的年龄

  5. 买鱼方法的具体思路:

    1. 当余额大于10元且鱼塘还有位置,优先买单价高的鱼,跳2。否则什么都不做

    2. 先计算余额能买几条某一种鱼,且判断鱼塘有没有这么多位置,有则全买,没有则按位置剩余数买。同时更新余额和池塘鱼数

  6. 买卖鱼时,用ArrayList来存储鱼。默认是买鱼时会添加相关鱼的实例,卖鱼时会遍历这个动态数组,对动态数组进行操作

可拓展

  1. 养鱼加一个喂饲料的需求,饲料也有不同的价格。那对应的效果是不是也不同?
  2. 买鱼的算法还可以怎么优化?
  3. 代码还能优化吗?比如对鱼的买入价格进行排序,而不是手动定义

Java

package test;
import java.util.ArrayList;
import java.util.List;
public class Market {
 public static Integer sum=132; // 口袋里面的钱(在线java做不了交互,先写死)
 public static Integer fishNum=0; // 目前池塘的鱼数
 public static Integer fishMaxNum=10; // 池塘最大容纳鱼数
 public static Integer monthTemp = 1;// 用于输出
 public static Integer months = 10;//想要预测的月份数
 protected static ArrayList arr;

    public static void main(String []args) {
        System.out.println("目前有:"+sum+"元");

        arr=new ArrayList<>();
        
        expect(months); // 预测n月后的收益
        System.out.println("----------------------");
        System.out.print(months+"个月后余额:");
        System.out.println(sum);
        System.out.print("池塘还剩下的鱼的数量:");
        System.out.print(fishNum); 
    }

    public static void expect(Integer n){
        while(n-- > 0){
            // 卖鱼
            sell();

            // 买鱼(这个算法经常只能买到鳙鱼...)
            while(sum >= 10 && fishNum != fishMaxNum){
                if(sum >= 15){
                    // 能买15元的鱼
                    Bighead fish = new Bighead(null, 0, null, null, 0, null);
                    purchase(fish);
                }else if(sum >= 12){
                    // 能买12元的鱼
                    GrassCarp fish = new GrassCarp(null, 0, null, null, 0, null);
                    purchase(fish);
                }else{
                    // 能买10元的鱼
                    CrucianCarp fish = new CrucianCarp(null, 0, null, null, 0, null);
                    purchase(fish);
                }
            }
        }
    }

    // 卖鱼的函数
    public static void sell(){
        Integer money = 0;
        // 初始化时arr的长度为0
        for(int i = 0; i < arr.size(); i++){
            Fish obj=((Fish) arr.get(i));
            if(obj.getMaxAge() <= obj.getAge()){
                money = obj.getSellPrice() * obj.getNum();
                sum += money;
                arr.remove(i);
                i--;
                fishNum -= obj.getNum();
            }else{
                obj.SetAge();
            }
        }
        System.out.println("----------------------");
        System.out.println("第" + monthTemp + "个月卖出了 " + money + " 元");
        monthTemp++;
    }

    // 买鱼函数
    public static void purchase(Fish fish){// 这个参数是父类对象,是复用买鱼函数的关键?
        Integer count;
        count = (sum/fish.getPurcPrice() - (fishMaxNum-fishNum)) > 0 ? (fishMaxNum-fishNum) : (sum/fish.getPurcPrice());
        System.out.println(fish.getName()+"买了"+count+"条");
        fish.setNum(count);
        sum -= count * fish.getPurcPrice();
        arr.add(fish);
        fishNum += count;
    }
}

// 抽象类
abstract class Fish{
    private String name;
    private Integer sellPrice; //卖出价格
    private Integer purcPrice; //买入价格
    private Integer age;
    private Integer num; // 鱼的数量
    private Integer maxAge; // 几个月
    // 构造器,如js的construtor
    public Fish(String name, Integer age, Integer sellPrice,
        Integer purcPrice, Integer num, Integer maxAge) {
        this.name = name;
        this.age = age;
        this.sellPrice = sellPrice;
        this.purcPrice = purcPrice;
        this.num = num;
        this.maxAge = maxAge;
    }

    // 获取鱼的年龄
    public final Integer getAge() {
        return age;
    }

    // 获取鱼的成熟年龄
    public final Integer getMaxAge() {
        return maxAge;
    }

    // 获取鱼的数量
    public final Integer getNum() {
        return num;
    }

    // 设置鱼的数量
    public final void setNum(Integer num) {
        this.num = num;
    }

    // 过了一个月,年龄增加
    public final void SetAge() {
        age++;
    }

    // 获取鱼的卖出价格
    public final Integer getSellPrice() {
        return sellPrice;
    }

    //  获取鱼的买入价格
    public final Integer getPurcPrice() {
        return purcPrice;
    }

    // 获取鱼的名称
    public final String getName() {
        return name;
    }
}

// 鲫鱼
class CrucianCarp extends Fish {
    public CrucianCarp(String name, Integer age, Integer sellPrice,
        Integer purcPrice, Integer num, Integer maxAge) {
        super(name = "鲫鱼", age, sellPrice = 20, purcPrice = 10, num, maxAge = 4);
    }
}

//草鱼
class GrassCarp extends Fish {
    public GrassCarp(String name, Integer age, Integer sellPrice,
        Integer purcPrice, Integer num, Integer maxAge) {
        super(name = "草鱼", age, sellPrice = 25, purcPrice = 12, num, maxAge = 4);
    }
}

//鳙鱼
class Bighead extends Fish {
    public Bighead(String name, Integer age, Integer sellPrice,
        Integer purcPrice, Integer num, Integer maxAge) {
        super(name = "鳙鱼", age, sellPrice = 35, purcPrice = 15, num, maxAge = 5);
    }
}
 
  

TS

思路

  1. 用readline-sync实现控制台交互
  2. arr:Array指创建一个数组元素是Fish类的数组arr

问题

  1. 用构造函数创建鱼的实例时,不能用null(Java用null),要用undefined
// index.js
var readlineSync = require('readline-sync');
var {Market,CrucianCarp,GrassCarp,Bighead}=require('./fish')

var months = readlineSync.question('how months?');
var fishMaxNum = readlineSync.question('fishMaxNum?');
var sum = readlineSync.question('money?');

var fi=new Market(months, fishMaxNum, sum);

fi.init()
// finish.ts
let fishNum:number = 0; // 鱼的数量

class Market{
    arr:Array = []; // 鱼
    months:number=0;
    fishMaxNum:number=0;
    sum:number=0;
    constructor(months:number,fishMaxNum:number,sum:number){
        this.months=months;
        this.fishMaxNum = fishMaxNum;
        this.sum = sum;
    }
    init(){
        console.log(`初始化:${this.months}月,最大容量为:${this.fishMaxNum},有${this.sum}元`)
        this.expect(this.months)
        console.log(`${this.months}月之后,池塘有${fishNum}条鱼,还有${this.sum}元`)
    }
    // 开始买卖
    expect(m:number):void{
        while(m-- > 0){
            // 卖出鱼
            this.sell();

            // 买鱼
            while (this.sum >= 10 && fishNum != this.fishMaxNum){
                if(this.sum >= 15){
                    let fish:Bighead = new Bighead(undefined, 0, undefined, undefined, 0, undefined);
                    this.purchase(fish);
                }else if(this.sum >= 12){
                    let fish:GrassCarp = new GrassCarp(undefined, 0, undefined, undefined, 0, undefined);
                    this.purchase(fish);
                }else{
                    let fish:CrucianCarp = new CrucianCarp(undefined, 0, undefined, undefined, 0, undefined);
                    this.purchase(fish);
                }
            }
        }
    }
    // 买鱼
    purchase(fish:Fish):void{
        let count:number;
        // console.log(fish.getPurcPrice())
        count = (this.sum/fish.getPurcPrice() - (this.fishMaxNum-fishNum) > 0 ? (this.fishMaxNum-fishNum) : Math.floor(this.sum/fish.getPurcPrice()))
        console.log(`${fish.getName()}买了${count}条`)
        fish.setNum(count)
        this.sum -= count * fish.getPurcPrice();
        this.arr.push(fish)
        fishNum += count
        // console.log(count);
    }
    // 卖鱼
    sell():void{
        let money:number = 0
        for(let i = 0; i < this.arr.length; i++){
            if(this.arr[i].getAge() >= this.arr[i].getMaxAge()){
                money = this.arr[i].getSellPrice() * this.arr[i].getNum()
                this.sum += money
                fishNum -= this.arr[i].getNum()
                this.arr.splice(i,1)
                i--
            }else{
                this.arr[i].setAge()
            }
        }
        console.log("--------------------")
        console.log(`这个月卖出了${money}元`)
    }
}

// 抽象类
abstract class Fish{
    private  name:string;
    private  sellPrice:number;
    private  purcPrice:number;
    private  age:number;
    private  num:number; // 鱼的数量
    private  maxAge:number;
    // 构造器
    constructor(name:string,age:number,sellPrice:number,purcPrice:number,num:number,maxAge:number) {
        this.name = name;
        this.age = age;
        this.sellPrice = sellPrice;
        this.purcPrice = purcPrice;
        this.num = num;
        this.maxAge = maxAge;
    }
    // 获取鱼的年龄
    getAge():number {
        return this.age;
    }
    // 获取鱼的成熟年龄
    getMaxAge():number {
        return this.maxAge;
    }
    // 获取鱼的数量
    getNum():number {
        return this.num;
    }
    // 设置鱼的数量
    setNum(num:number):void {
        this.num=num;
    }
    // 过了一个月,年龄增加
    setAge():void {
        this.age++;
    }
    // 获取鱼的卖出价格
    getSellPrice():number {
        return this.sellPrice;
    }
    //获取鱼的买入价格
    getPurcPrice():number {
        return this.purcPrice;
    }
    // 获取鱼的名称
    getName():string {
        return this.name;
    }
}
// crucianCarp
class CrucianCarp extends Fish{
    constructor(name:string='鲫鱼',age:number=0,sellPrice:number=20,purcPrice:number=10,num:number,maxAge:number=4){
        super(name,age,sellPrice,purcPrice,num,maxAge)
    }
}
// grassCarp
class GrassCarp extends Fish{
    constructor(name:string="草鱼",age:number=0,sellPrice:number=25,purcPrice:number=12,num:number,maxAge:number=4){
        super(name,age,sellPrice,purcPrice,num,maxAge)
    }
}
// bighead
class Bighead extends Fish{
    constructor(name:string="鳙鱼",age:number=0,sellPrice:number=35,purcPrice:number=15,num:number,maxAge:number=5){
        super(name,age,sellPrice,purcPrice,num,maxAge)

    }
}
export {Market,Bighead,GrassCarp,CrucianCarp}

 

你可能感兴趣的:(java)