【发布时间】:2015-04-07 23:37:43
【问题描述】:
所以这个项目有点超出我的舒适区。我会将我当前的开发阶段描述为,“我了解以下内容:收集、设计模式,以及一般来说什么是好的 OOP。但这些事情在我目前的极限范围内。所以我可能不会使用它们或尝试尽可能多地使用它们。”
我正在尝试改变这一点,所以我一直在研究非常适合上述情况的相当小的挑战/应用程序,并要求自己编写智能、干净的代码。到目前为止,我对自己能够做到的事情感到相当满意。但是,我还有两门课需要深入学习。我有很多关于如何去做的想法。我敢肯定,有些是好的,有些是坏的,但最重要的是,我想我想多了。
简而言之,这就是我想要做的,这就是我所拥有的,这就是我需要去的地方:
我正在尝试做的事情: 陈述这个应用程序目标的最简单方法是,我有信用卡(这门课是我做过的课程),我有钱包,并且我有人。从高层的角度来看,我是把卡放在钱包里,把钱包放在人们身上。我有 3 张卡,它们实际上只是它们的“名称”和利率不同。我希望一些钱包有 1 张卡,而另一些钱包则拥有所有 3 张卡。至于钱包,显然每个人都至少需要一个,但我想给某人两个。就是这样,我为卡片上的简单兴趣计算了一些数学,我会在某个时候配合,但主要是我希望构建一个干净且设计良好的应用程序。
我有什么:正如我所说,我或多或少地完成了CreditCard 课程。我仍然在微调它,但我已经能够改进它很多,我对此很满意。我将在下面包含此类以提供应用程序的上下文,并且您可以在需要时提供建议。在课程的顶部,您会看到很多文档。这主要是计算简单兴趣的数学和逻辑。你会看到的。但是我还有两个要编写代码的测试用例,你也会看到的。
我要去哪里:我有信用卡。现在我只需要钱包和人。从我的角度来看,我可以看到钱包使用了 ArrayList。但是,集合的不同方面可能会更好地发挥作用。我主要(大部分)使用 ArrayList,所以我主要使用 ArrayList。到目前为止,它已经解决了……除此之外,我一直在考虑将 Wallet 和 Person 抽象化,这似乎是个好主意,但再一次,在做出这些选择方面没有太多经验。
所以在所有这一切结束时,我正在寻找一些方向,整合好的想法以及弱点的替代方案。如果这些可以与示例相结合,或者如果建议可以用文字和代码来表达,这将是最佳的,因为当我看到它在行动中时,我会从建议中得到更多。对我来说,一个好的代码建议“通常”比没有代码的好建议更有帮助。这一切都是为了能够应用该建议。但是,这只是我,每个人都不一样。我可以明确地告诉你的是,所有建议,无论它们是什么,都会受到赞赏和帮助。因为,我在做这个,我在这里,学习。
/*
* Test Cases:
* 1) 1 person has 1 wallet and 3 cards (1 Visa, 1 MC 1 Discover) – Each Card has a balance of $100 – calculate the total interest (simple interest) for this person and per card.
*
* 2) 1 person has 2 wallets Wallet 1 has a Visa and Discover , wallet 2 a MC - each card has $100
* balance - calculate the total interest(simple interest) for this person and interest per wallet
*/
/*
* Formula Key:
*
* Algebraic Symbols:
* A = Total Accrued Amount (principal + interest)
* P = Principal Amount
* I = Interest Amount
* r & R = Rate of Interest per year in percentage & decimal
* t = Time Period involved in months or years(duration pertaining to this equation)
*
* Rate of Interest, Percentage To Decimal Equations:
* R = r * 100
* r = R / 100
*
* Simple Interest Equation:
* A = P(1 + (r * t))
*/
/*
* Card:
* VISA 10%
*
* Equation:
* Accrued Amount(A) = Principle Amount(P) * (1 +(Interest Rate(r) * Time Period(t)))
*
* Calculation:
* First, converting Interest Rate(R) of 10%, to, Interest Rate(r) of 0.1
* r = R/100 = 10%/100 = 0.1 per year,
* put Time Period(t) of 1 month into years,
* months/year(1 month ÷ 12) = 0.08 years
*
* Solving Equation:
* A = 100(1 + (0.1 × 0.08)) = 100.8
* A = $ 100.80
*
* Solution:
* The total Amount Accrued(A), Principal(P) plus Interest(I),
* from Simple Interest on a Principal(P) of $ 100.00
* at a Rate(r = R/100(convert a percentage to a decimal)) of 10% or 0.1 per year
* for 0.08 years, 1 month(t) is $ 100.80.
*/
/*
* Card:
* MC(Master Card) 5%
*
* Equation:
* Accrued Amount(A) = Principle Amount(P) * (1 +(Interest Rate(r) * Time Period(t)))
*
* Calculation:
* First, converting Interest Rate(R) of 5%, to, Interest Rate(r) of 0.05
* r = R/100 = 5%/100 = 0.05 per year,
* put Time Period(t) of 1 month into years,
* months/year(1 month ÷ 12) = 0.08 years
*
* Solving Equation:
* A = 100(1 + (0.05 × 0.08)) = 100.4
* A = $ 100.40
*
* Solution:
* The total Amount Accrued(A), Principal(P) plus Interest(I),
* from Simple Interest on a Principal(P) of $ 100.00
* at a Rate(r = R/100(convert a percentage to a decimal)) of 5% or 0.05 per year
* for 0.08 years, 1 month(t) is $ 100.40.
*/
/*
* Card:
* Discover 1%
*
* Equation:
* Accrued Amount(A) = Principle Amount(P) * (1 +(Interest Rate(r) * Time Period(t)))
*
* Calculation:
* First, converting Interest Rate(R) of 1%, to, Interest Rate(r) of 0.01
* r = R/100 = 1%/100 = 0.01 per year,
* put Time Period(t) into years,
* months/year(1 month ÷ 12) = 0.08 years
*
*
* Solving Equation:
* A = 100(1 + (0.01 × 0.08)) = 100.08
* A = $ 100.08
*
* Solution:
* The total Amount Accrued(A), Principal(P) Plus Interest(I),
* from Simple Interest on a Principal(P) of $ 100.00
* at a Rate(r = R/100(convert a percentage to a decimal)) of 1% or 0.01 per year
* for 0.08 years, 1 month(t) is $ 100.08.
*/
public class CreditCard
{
private BrandOfCard brandOfCard;
private static final double PRINCIPAL_AMOUNT = 100.00;
private static final double TIME_PERIOD = 0.08;
public CreditCard(BrandOfCard brandOfCard)
{
this.brandOfCard = brandOfCard;
}
/*
* A = P(1 + (r * t))
*/
public double getAccruedAmount()
{
double accruedAmount;
accruedAmount = PRINCIPAL_AMOUNT * (1 + (brandOfCard.getInterestRate() * TIME_PERIOD));
return accruedAmount;
}
public enum BrandOfCard
{
VISA(0.1), MASTER_CARD(0.05), DISCOVER(0.01);
private final double interestRate;
BrandOfCard(double interestRate)
{
this.interestRate = interestRate;
}
public double getInterestRate()
{
return interestRate;
}
}
//bottom of class
public static void main(String[] args)
{
CreditCard visa = new CreditCard(BrandOfCard.VISA);
CreditCard masterCard = new CreditCard(BrandOfCard.MASTER_CARD);
CreditCard discover = new CreditCard(BrandOfCard.DISCOVER);
double accruedAmount;
accruedAmount = visa.getAccruedAmount();
System.out.println("Visa card, with a principle amount of $100.00, & an interest rate of 10%, " +
"has accrued $" + (accruedAmount - PRINCIPAL_AMOUNT) + " in interest, " +
"over the last monthly term.");
System.out.println("The total amount due on this card is now $" + accruedAmount);
accruedAmount = masterCard.getAccruedAmount();
System.out.println("Master Card card, with a principle amount of $100.00, & an interest rate of 5%, " +
"has accrued $" + (accruedAmount - PRINCIPAL_AMOUNT) + " in interest, " +
"over the last monthly term.");
System.out.println("The total amount due on this card is now $" + accruedAmount);
accruedAmount = discover.getAccruedAmount();
System.out.println("Discover card, with a principle amount of $100.00, & an interest rate of 1%, " +
"has accrued $" + (accruedAmount - PRINCIPAL_AMOUNT) + " in interest, " +
"over the last monthly term.");
System.out.println("The total amount due on this card is now $" + accruedAmount);
}
}
【问题讨论】:
-
Wallet的目的是什么? -
好吧,我想首先,这对我来说似乎很有意义,因为“试图让你的代码以某种方式更好地代表现实世界”。因此,正是从那时起,我开始尝试确定钱包为什么有用以及它的真正作用。为了将其降低到更多的代码级别,我会说钱包包含一组类似的对象,在这种情况下是卡片。如果一个人有两个钱包,这些钱包会存放物品并提供两组之间的分离/差异。所以这就是我想在我的钱包中实现的目标。
-
虽然这是事实,但 OOP 的目标并不是实现设计中的所有可能类,而只是实现那些影响功能的类。卡余额、利息等如何取决于它所在的特定钱包?
-
不能再同意了。如果这些回复再长一点,我会继续说一些与您发布的内容非常相似的内容,因为您的帖子密切反映了我一直在考虑的一个反驳点。很明显,我不是 OOP 方面的专家,但即使在我适度的研究中,我也注意到在如何教授、描述和使用 OOP 方面存在许多这样的二元性。关于 OOP,您可以说的唯一真实的事情可能是 OOP 就像 OOP 剂量一样。这真的回到了我问这个问题的原因,因为在缺乏客观性(没有双关语)的情况下,获得视角。
-
至于钱包,在余额、利息等方面……根本不涉及,没有目的。但是,我认为它对我有用,因为它为我的应用程序增加了一些新的动力,更多的练习。但在功能上,不涉及数学。您的观察和观点很好,“Wallet 的目的是什么?”该死的好问题,该死的好......比这个回复好多了。
标签: java oop arraylist collections abstract