What is the best way to structure a group of variables that have a group of variables that have a group of variables and so on?

huangapple go评论65阅读模式
英文:

What is the best way to structure a group of variables that have a group of variables that have a group of variables and so on?

问题

我正在创建一个Java程序,用于计算费率。费率有一个季节,每个季节都有高峰期,这些高峰期有各自的时间和价格。我考虑过使用嵌套类,但肯定还有其他更好的方法,只是我暂时没有想到而已。

需要存储的变量:

费率
	夏季
		高峰期
			时间
			价格
		中高峰期
			时间
			价格
		非高峰期
			时间
			价格
	非夏季
		高峰期
			时间
			价格
		中高峰期
			时间
			价格
		非高峰期
			时间
			价格
英文:

I'm creating a Java program which calculates rates. The rate has a season which has its peaks and those peaks have their own hours and prices. I was thinking of doing nested classes, but there's got to be a better way that I'm just not thinking about it right?

Variables that I need to store:

Rate
	Summer
		peak
			hours
			price
		midpeak
			hours
			price
		off-peak
			hours
			price
	Non-summer
		peak
			hours
			price
		midpeak
			hours
			price
		off-peak
			hours
			price

答案1

得分: 1

这是一种方法:

class BaseRate {
    int hours;
    int price;

    public BaseRate(int hours, int price) {
        this.hours = hours;
        this.price = price;
    }
}

class SeasonalRate {
    BaseRate peakRate;
    BaseRate midPeakRate;
    BaseRate offPeakRate;

    public SeasonalRate(BaseRate peakRate, BaseRate midPeakRate, BaseRate offPeakRate) {
        this.peakRate = peakRate;
        this.midPeakRate = midPeakRate;
        this.offPeakRate = offPeakRate;
    }
}

class AnnualRates {
    SeasonalRate summerRate;
    SeasonalRate winterRate;

    public AnnualRates(SeasonalRate summerRate, SeasonalRate winterRate) {
        this.summerRate = summerRate;
        this.winterRate = winterRate;
    }

    public static void main(String[] args) {
        BaseRate peakWinterRateFor2019 = new BaseRate(1, 3);
        BaseRate midPeakWinterRateFor2019 = new BaseRate(1, 3);
        BaseRate offPeakWinterRateFor2019 = new BaseRate(1, 3);
        BaseRate peakSummerRateFor2019 = new BaseRate(1, 3);
        BaseRate midPeakSummerRateFor2019 = new BaseRate(1, 3);
        BaseRate offPeakSummerRateFor2019 = new BaseRate(1, 3);

        SeasonalRate winterRateFor2019 = new SeasonalRate(peakWinterRateFor2019, midPeakWinterRateFor2019, offPeakWinterRateFor2019);
        SeasonalRate summerRateFor2019 = new SeasonalRate(peakSummerRateFor2019, midPeakSummerRateFor2019, midPeakWinterRateFor2019);

        AnnualRates ratesFor2019 = new AnnualRates(summerRateFor2019, winterRateFor2019);
    }
}
英文:

Here is an approach:

class BaseRate {
int hours;
int price;
public BaseRate(int hours, int price) {
this.hours = hours;
this.price = price;
}
}
class SeasonalRate {
BaseRate peakRate;
BaseRate midPeakRate;
BaseRate offPeakRate;
public SeasonalRate(BaseRate peakRate, BaseRate midPeakRate, BaseRate offPeakRate) {
this.peakRate = peakRate;
this.midPeakRate = midPeakRate;
this.offPeakRate = offPeakRate;
}
}
class AnnualRates {
SeasonalRate summerRate;
SeasonalRate winterRate;
public AnnualRates(SeasonalRate summerRate, SeasonalRate winterRate) {
this.summerRate = summerRate;
this.winterRate = winterRate;
}
public static void main(String[] args) {
BaseRate peakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate midPeakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate offPeakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate peakSummerRateFor2019 = new BaseRate(1, 3);
BaseRate midPeakSummerRateFor2019 = new BaseRate(1, 3);
BaseRate offPeakSummerRateFor2019 = new BaseRate(1, 3);
SeasonalRate winterRateFor2019 = new SeasonalRate(peakWinterRateFor2019, midPeakWinterRateFor2019, offPeakWinterRateFor2019);
SeasonalRate summerRateFor2019 = new SeasonalRate(peakSummerRateFor2019, midPeakSummerRateFor2019, midPeakWinterRateFor2019);
AnnualRates ratesFor2019 = new AnnualRates(summerRateFor2019, winterRateFor2019);
}
}

huangapple
  • 本文由 发表于 2020年4月4日 15:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61024826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定