Skip to content

Commit 444856a

Browse files
Merge pull request #2 from Anish-Bansode/anish-branch
Added two new blockchain codes (Bank and Student Solidity Contracts)
2 parents fa05bc7 + 91511b0 commit 444856a

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract BankAccount {
5+
// State variable to store the balance of each account
6+
mapping(address => uint256) private balances;
7+
8+
// Event to log deposits
9+
event Deposit(address indexed account, uint256 amount);
10+
11+
// Event to log withdrawals
12+
event Withdraw(address indexed account, uint256 amount);
13+
14+
// Function to deposit money into the account
15+
function deposit(uint256 amount) public payable {
16+
require(amount > 0, "Deposit amount must be greater than zero");
17+
// Ensure the value sent with the transaction matches the amount specified
18+
require(msg.value == amount, "Amount does not match the value sent");
19+
20+
balances[msg.sender] += msg.value;
21+
22+
emit Deposit(msg.sender, msg.value);
23+
}
24+
25+
// Function to withdraw money from the account
26+
function withdraw(uint256 amount) public {
27+
require(amount > 0, "Withdrawal amount must be greater than zero");
28+
require(balances[msg.sender] >= amount, "Insufficient balance");
29+
30+
balances[msg.sender] -= amount;
31+
32+
// Transfer the amount back to the caller
33+
payable(msg.sender).transfer(amount);
34+
35+
emit Withdraw(msg.sender, amount);
36+
}
37+
38+
// Function to check the balance of the account
39+
function getBalance() public view returns (uint256) {
40+
return balances[msg.sender];
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
contract StudentData {
5+
struct Student {
6+
uint id;
7+
string name;
8+
uint age;
9+
string course;
10+
}
11+
12+
Student[] public students; // Array to store student records
13+
uint public studentCount = 0;
14+
15+
// Function to add a new student
16+
function addStudent(string memory _name, uint _age, string memory _course) public {
17+
studentCount++;
18+
students.push(Student(studentCount, _name, _age, _course));
19+
}
20+
21+
// Function to retrieve a student's data by ID
22+
function getStudent(uint _id) public view returns (uint, string memory, uint, string memory) {
23+
require(_id > 0 && _id <= studentCount, "Student ID out of range");
24+
Student memory student = students[_id - 1];
25+
return (student.id, student.name, student.age, student.course);
26+
}
27+
28+
// Fallback function to handle unexpected transactions
29+
fallback() external payable {}
30+
31+
// Receive function to handle direct Ether transfers
32+
receive() external payable {}
33+
34+
// Function to check the balance of the contract
35+
function getBalance() public view returns (uint) {
36+
return address(this).balance;
37+
}
38+
}

0 commit comments

Comments
 (0)