📄 atm.java
字号:
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.
/* Illustrates object-oriented programming, identifying
* objects from scenarios for uses, and their methods
* from the responsibilities identified in the scenarios.
* The main method just constructs objects, which interact,
* providing the behavior they are responsible for.
*/
import personData.*;
import iopack.Io;
class User {
private Person me;
private Teller teller;
public User(Person p, Teller t) {
me = p;
teller = t;
System.out.println("\nUser is \n" + p + "\n");
teller.acceptCard(this);
}
public void enterPIN() {
String pin = Io.readString("Enter your PIN");
teller.acceptPIN(pin);
}
public void selectTransaction() {
int choice;
do {
System.out.println();
System.out.println("Choose a transaction");
System.out.println("1. Deposit");
System.out.println("2. Withdraw");
System.out.println("3. Cancel");
System.out.println("4. Get balance");
choice = Io.readInt("Enter your choice, 1, 2, 3, or 4");
} while (choice != 1 && choice != 2 && choice != 3 && choice != 4);
teller.acceptTransaction(choice);
}
public void selectType() {
int choice;
do {
System.out.println();
System.out.println("Choose an account type");
System.out.println("1. Savings");
System.out.println("2. Checking");
System.out.println("3. Cancel");
choice = Io.readInt("Enter your choice, 1, 2 or 3");
} while (choice != 1 && choice != 2 && choice != 3);
teller.acceptType(choice);
}
public void specifyAmount() {
double d = Io.readDouble("Enter an amount");
teller.acceptAmount(d);
}
}
class Teller {
public static final int DEPOSIT =1;
public static final int WITHDRAW = 2;
public static final int CANCEL = 3;
public static final int BALANCE = 4;
String id;
int transType;
int acctType;
User user;
Bank bank;
Account account;
public Teller(Bank b) {
bank = b;
}
public void acceptCard(User u) {
user = u;
user.enterPIN();
}
public void acceptPIN(String s) {
id = s;
user.selectTransaction();
}
public void acceptTransaction(int trans) {
transType = trans;
if (transType != CANCEL)
user.selectType();
}
public void acceptType(int type) {
acctType = type;
if (acctType != CANCEL)
bank.find(id,acctType,this);
}
public void acceptAccount(Account a) {
account = a;
if (account != null)
if (transType == BALANCE){
System.out.print("The balance is ");
Io.println$(account.getBalance());
user.selectTransaction();
}
else {
if (transType == DEPOSIT || transType == WITHDRAW)
user.specifyAmount();
}
else {
System.out.println("No such account");
user.selectTransaction();
}
}
public void acceptAmount(double amount) {
switch(transType) {
case DEPOSIT :
account.deposit(amount);
break;
case WITHDRAW:
account.withdraw(amount);
break;
}
user.selectTransaction();
}
}
class Bank {
public static final int SAVINGS = 1;
public static final int CHECKING = 2;
Name n1 = new Name("John","Venn");
Address a1 = new Address( "123 Main St.", "Tyler","WY", "45654");
Person p1 = new Person("123123123",n1,a1);
Name n2 = new Name("Mabel","Venn");
Person p2 = new Person("456456456",n2,a1);
Account p1Savings = new Savings(1500.00,p1,4.0);
Account p1Checking = new Checking(p1,2500.00,.50);
Account p2Savings = new Savings(1000.00,p2,3.5);
public void find(String id, int acctType, Teller teller) {
if (p1Savings.getId().equals(id) && acctType==SAVINGS)
teller.acceptAccount(p1Savings);
else if (p1Checking.getId().equals(id)&& acctType==CHECKING)
teller.acceptAccount(p1Checking);
else if (p2Savings.getId().equals(id) && acctType==SAVINGS)
teller.acceptAccount(p2Savings);
else
teller.acceptAccount(null);
}
}
public class Atm {
public static void main(String [] args) {
Bank bank = new Bank();
Teller teller = new Teller(bank);
User user1 = new User(bank.p1,teller);
User user2 = new User(bank.p2,teller);
Io.readString("Press any key to exit"); // Added for IDE use
}
}
abstract class Account {
private double balance;
private Person holder;
public Account(Person p) {
this(0,p);
}
public Account(double initialAmount, Person p) {
balance = initialAmount;
holder = p;
}
public String getId() {
return holder.getId();
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
public double getBalance() {
return balance;
}
public void setBalance(double amount) {
balance = amount;
}
}
class Checking extends Account {
private double minBalance;
private double charge;
public Checking(Person p,double minAmount, double charge) {
super(p);
minBalance = minAmount;
this.charge = charge;
}
public void processCheck(double amount) {
if (getBalance() >= minBalance)
super.withdraw(amount);
else
super.withdraw(amount + charge);
}
public void withdraw(double amount) {
processCheck(amount);
}
}
class Savings extends Account {
private double interestRate;
public Savings(double amount, Person p, double rate) {
super(amount,p);
interestRate = rate;
}
public void postInterest() {
double balance = getBalance();
double interest = interestRate/100*balance;
setBalance(balance + interest);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -