Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ros assignmnet #77

Open
wants to merge 8 commits into
base: ayush_g
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 290 additions & 0 deletions OOPS/bank_accound.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
#include<iostream>
#include<string.h>
#include <string>
#include <cstdlib> // For rand() function
using namespace std;

class Statement {
private:
double amount;
string transfer_type;
double Amount_flow;

public:
Statement(string desc, double amt) {
transfer_type = desc;
amount = amt;
}
void getDetails() {
cout << "Description: " << transfer_type << endl;
cout << "Amount: $" << amount << endl;
}
};

class Bank_Account {
protected:
string Account_type;
double Balance;
Statement** statements;
int numStatements;

public:
long Account_No;

Bank_Account(int id, string type, double openingBalance) {
Account_No = id; // Account number is provided during account creation
Account_type = type;
Balance = openingBalance;
numStatements = 0;
statements = new Statement*[100];
}

virtual ~Bank_Account() {
for (int i = 0; i < numStatements; ++i) {
delete statements[i];
}
delete[] statements;
}

string getAccountType() const {
return Account_type;
}

void addStatement(const Statement& statement) {
statements[numStatements++] = new Statement(statement);
}

void viewStatements() const {
cout << "----- Statements for Account ID: " << Account_No << " -----" << endl;
for (int i = 0; i < numStatements; ++i) {
statements[i]->getDetails();
cout << endl;
}
}

void deposit(double amount);
bool withdraw(double amount);
};

bool Bank_Account::withdraw(double amount) {
if (Account_type == "Savings" && amount > 200000) {
cout << "Savings account cannot withdraw more than 200000 at a time." << endl;
return false;
}

if (Balance >= amount) {
Balance -= amount;
addStatement(Statement("Withdrawal", -amount));
cout << "Withdrawal of " << amount << " successful. New balance: " << Balance << endl;
return true;
} else {
cout << "Insufficient funds." << endl;
return false;
}
}

class Savings_Account : public Bank_Account {
private:
float Interest_Rate;
const int Upper_Transcation_Limit = 2000000;

public:
Savings_Account(long accountNo) : Bank_Account(accountNo, "Savings", 0) {}

float getInterestRate() {
return Interest_Rate;
}

float setInterestRate(float interestrate) {
if (interestrate > 6) {
cout << "Max interest rate is 6";
} else {
Interest_Rate = interestrate;
}
return Interest_Rate;
}

~Savings_Account() {}
};

class Current_Account : public Bank_Account {
private:
const float Interest_Rate = 0;

public:
Current_Account(long accountNo) : Bank_Account(accountNo, "Current", 0) {}

float getInterestRate() {
throw ("Interest Rate is 0");
}

~Current_Account() {}
};

class Bank_Account_Holder {
private:
string username;
string password;
Bank_Account** list;
int capacity;
int numofAccounts;

public:
Bank_Account_Holder(string name, string passwd) {
username = name;
password = passwd;
capacity = 100;
numofAccounts = 0;
list = new Bank_Account*[capacity];
};

void changepasswd(string passwd);
void createAccount(string accounttype);

Savings_Account* get_savingsAccount(long accountId);
Current_Account* get_currentAccount(long accountId);
};

void Bank_Account_Holder::changepasswd(string passwd) {
password = passwd;
}

void Bank_Account_Holder::createAccount(string accounttype) {
if (numofAccounts < capacity) {
long accountNo = rand() % 900000000 + 100000000; // Generate a random account number
if (accounttype == "Savings") {
list[numofAccounts++] = new Savings_Account(accountNo);
cout << "Savings account created with account number: " << accountNo << endl;
} else if (accounttype == "Current") {
list[numofAccounts++] = new Current_Account(accountNo);
cout << "Current account created with account number: " << accountNo << endl;
}
} else {
cout << "Maximum account limit reached." << endl;
}
}

Savings_Account* Bank_Account_Holder::get_savingsAccount(long accountId) {
for (int i = 0; i < numofAccounts; ++i) {
if (list[i]->Account_No == accountId && list[i]->getAccountType() == "Savings") {
return dynamic_cast<Savings_Account*>(list[i]);
}
}
return nullptr;
}

Current_Account* Bank_Account_Holder::get_currentAccount(long accountId) {
for (int i = 0; i < numofAccounts; ++i) {
if (list[i]->Account_No == accountId && list[i]->getAccountType() == "Current") {
return dynamic_cast<Current_Account*>(list[i]);
}
}
return nullptr;
}

void Bank_Account::deposit(double money) {
Balance += money;
addStatement(Statement("Deposit", money));
}

int main() {
Bank_Account_Holder accountHolder("Ayush", "password123");

// Main menu
int choice;
do {
cout << "\nBanking System Menu\n";
cout << "1. Create Savings Account\n";
cout << "2. Create Current Account\n";
cout << "3. Deposit\n";
cout << "4. Withdraw\n";
cout << "5. View Statements\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
accountHolder.createAccount("Savings");
} else if (choice == 2) {
accountHolder.createAccount("Current");
} else if (choice == 3) {
long accountNumber;
double amount;
string Accounttype;
cout << "Enter account type Saving/Current: ";
cin >> Accounttype;
cout << "Enter account number: ";
cin >> accountNumber;
cout << "Enter amount to deposit: ";
cin >> amount;
if (Accounttype == "Savings") {
Savings_Account* account = accountHolder.get_savingsAccount(accountNumber);
if (account != nullptr) {
account->deposit(amount);
} else {
cout << "Savings account with given account number not found." << endl;
}
} else if (Accounttype == "Current") {
Current_Account* account = accountHolder.get_currentAccount(accountNumber);
if (account != nullptr) {
account->deposit(amount);
} else {
cout << "Current account with given account number not found." << endl;
}
}
} else if (choice == 4) {
long accountNumber;
double amount;
string Accounttype;
cout << "Enter account type Saving/Current: ";
cin >> Accounttype;
cout << "Enter account number: ";
cin >> accountNumber;
cout << "Enter amount to withdraw: ";
cin >> amount;
if (Accounttype == "Savings") {
Savings_Account* account = accountHolder.get_savingsAccount(accountNumber);
if (account != nullptr) {
account->withdraw(amount);
} else {
cout << "Savings account with given account number not found." << endl;
}
} else if (Accounttype == "Current") {
Current_Account* account = accountHolder.get_currentAccount(accountNumber);
if (account != nullptr) {
account->withdraw(amount);
} else {
cout << "Current account with given account number not found." << endl;
}
}
} else if (choice == 5) {
long accountNumber;
string Accounttype;
cout << "Enter account type Saving/Current: ";
cin >> Accounttype;
cout << "Enter account number: ";
cin >> accountNumber;
if (Accounttype == "Savings") {
Savings_Account* account = accountHolder.get_savingsAccount(accountNumber);
if (account != nullptr) {
account->viewStatements();
} else {
cout << "Savings account with given account number not found." << endl;
}
} else if (Accounttype == "Current") {
Current_Account* account = accountHolder.get_currentAccount(accountNumber);
if (account != nullptr) {
account->viewStatements();
} else {
cout << "Current account with given account number not found." << endl;
}
}
} else if (choice == 6) {
cout << "Have a good Day" << endl << "Exiting...";
} else {
cout << "Invalid choice. Please enter a number from 1 to 6." << endl;
}
} while (choice != 6);

return 0;
}
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# Induction_Y23
Induction Assignments and resources for Y23
I have created two files name ftpserver and ftp client

From client we can perform tasks like storing the file on server, retriving the file from server, listing all the files already present in server

Storing the file to server, retriving it from server along with list function work completely fine

Whenever a client connects to the server it first ask username. If username is already present in the list that it asks for password and if it matches the useranme password key present in the fserver it lets the userin

Also if username is not already listed, he can create a new user.

Problem with this code is that it can not handle exceptions for ex. file that you want to transfer to server , if its not present in your working directory it will give error andget stuck midway.

It will work if user give the right set of command. like to store a file in server the file, the client has to write the right filename or my code won't work

I am unable to do this exception handling

I have also tried making admin. For that i created a new python file named admin. But integrating it with server and client was problem as the code got stuck midway due to some improper aapplication of threading(i guess)

You can check the integrated code that does not work its named ftpserver copy and ftp client copy.py
47 changes: 47 additions & 0 deletions admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import socket
import threading
admin = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

IP_ADDR = socket.gethostbyname(socket.gethostname())
port = 1234

admin.connect((IP_ADDR,port))

print(f"Server is listening on {IP_ADDR}:{port}")

admin.send("ADMIN".encode())

if __name__ == "__main__":
print("Welcome to the Server. Please follow below commands:")
print("To ADD a user: ADD")
print("To DELETE a user : DEL")
print("To BAN a user: BAN")
print("To UNBAN a user: UNBAN")
while True:
command = input("Enter the command: ")
if command == "ADD":
admin.send("ADD".encode())
username= input("Enter username you want to add: ")
admin.send(username.encode())
passwd=input("Enter the passwd for the username: ")
admin.send(passwd.encode())

elif command == "DEL":
admin.send("DEL".encode())
username= input("Enter username you want to add: ")
admin.send(username.encode())

elif command=="BAN":
admin.send("BAN".encode())
username= input("Enter username you want to add: ")
admin.send(username.encode())

elif command == "UNBAN":
admin.send("UNBAN".encode())
username= input("Enter username you want to add: ")
admin.send(username.encode())

else:
print("The given command is wrong")
break

21 changes: 21 additions & 0 deletions different_world_simulation (2).launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>

<launch>
<!-- Declare the laser_enabled argument and set its value to true -->


<!-- Include the smb_gazebo.launch file and pass the laser_enabled argument -->
<include file="$(find smb_gazebo)/launch/smb_gazebo.launch">
<arg name="laser_enabled" default="true" />
<arg name="world_file" value="/usr/share/gazebo-11/worlds/robocup14_spl_field.world"/>

</include>

<!-- Node configuration -->
<node name="SmbHighlevelController" pkg="smb_highlevel_controller" type="smb_highlevel_controller" output="screen">
<rosparam command="load" file="$(find smb_highlevel_controller)/config/param.yaml"/>
</node>
<node type="rviz" name="rviz" pkg="rviz" args="-d $(find smb_highlevel_controller)/rviz/default.rviz" />

</launch>

Loading