Skip to content

Commit

Permalink
Merge pull request #512 from starlight0405/master
Browse files Browse the repository at this point in the history
atm homework
  • Loading branch information
onlyliuxin authored Jul 25, 2017
2 parents 4b68c39 + bc2ecb5 commit 9e514d7
Show file tree
Hide file tree
Showing 32 changed files with 1,350 additions and 0 deletions.
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();
}
}
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);
}

}
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 + "\"";
}
}
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;
}

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:退卡");
}

}
Loading

0 comments on commit 9e514d7

Please sign in to comment.