英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论