简单 Proxy 自动提款机应用
2014-06-04 00:00
169 查看
摘要: Proxy
Input输入类:
import java.util.Scanner;
public class Input {
private ProxyAccount account;
public Input(ProxyAccount account) {
super();
this.account = account;
}
public void startInput() {
String p = account.enterPIN();
if(p.equals(account.getPIN())){
Scanner scanner = new Scanner(System.in);
String c = "a";
while (!c.equals("x")) {
System.out.println("Enter");
System.out.println(" w to withdraw money");
System.out.println(" d to deposit money");
System.out.println(" x to quit");
System.out.print("Choice: ");
c = scanner.next();
if (c.equals("w") || c.equals("d")) {
int amount = 0;
System.out.print("Amount: ");
amount = scanner.nextInt();
if (c.equals("w")) {
account.withdraw(amount);
System.out.println("New amount: " + account.getAmount());
} else if (c.equals("d")) {
account.deposit(amount);
System.out.println("New amount: " + account.getAmount());
}
}
}
}
else{
System.out.println("Your password is false!");
this.startInput();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
RealAccount 实际Subject类:
public class RealAccount implements Account{
private int amount;
public RealAccount(int amount) {
super();
this.amount = amount;
}
public void withdraw(int money) {
amount = amount - money;
}
public void deposit(int money) {
amount = amount + money;
}
public int getAmount() {
return amount;
}
}
-------------------------------------------------------------------------------------------------------------------------------
ProxyAccount 代理类:
import java.util.Scanner;
public class ProxyAccount implements Account{
private Account proxied;
private String PIN;
public ProxyAccount(Account proxied, String PIN) {
super();
this.proxied = proxied;
this.PIN = PIN;
}
public String getPIN() {
return PIN;
}
public String enterPIN(){
Scanner pin = new Scanner(System.in);
System.out.print("Please enter your password: ");
return pin.next();
}
public void deposit(int money) {
proxied.deposit(money);
}
public int getAmount() {
return proxied.getAmount();
}
public void withdraw(int money) {
proxied.withdraw(money);
}
}
-------------------------------------------------------------------------------------------------------------------------------
Account 接口:
public interface Account {
void withdraw(int money);
void deposit(int money);
int getAmount();
}
-------------------------------------------------------------------------------------------------------------------------------
Main 主方法:
public class MainAccount {
public static void main(String[] args) {
Account account = new RealAccount(5000); //多态
ProxyAccount proxyaccount = new ProxyAccount(account,"123456");
Input input = new Input(proxyaccount);
input.startInput();
}
}
Input输入类:
import java.util.Scanner;
public class Input {
private ProxyAccount account;
public Input(ProxyAccount account) {
super();
this.account = account;
}
public void startInput() {
String p = account.enterPIN();
if(p.equals(account.getPIN())){
Scanner scanner = new Scanner(System.in);
String c = "a";
while (!c.equals("x")) {
System.out.println("Enter");
System.out.println(" w to withdraw money");
System.out.println(" d to deposit money");
System.out.println(" x to quit");
System.out.print("Choice: ");
c = scanner.next();
if (c.equals("w") || c.equals("d")) {
int amount = 0;
System.out.print("Amount: ");
amount = scanner.nextInt();
if (c.equals("w")) {
account.withdraw(amount);
System.out.println("New amount: " + account.getAmount());
} else if (c.equals("d")) {
account.deposit(amount);
System.out.println("New amount: " + account.getAmount());
}
}
}
}
else{
System.out.println("Your password is false!");
this.startInput();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
RealAccount 实际Subject类:
public class RealAccount implements Account{
private int amount;
public RealAccount(int amount) {
super();
this.amount = amount;
}
public void withdraw(int money) {
amount = amount - money;
}
public void deposit(int money) {
amount = amount + money;
}
public int getAmount() {
return amount;
}
}
-------------------------------------------------------------------------------------------------------------------------------
ProxyAccount 代理类:
import java.util.Scanner;
public class ProxyAccount implements Account{
private Account proxied;
private String PIN;
public ProxyAccount(Account proxied, String PIN) {
super();
this.proxied = proxied;
this.PIN = PIN;
}
public String getPIN() {
return PIN;
}
public String enterPIN(){
Scanner pin = new Scanner(System.in);
System.out.print("Please enter your password: ");
return pin.next();
}
public void deposit(int money) {
proxied.deposit(money);
}
public int getAmount() {
return proxied.getAmount();
}
public void withdraw(int money) {
proxied.withdraw(money);
}
}
-------------------------------------------------------------------------------------------------------------------------------
Account 接口:
public interface Account {
void withdraw(int money);
void deposit(int money);
int getAmount();
}
-------------------------------------------------------------------------------------------------------------------------------
Main 主方法:
public class MainAccount {
public static void main(String[] args) {
Account account = new RealAccount(5000); //多态
ProxyAccount proxyaccount = new ProxyAccount(account,"123456");
Input input = new Input(proxyaccount);
input.startInput();
}
}
相关文章推荐
- 自动执行单元测试(NUnit)的简单应用
- Android文本自动提示框的简单应用实例
- 获取汇率模板自动生成注释的jsoup简单应用
- 两个将简体中文网页自动转为繁体中文网的页简单方法 (原理非常简单,尚不完善 IHttpHandler Proxy / Response.Filter)
- for循环的简单应用-自动生成div
- Calendar针对于相隔一段时间自动执行的简单应用
- Android文本自动提示框的简单应用实例
- 超简单微信公众帐号自动回复和天气播报功能应用
- Auto Layout简单应用——以编码的方式实现Auto Layout自动布局(二)
- Jquery UI简单应用——自动完成(Autocomplete)实现类似百度输入提醒效果
- Android仿百度谷歌搜索自动提示框AutoCompleteTextView简单应用示例
- 如何应用Asp.Net Mvc内建功能(DefaultModelBinder)实现简单类型、复杂类型、集合类型,以及字典类型的自动绑定
- Auto Layout简单应用——以编码的方式实现Auto Layout自动布局(一)
- 简单Dream-ORACLE_RAC故障透明切换测试,已连接的应用不会自动切换到第二个节点上
- python脚本设置微信自动回复[简单应用]
- jenkins自动部署应用到tomcat中,编译后shell脚本的简单示例
- Android应用学习(一)实现activity自动跳转和简单的登录
- Servlet监听器与Timer定时器配合实现JAVA WEB应用简单自动作业
- 两个将简体中文网页自动转为繁体中文网的页简单方法 (原理非常简单,尚不完善 IHttpHandler Proxy / Response.Filter)
- iOS 6编程-创建自动布局(Auto Layout)简单应用