一个池塘很多种类鱼,鱼吃东西,养大之后卖,卖了可以再买鱼。
代码以 ts小练习(池塘问题)为基础在上面添加自己的想法。在线运行:http://java.jsrun.net/
鳙鱼 |
草鱼 |
鲫鱼 |
|
买入 | 15 | 12 | 10 |
卖出 | 35 | 25 | 20 |
判断鱼塘有没有成熟的鱼,有则卖,没有则跳2
当余额还能买最便宜的鱼并且鱼塘里还有空位,则买,没有则跳3
如果预测的月份数还有,则等鱼成熟(自动判断)
卖鱼方法的具体思路:
判断鱼是否达到最大年龄,达到则拿去卖,同时更新余额和池塘鱼数。否则跳2
只更新鱼的年龄
买鱼方法的具体思路:
当余额大于10元且鱼塘还有位置,优先买单价高的鱼,跳2。否则什么都不做
先计算余额能买几条某一种鱼,且判断鱼塘有没有这么多位置,有则全买,没有则按位置剩余数买。同时更新余额和池塘鱼数
买卖鱼时,用ArrayList来存储鱼类。默认是买鱼时会添加相关鱼的实例,卖鱼时会遍历这个动态数组,对动态数组进行操作
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
// 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}