LEARNING to Program with Robots (JAVA) The link to the textbook is: http://www.l
ID: 3866831 • Letter: L
Question
LEARNING to Program with Robots (JAVA) The link to the textbook is: http://www.learningwithrobots.com/textbook/PDFs/WholeThing.pdf
8.2 Consider the class diagram shown in Figurc 8-26. It shows onc possible rclation ship between a bank's client and the client's account. Each client has a personal identification number (PIN) to use in accessing his account. The methods requir ing a PIN do nothing if the PIN docsn't match the one stored for the client. Client Account String name Account acct int pin double balance +Account( ) +void deposit(double amt) 4void withdraw(double amt) woid getBalance) +Client(Account a) tvoid deposit(int pin, double amt) tvoid withdraw int pin, double amt) +double getBalance(int pin) +Account getAccount) uppose you are a programmer working on the Bank class, which contains ref erences to objects representing all of the bank's clients. Explain three ways in which you could transfer moncy from one client's account to your account without knowing the client's PIN. In each case, explain how this security hole could be closed. Assume the programmer who implemented client and Account knows noth- ing of the dangers of using aliascs.Explanation / Answer
/**
*
* @author Sam
*/
public class Client {
private String name;
public Account acct;
private int pin;
public Client(Account acct) {
this.acct = acct;
pin = 1234;
name = "";
}
void deposit(int pin, double amt) {
if (pin == this.pin)
acct.deposit(amt);
}
void withdraw(int pin, double amt) {
if (pin == this.pin)
acct.withdraw(amt);
}
double getBalance(int pin) {
if (pin == this.pin)
return acct.getBalance();
return -999;
}
}
class Account {
private double balance;
public Account() {
balance = 0;
}
public double getBalance() {
return balance;
}
public void deposit(double amt) {
balance += amt;
}
public void withdraw(double amt) {
if (amt <= balance)
balance -= amt;
}
I belive that this is what you needed! right?
BTW for unsecure transaction we may have a code like this.
public static void sleathTransfer(Client a, Client b, double amount) {
a.acct.withdraw(amount); // we can use it since Account of client is public
b.acct.deposit(amount);
}
To secure such transactions, we may make Account acct private
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.