2015-02-08 35 views
1

所以这个项目是在我的舒适区之外。我会将我目前的发展阶段描述为:“我知道诸如:收藏,设计模式,以及通常为什么会产生良好的面向对象的原因。但是这些东西有点像我目前的极限。因此,我可能不会使用它们或试图尽可能多地使用它们。“爪哇:面向对象的建议和建议,以推进一个小应用程序

我试图改变这种状况,所以我一直在努力研究相当小的挑战/应用程序,它真的适合于上面并要求自己写出聪明,干净的代码。我对迄今为止能够做到的事情非常满意。但是,我还剩下两门课,但仍需要深入研究。我对如何解决这个问题有很多想法。我敢肯定,有些是好的,有些是坏的,但更重要的是,我认为我在思考问题。

总之,这里是我想要做的,这里是我,这里是我需要的地方:

我想要做的事:最简单说明这个应用的目标的方式是,我有信用卡(这个班是我所做的班),我有钱包,我有人。从高级角度来看,我把卡片放在人们的钱包和钱包里。我有3张牌,他们的名字和利率真的不同。我想要一些钱包有一张卡,其他人有三张。至于钱包,每个人至少需要一个,但我想给两个人。这确实是关于它的,我计算了一些简单的兴趣数学,我将在某些时候配合使用,但大多数情况下我都希望构建一个干净和设计良好的应用程序。

我有什么:正如我所说我或多或少有CreditCard类完成。我仍然在很好地调整它,但我已经能够改善它很多,我对它随随便便。我将在下面包括这个类来为应用程序提供上下文,因此如果需要,您可以提供建议。在课程的顶部你会看到很多文档。这主要是数学,而这是逻辑,用于计算简单的兴趣。你会看到的。但是我也有两个我正在编码的测试用例,你也会看到。

我需要去的地方:那么我有信用卡。现在我只需要钱包和人。从我的角度来看,我可以看到使用ArrayList的钱包。尽管如此,收藏的不同方面可能会更好。我主要(主要)使用ArrayList,所以我主要使用ArrayList。到目前为止,这是成功的...除此之外,我一直在考虑制作电子钱包和人物摘要,这似乎是一个好主意,但再次,没有太多经验做出这些选择。因此,在所有这一切的结尾,我正在寻找一些方向,确定好的想法和替代较弱的想法。如果这些可以与示例相结合,或者如果建议可以用言语和代码表达自己,这将是最佳的,因为当我看到它在行动中时,我得到更多的建议。对于我来说,一个好的代码建议,通常比没有的建议更有帮助。这完全是关于能够应用该建议。但是,那只是我,每个人都不一样。我可以告诉你的是,这是明确的,就是所有的建议,无论它们是什么,都会受到赞赏和帮助。因为,我正在这样做,我在这里,要学习。

/* 
* 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); 
    } 
} 
+0

W¯¯帽子是'钱包'的用途? – 2015-02-08 00:53:38

+0

好吧,我想首先,对我来说,这似乎是有道理的,“试图让你的代码更好地代表生活世界的方式。”因此,我开始试图确定一个钱包为什么有用以及它真正做了什么。为了将其降低到更多的代码级别,我会说钱包拥有一组类似的对象,在这种情况下是卡片。如果一个人有两个钱包,那么这些钱包就会存放物品,并在两组之间提供分离/区别。这就是我想在我的钱包中实现的。 – ReedWilliams19842004 2015-02-08 01:11:15

+0

虽然这是事实,但OOP的目标不是在您的设计中实现所有可能的类,而只是那些影响功能的类。卡的余额,利息等将如何取决于它保存的特定钱包? – 2015-02-08 01:22:31

回答

0

首先不要去对象快乐!更多的对象!=更好的代码!

从数据的角度来看这个,因为这是大多数OOP程序员迷失方向的地方。这个东西存储关系的原因。

  • 的人让一个对象
  • 钱包 - 这简直是一个中间键的加入卡n个一个人。
  • CC作出这样的,因为每个CC的对象定义的付款,费用,利率等

方面你最终得到的是:

  • 表用户。
  • 表卡。

钱包,除非你想给它分配特定的属性,这是真的不存在的事情,因为拥有CC记录中的卡拥有者密钥卡给所有者。

蹭的确是在申请付款。就像银行之前他们开始处理您的支票或自动付款增加你的存款帐户,你有你calc下的利息+手续费值添加到平衡,使前申请任何发布方式:

for owners{ 
for cards with card.ownerID = owners.ID{ 
    card.balance=card.balance-payments ; 
    card.balance=card.balance+calcInterest(card.balance, card.rate)+GetSumOfFees(card); 
    } 
} 

这是每个CC发行人每晚运行的基本批量作业。

对事物的充电侧它是最小的代码,他们可以逃脱这个正弦必须在付费终端,等等,等等

public static String chargeThis(String CCData, Double AMT){ 

    CCNum = GetCCNum(CCData) ; 
    boolean isValid = validateCC(ccData); 
    if(isValid) return chargeCC(ccNum,AMT) else return rejectedTrans ; 
} 

关于列表的哈希地图等几乎在瞬间发生Java或C++中的向量...

任何对它们进行大小调整的操作都将非常快速地进行,直到它们超过L2缓存的大小!超过二级缓存的大小后,它们被存储在系统RAM中,并且它们的性能进入储罐。此时链表是优越的,因为增加或减少一个元素就是插入一个节点或删除一个节点。记住,获得高级性能需要了解机器并理解JVM或编译器如何安排事物。为了从最好到最差存储如下:

  • L0高速缓存 - 1个CPU周期
  • L1高速缓存 - 2个CPU周期
  • L2高速缓存 - 5至10个CPU周期
  • 系统内存1000的的CPU周期
  • 以太网(获得通过线路数据)K周期
  • 磁盘的10的 - 50〜100 K时周期
+0

否定一点。当我们的程序员在这里写两三个班的时候,计数周期会让你继续前进? – Aitch 2015-02-08 02:20:53

+0

@Aitch我甚至不知道那个评论的意思。顺便说一句,我也是一个OOP程序员。定义数据FIRST,其次是对象。 – FlyingGuy 2015-02-08 19:45:11

+0

这太棒了!这就是我正在谈论的。你建议一张地图,它会立即引发我的想法。我刚刚完成了对地图的简短回顾,现在我想知道为什么我没有更频繁地使用它们。在学习像Java这样的大规模语言时,我认为对像我这样的人来说,一个很大的挑战(自学,没有背景,没有网络资源)只是语言的大小(更不用说可以使用的其他语言和技术与)和所需时间来探索这一切。大多数人有背景,培训或指导,以帮助他们导航, – ReedWilliams19842004 2015-02-08 20:24:42