-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #512 from starlight0405/master
atm homework
- Loading branch information
Showing
32 changed files
with
1,350 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...ents/2816977791/ood/ood-assignment/src/main/java/com/coderings/dp/builder/TagBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.coderings.dp.builder; | ||
|
||
public class TagBuilder { | ||
|
||
final TagNode rootNode; | ||
|
||
TagNode iteratorNode; | ||
|
||
TagNode prevIteratorNode; | ||
|
||
public TagBuilder(String rootTagName) { | ||
rootNode = new TagNode(rootTagName); | ||
iteratorNode = rootNode; | ||
prevIteratorNode = rootNode; | ||
} | ||
|
||
public TagBuilder addChild(String childTagName) { | ||
TagNode tagNode = new TagNode(childTagName); | ||
iteratorNode.add(tagNode); | ||
|
||
prevIteratorNode = iteratorNode; | ||
iteratorNode = tagNode; | ||
|
||
return this; | ||
} | ||
|
||
public TagBuilder addSibling(String siblingTagName) { | ||
TagNode tagNode = new TagNode(siblingTagName); | ||
prevIteratorNode.add(tagNode); | ||
|
||
iteratorNode = tagNode; | ||
|
||
return this; | ||
} | ||
|
||
public TagBuilder setAttribute(String name, String value) { | ||
iteratorNode.setAttribute(name, value); | ||
return this; | ||
} | ||
|
||
public TagBuilder setText(String value) { | ||
iteratorNode.setValue(value); | ||
return this; | ||
} | ||
|
||
public String toXML() { | ||
return rootNode.toXML(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../2816977791/ood/ood-assignment/src/main/java/com/coderings/dp/builder/TagBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.coderings.dp.builder; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TagBuilderTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testToXML() { | ||
|
||
TagBuilder builder = new TagBuilder("order"); | ||
|
||
String xml = builder.addChild("line-items") | ||
.addChild("line-item").setAttribute("pid", "P3677").setAttribute("qty", "3") | ||
.addSibling("line-item").setAttribute("pid", "P9877").setAttribute("qty", "10") | ||
.toXML(); | ||
|
||
String expected = "<order>" | ||
+ "<line-items>" | ||
+ "<line-item pid=\"P3677\" qty=\"3\"/>" | ||
+ "<line-item pid=\"P9877\" qty=\"10\"/>" | ||
+ "</line-items>" | ||
+ "</order>"; | ||
|
||
System.out.println(xml); | ||
assertEquals(expected, xml); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
students/2816977791/ood/ood-assignment/src/main/java/com/coderings/dp/builder/TagNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.coderings.dp.builder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TagNode { | ||
private String tagName; | ||
private String tagValue; | ||
private List<TagNode> children = new ArrayList<>(); | ||
private List<Attribute> attributes = new ArrayList<>(); | ||
|
||
public TagNode(String name){ | ||
this.tagName = name; | ||
} | ||
public void add(TagNode child){ | ||
this.children.add(child); | ||
} | ||
public void setAttribute(String name, String value) { | ||
Attribute attr = findAttribute(name); | ||
if(attr != null){ | ||
attr.value = value; | ||
return; | ||
} | ||
|
||
attributes.add(new Attribute(name,value)); | ||
} | ||
private Attribute findAttribute(String name){ | ||
for(Attribute attr : attributes){ | ||
if(attr.name.equals(name)){ | ||
return attr; | ||
} | ||
} | ||
return null; | ||
} | ||
public void setValue(String value) { | ||
this.tagValue = value; | ||
|
||
} | ||
public String getTagName() { | ||
return tagName; | ||
} | ||
public List<TagNode> getChildren() { | ||
return children; | ||
} | ||
|
||
private static class Attribute{ | ||
public Attribute(String name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
String name; | ||
String value; | ||
|
||
} | ||
public String toXML(){ | ||
return toXML(this); | ||
} | ||
private String toXML(TagNode node){ | ||
StringBuilder buffer = new StringBuilder(); | ||
buffer.append("<").append(node.tagName); | ||
if(node.attributes.size()> 0){ | ||
for(int i=0;i<node.attributes.size();i++){ | ||
Attribute attr = node.attributes.get(i); | ||
buffer.append(" ").append(toXML(attr)); | ||
} | ||
} | ||
if(node.children.size()== 0){ | ||
buffer.append("/>"); | ||
return buffer.toString(); | ||
} | ||
buffer.append(">"); | ||
for(TagNode childNode : node.children){ | ||
buffer.append(toXML(childNode)); | ||
} | ||
buffer.append("</").append(node.tagName).append(">"); | ||
|
||
|
||
return buffer.toString(); | ||
} | ||
private String toXML(Attribute attr){ | ||
return attr.name+"=\""+attr.value + "\""; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...91/ood/ood-assignment/src/main/java/com/coderings/dp/singleton/jdk_singleton.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#Singleton in JDK | ||
|
||
## java.lang.Runtime | ||
|
||
private static Runtime currentRuntime = new Runtime(); | ||
|
||
public static Runtime getRuntime() { | ||
return currentRuntime; | ||
} | ||
|
||
## java.awt.Desktop | ||
|
||
private DesktopPeer peer; | ||
|
||
private Desktop() { | ||
peer = Toolkit.getDefaultToolkit().createDesktopPeer(this); | ||
} | ||
|
||
|
||
public static synchronized Desktop getDesktop(){ | ||
Desktop desktop = (Desktop)context.get(Desktop.class); | ||
if (desktop == null) { | ||
desktop = new Desktop(); | ||
context.put(Desktop.class, desktop); | ||
} | ||
return desktop; | ||
} | ||
|
||
## java.lang.System | ||
|
||
private static volatile SecurityManager security = null; | ||
|
||
public static SecurityManager getSecurityManager() { | ||
return security; | ||
} | ||
|
166 changes: 166 additions & 0 deletions
166
...2816977791/ood/ood-assignment/src/main/java/com/coderising/ood/atmSimulation/atm/Atm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.coderising.ood.atmSimulation.atm; | ||
|
||
import com.coderising.ood.atmSimulation.atm.console.SuperKeypad; | ||
import com.coderising.ood.atmSimulation.atm.print.Printer; | ||
import com.coderising.ood.atmSimulation.atm.proxy.BankProxy; | ||
import com.coderising.ood.atmSimulation.atm.reader.CardReader; | ||
import com.coderising.ood.atmSimulation.atm.slot.CashDepensier; | ||
import com.coderising.ood.atmSimulation.atm.slot.DepositSlot; | ||
import com.coderising.ood.atmSimulation.atm.transaction.*; | ||
import com.coderising.ood.atmSimulation.card.Card; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
/** | ||
* @author nvarchar | ||
* date 2017/7/14 | ||
*/ | ||
public class Atm { | ||
//存钱口 | ||
private DepositSlot depositSlot; | ||
//取钱口 | ||
private CashDepensier cashDepensier; | ||
//超级键盘 | ||
private SuperKeypad superKeypad; | ||
//读卡器 | ||
private CardReader cardReader; | ||
//bankProxy | ||
private BankProxy bankProxy; | ||
//打印器 | ||
private Printer printer; | ||
|
||
public Atm(DepositSlot depositSlot, CashDepensier cashDepensier, | ||
SuperKeypad superKeypad, CardReader cardReader, BankProxy bankProxy, Printer printer) { | ||
this.depositSlot = depositSlot; | ||
this.cashDepensier = cashDepensier; | ||
this.superKeypad = superKeypad; | ||
this.cardReader = cardReader; | ||
this.bankProxy = bankProxy; | ||
this.printer = printer; | ||
} | ||
|
||
//账号|密码 | ||
private String account; | ||
private String password; | ||
|
||
private List<Trasaction> list = new ArrayList<>(); | ||
|
||
public String getAccount() { | ||
return account; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public boolean hasEnoughMoney(int amount) { | ||
return cashDepensier.hasEnoughMoney(amount); | ||
} | ||
|
||
public boolean dipenseMoney(int amount) { | ||
return cashDepensier.dispenseMoney(amount); | ||
} | ||
|
||
public int retriveMoney() { | ||
Random random = new Random(); | ||
int money = random.nextInt(10000); | ||
return depositSlot.retriveMoney(money); | ||
} | ||
|
||
//插卡 | ||
public void insertCard(Card card) { | ||
account = cardReader.readCard(card); | ||
} | ||
|
||
//输入密码 | ||
public void writePassword() { | ||
int count = 1; | ||
while (count <= 3) { | ||
count++; | ||
password = superKeypad.inputPassword(); | ||
if (bankProxy.verify(account, password)) { | ||
return; | ||
} | ||
} | ||
superKeypad.display("吞卡啦,到前台吧~"); | ||
throw new RuntimeException("atm eat card"); | ||
} | ||
|
||
//查询 | ||
public void query() { | ||
superKeypad.display("查询"); | ||
QueryBalanceTx tx = new QueryBalanceTx(); | ||
if (tx.preProcess(this)) { | ||
String response = bankProxy.process(tx, this); | ||
tx.setAmount(Integer.valueOf(response)); | ||
superKeypad.display("查询操作余额为:" + response); | ||
} | ||
list.add(tx); | ||
tx.postProcess(this); | ||
} | ||
|
||
//转账 | ||
public void transfer() { | ||
superKeypad.display("转账"); | ||
String toCard = superKeypad.inputCardNumber(); | ||
int amount = superKeypad.inputAmount(); | ||
TransferTx tx = new TransferTx(toCard, amount); | ||
if (tx.preProcess(this)) { | ||
String response = bankProxy.process(tx, this); | ||
superKeypad.display("转账后,余额为:" + response); | ||
} | ||
list.add(tx); | ||
tx.postProcess(this); | ||
} | ||
|
||
//取款 | ||
public void dependesier() { | ||
superKeypad.display("取款"); | ||
int amount = superKeypad.inputAmount(); | ||
WithdrawTx tx = new WithdrawTx(amount); | ||
if (tx.preProcess(this)) { | ||
String response = bankProxy.process(tx, this); | ||
superKeypad.display("取款后,余额为:" + response); | ||
} | ||
list.add(tx); | ||
tx.postProcess(this); | ||
} | ||
|
||
//存钱 | ||
public void deposit() { | ||
superKeypad.display("存款"); | ||
DepositTx tx = new DepositTx(retriveMoney()); | ||
if (tx.preProcess(this)) { | ||
String response = bankProxy.process(tx, this); | ||
superKeypad.display("存钱后,余额为:" + response); | ||
} | ||
list.add(tx); | ||
tx.postProcess(this); | ||
} | ||
|
||
//打印 | ||
public void print() { | ||
superKeypad.display("------打印操作-------"); | ||
printer.print(list); | ||
superKeypad.display("------打印完毕-------"); | ||
} | ||
|
||
//退卡 | ||
public void ejectCard(Card card) { | ||
cardReader.ejectCard(card); | ||
superKeypad.display("退卡成功"); | ||
} | ||
|
||
//引导 | ||
public void showGuide() { | ||
superKeypad.display("请选择操作:"); | ||
superKeypad.display("1:查询"); | ||
superKeypad.display("2:存钱"); | ||
superKeypad.display("3:取款"); | ||
superKeypad.display("4:转账"); | ||
superKeypad.display("5:退卡"); | ||
} | ||
|
||
} |
Oops, something went wrong.