0% found this document useful (0 votes)
6 views

User.java

Uploaded by

bindughy2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

User.java

Uploaded by

bindughy2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

User.

java

1 /* Write a Java code that gives a simple demonstration of how a payment app processes a
payment request of a specific amount. The payment app initiates the transfer after checking
the balance, and prints whether the transaction is successful or not. The code uses the
concepts of inner classes in Java, and has the following components:
2
3 Interface Transferable with an abstract method transfer()
4
5 Class PaymentApp has the following members:
6
7 Instance variable accno of type String; it holds the account number for a specific Account
object
8 Constructor to initialize accno
9 An inner private class Account that has/does the following:
10 Instance variables balance, amount that hold the account balance and the amount to be
transferred, respectively.
11 Implements interface Transferable, which enables its object to be accessible from outside the
class PaymentApp.
12 Constructor Account(double a) assigns the value a to amount and initializes balance to 1000.
13 Method public String transfer() checks whether balance is less than amount. If it is, it
returns the string "Failed transaction", else it returns "Money debited, current balance is
<balance - amount>".
14 Method payRequest(double) that takes the amount to be transferred as an argument and returns
a Transferable object on which the method transfer() can be invoked.
15 Class User has a main method that does the following:
16
17 Creates an object of PaymentApp with a specific accno. Using this object, it invokes method
payRequest() to obtain a Transferable object and invoke the method transfer on it.
18 What you have to do
19
20 Define method payRequest(double) in class PaymentApp. */
21
22
23 import java.util.*;
24
25 interface Transferable {
26 String transfer();
27 }
28
29 class PaymentApp {
30 String accno;
31
32 public PaymentApp(String n) {
33 accno = n;
34 }
35
36 private class Account implements Transferable {
37 double amount;
38 double balance;
39
40 public Account(double a) {
41 amount = a;
42 balance = 1000;
43 }
44
45 public String transfer() {
46 if (balance < amount) {
47 return "Failed transaction";
48 } else {
49 balance = balance - amount;
50 return "Money debited, current balance is " + balance;
51 }
52 }
53 }
54
55 // DEFINE method payRequest()
56 public Transferable payRequest(double amount) {
57 return new Account(amount);
58 }
59 }
60
61 public class User {
62 public static void main(String[] args) {
63 Scanner sc = new Scanner(System.in);
64 PaymentApp u1 = new PaymentApp("ACC101010");
65 Transferable t1 = u1.payRequest(sc.nextDouble());
66 System.out.println(t1.transfer());
67 }
68 }

You might also like