0% found this document useful (0 votes)
10 views17 pages

Java Lab Project: Banking System

Uploaded by

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

Java Lab Project: Banking System

Uploaded by

Rashadur Rahaman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Bangladesh University

PROJECT
Course Title: Programming Language
(Java) Lab Course Code: CSE-2102

Prepared for: Pabon

Shaha Lecturer

Department of Computer Science &

Engineering Bangladesh University

Prepared by: Jannatul Nayim Miti

Batch CSE-63 (BSc) Day, ID: 2022 210


63001

Department of Computer Science &

Engineering Bangladesh University

Date of Submission: August 14, 2023


Real World Example:
Account

Object in JAVA:
• An entity that has state and behavior is known as an object.

• e.g., chair, bike, marker, pen, table, car, etc.

• It can be physical or logical (tangible and intangible).

• The example of an intangible object is the banking system.

Class in JAVA:
• A class is a group of objects which have common properties.

• It is a template or blueprint from which objects are created.

• It is a logical entity.

• It can't be physical.

Method in JAVA:
In Java, a method is like a function which is used to expose

the behavior of an object.

Real Example:
First create Java Program to demonstrate the working of a banking-system, where we deposit and
withdraw amount from our account. Creating an Account class which has deposit() and withdraw()
methods.

Input:
1. package javaapplication5; 2.
3. import [Link]; 4.
5. public class Account {
6. String acc_holder;
7. double deposit,withdraw,new_balance;
8. double balance;
9. long acc;
10. Scanner sc=new
Scanner([Link]); 11.
12. void information(){
13. [Link]("Name of account holder:");
14. acc_holder=[Link]();
15. [Link]("Account number:");
16. acc=[Link]();
17. [Link]("Initial Deposit:");
18. deposit=[Link]();
19. balance=deposi
t; 20.
21.
22. }
23. void showBalance(){
24. [Link]("Your current balance is:"+balance);
25. }
26. void deposit(){
27. [Link]("Enter the amount you deposit:");
28. double deposit_amount=[Link]();
29. balance=balance+deposit_amou
nt; 30.
31.
32. }
33. void withdraw(){
34. [Link]("Enter the amount you withdraw:");
35. double withdraw_amount=[Link]();
36. if(withdraw_amount <=balance){
37. balance=balance-withdraw_amount;
38. }
39. else
40. {
41. [Link]("Sorry! You don't have sufficient balance");
42. }
43.
44.
45. }
46. void accInfo(){
47. [Link]("------Your account info- ");
48. [Link]("Account name:"+acc_holder);
49. [Link]("Account Number:"+acc);
50. [Link]("Current
Balance:"+balance); 51.
52. }
53.
54. }
55. class main extends
Account{ 56.
57. public static void main(String[] args) {
58. [Link]("Welcome to M-Bank");
59. Account Ac = new Account();
60. boolean D = true;
61. while (D)
62. {
63. [Link]("1. Create Ac\n2. Show balance\n3. Deposit amount\n4.
Withdraw Amount\n5. Account info\n6. Exit");
64. [Link]("Enter your choise:");
65. int n =
[Link](); 66.
67. switch(n){
68. case 1:
69. [Link]();
70. break;
71. case 2:
72. [Link]();
73. break;
74. case 3:
75. [Link]();
76. break;
77.
78. case 4:
79. [Link]();
80. break;
81. case 5:
82. [Link]();
83. break;
84. case 6 :
85. [Link]("Thank you.\nYou are successfully done.");
86. D = false;
87. break;
88.
89. }
90.
91. }
92. }
93. }
94.
95.

Output:
Build a calculator by using class and
object in Java

Method in JAVA:
• A method is a block of code or collection of statements or a set of

code grouped together to perform a certain task or operation.

• It is used to achieve the reusability of code.

• It also provides the easy modification.

• and readability of code, just by adding or removing a chunk of code.

• The most important method in Java is the main() method.

Naming a Method:
• method name must be a verb and start with a lowercase letter.

• In the multi-word method name, the first letter of each word must be in uppercase except the
first word.

• For example:

• Single-word method name: sum(), area()

• Multi-word method name: areaOfCircle(), stringComparision()

Types of Method:
There are two types of methods in Java:

I. Predefined Method

II. User-defined Method

Example:
Here I build a calculator by using class and object in Java. Where method are-

I. Addition

II. Subtraction

III. Multiplication

IV. Division
V. Mod

• Which take user choice and take user input from user.

Code:
package javaapplication5;

import [Link];

public class Calculator {


double addition(double a, double
b){ return a+b;
}
double subtraction(double a, double
b){ return a-b;
}
double multiplication(double a,double
b){ return a*b;
}

double mod(double a,double b){

return a%b;
}
void division(double a, double
b){ double res = 0;
if(b==0)
{
[Link]("Cannot devide by zero!");
}
else
{
res = a/b;
[Link](""+
res);
}

}
public static void main(String[] args) {
Scanner input=new Scanner([Link]);
Calculator calculator = new
Calculator();
[Link]("Choose an
option:");
[Link]("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.
Modulas");
int opt=[Link]();
[Link]("Enter two
numbers:"); double
n1=[Link]();
double n2=[Link]();

switch(opt){
case 1:
[Link]("Result="+[Link](n1, n2));
break;
case 2:
[Link]("Result="+[Link](n1,
n2)); break;
case 3:
[Link]("Result="+[Link](n1,
n2)); break;
case 4:
[Link](n1,n2
); break;
case 5:
[Link]("Result="+[Link](n1, n2)); break;
default:
[Link]("Invalid Choise.");

}
}

Output:
Inheritance in JAVA
Introduction:
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.

Types of Inheritance:
There are five types of inheritance.

1. Single Inheritance

2. Multilevel Inheritance

3. Hierarchical Inheritance

4. Multiple Inheritance

5. Hybrid Inheritance

Terms used in Inheritance:


• Class: A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created.

• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived
class, extended class, or child class.

• Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It
is also called a base class or a parent class.

Single Inheritance: In single inheritance, a single subclass extends from a single superclass.
• For example:
Implementation of Single Inheritance:
1. package
javaapplication5; 2.
Output:
3. public class Employee {
4. float
salary=10000; 5.
6. }
7. class Devloper extends Employee{
8. float bonus=5000;
9. public static void main(String[] args) {
10. Devloper d=new Devloper();
11. [Link]("Devloper salary="+[Link]);
12. [Link]("Bonus="+[Link]);
13. }
14.
Multilevel Inheritance:
} In multilevel inheritance, a subclass extends from a superclass and
then the same subclass acts as a superclass for another class.

• For example:
Implementation of Multilevel Inheritance:
1. package javaapplication5;
2.
class Employee2 {
float salary=10000; 5.
6. }
class Programmer extends Employee2{
float bonusforprog=5000; 9.
10. }
class webDevloper extends Programmer{
float bonusfordev=5500; 13.
14. }
15. public class multilevelInheritance{

16. public static void main(String[] args) {


17. webDevloper d=new webDevloper(); [Link]("Devloper salary="+[Link]
18.
19.
20.
21.
22. }
23. } 24.

Output:
Hierarchical Inheritance: In hierarchical inheritance, multiple subclasses extend from a
single superclass.

For example:

Implementation of Hierarchical Inheritance:

package javaapplication5;

class Employee3 {
float
salary=10000;

}
class programmer extends
Employee3{ float
bonusforprog=5000;

}
class webdevloper extends
Employee3 { float
bonusfordev=5500;

public class hierarchicalInhertance {


public static void main(String[] args)
{ webdevloper d=new webdevloper();
programmer p =new programmer();
[Link]("Salary="+[Link]
y);
Output:

Multiple Inheritance: In multiple inheritance, a single subclass extends from multiple super
classes.

• For example:

Multiple inheritance is not possible in JAVA. The reason behind is to prevent ambiguity. Consider a case
where class B extends class A and class C and both class A and C have the same method display().Now
java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple
inheritances is not allowed in java.
Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.
• For example:

Implementation of Hybrid Inheritance:


package javaapplication5;
class C 3. {
[Link] void disp() 5.{
[Link]("C"); 7.}
8. }
9.
10. class A extends C
11. {
[Link] void disp()
13.{
[Link]("A");
15.}
16. } 17.
18. class B extends C
19. {
[Link] void disp()
21.{
[Link]("B");
23.}
24.
25. } 26.
27. class D extends A
28. {
[Link] void disp()
30.{
[Link]("D");
32. }
33. public static void main(String args[]){
34.
35. D obj = new D();
36. A A_obj =new A();
37.
38. [Link]();
39. A_obj.disp();
40.
41. }
42. }
43.

Output:
Method Overloading and
Overriding

Method Overloading in JAVA:


If a class has multiple methods having same name but different in parameters , it is known as Method
Overloading.

Different ways to overload the method:


There are two ways to overload the method in java

 By changing number of arguments


 By changing the data type

Implementation of Method Overloading by changing data type of


arguments:
In this example, we have created two methods that differs in data type. The first display method receives
one integer arguments and second display method receives string arguments.

1. package javaapplication5;
2.
3. public class MethodOverloading {
4. static void display(int a)
5. {
6. [Link]("My self Miti.");
7. }
8. static void display(String a)
9. {
10. [Link]("I am a student of CSE");
11. }
12. public static void main(String[] args) { display(1);
13. display("Hi");
14.
15.
16. }
17.
18. } 19.
Output:

Can we overload java main() method:


Yes,we can overload java main method by method overloading. We can have any number of main
methods in a class by method overloading. But JVM calls main() method which receives string array as
arguments only.

Method Overriding in Java:


If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.

Usage of Java Method Overriding:


• Method overriding is used to provide the specific implementation of a method which is already
provided by its superclass.

• Method overriding is used for runtime polymorphism.

Rules for Java Method Overriding:


 The method must have the same name as in the parent class.
 The method must have the same parameter as in the parent class.
 There must be an IS-A relationship (inheritance).

Implementation of Method Overriding:


In this example, we have defined the display method in the subclass as defined in the parent class but it
has some specific implementation. The name and parameter of the method are the same, and there is
IS- A relationship between the classes, so there is method overriding.
package javaapplication5; class Student{
public void displayinfo(){ [Link]("I am a stdudent.");
}
}
class Myself extends Student{ public void displayinfo(){
[Link]("Myself Miti.");

}
}
public class MethodOverriding {
public static void main(String[] args) { Myself m =new Myself(); [Link]();
}

Output:

Common questions

Powered by AI

Encapsulation strengthens modularity by hiding implementation details, allowing for changes without affecting external code. Inheritance enhances maintainability by enabling code reuse and hierarchical class structures, allowing updates to parent classes to cascade down to subclasses, minimizing duplication. Together, they support scalable and manageable codebases .

Inheritance in Java allows one class (child class) to inherit the properties and behaviors of another class (parent class), demonstrating an IS-A relationship. This means the child class, also known as the subclass, can leverage or modify the superclass's features, promoting code reusability and method overriding for specific functionality .

The 'main' method serves as the entry point for Java applications, which the JVM (Java Virtual Machine) invokes to start program execution. It is essential because it initializes the application, allowing user interaction via instantiated objects and method calls, as seen in both the calculator and banking system programs .

Method overloading in Java involves creating multiple methods with the same name but different parameters (number or type), allowing different implementations based on input arguments. In contrast, method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass, reflecting run-time polymorphism .

The 'deposit' method demonstrates encapsulation by modifying the balance within the method while keeping this operation hidden from external classes. It directly interacts with private data fields, ensuring that changes to the balance occur within controlled limits. This usage promotes data integrity, preventing unauthorized access .

Multiple inheritance is not possible in Java to avoid the diamond problem, where ambiguity arises if two parent classes have the same method. Java handles similar situations by allowing interfaces, which enable multiple inheritance-like behavior without ambiguity as interfaces provide only method signatures, not implementations .

The lack of exception handling in the calculator's division method could lead to runtime errors when dividing by zero. While the program manually checks for zero, using Java's exception handling would provide a robust mechanism to manage such errors dynamically, ensuring safe operations and preserving application stability .

In the Java banking application, Scanner is used to capture user inputs by reading data types such as strings and numbers from the console. It allows for interactive programming by reading user input and storing it in program variables, which is essential for functions like creating accounts and performing transactions .

Hierarchical inheritance involves multiple subclasses extending from a single superclass. It enables shared behavior and properties among different subclasses, promoting code reuse. In the Java example, 'programmer' and 'webdevloper' classes both extend the 'Employee3' class, inheriting the salary property while defining unique bonuses .

To adhere to Java naming conventions, method names in the `Account` class should start with a lowercase letter and follow camelCase for multiple words. For example, `information` should be renamed `setInformation`, `showBalance` remains as it follows conventions, and `accInfo` should be changed to `accountInfo` .

You might also like