我正在开发一个项目,该项目应该允许您创建表格的3个帐户(名称,帐户编号,余额),然后为您提供以下选项:
1)添加一个帐户。
2)关闭现有帐户。
3)存入金额。
4)撤回帐户
5)没有请求。
到目前为止,我能够成功创建3个帐户,显示它们,添加另一个帐户,再次显示它们,删除帐户并再次显示它们。但是,我无法找到从特定帐户存款或取款的方法。 所以要明确的是,我的问题是:如何访问所选帐户的余额并存入/取出金额(请记住我使用向量来保存所有帐户)?
以下是我的代码:
的main.cpp
#include <iostream>
#include <vector>
#include "bank.hpp"
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
void fillaccounts(vector<bank>&);
void printaccounts(const vector<bank>&);
void createaccount(vector<bank>&);
void depositaccount(vector<bank>&);
void deleteaccount (vector<bank>&);
int main() {
vector<bank> mybank;
fillaccounts(mybank);
for (int i=0;i<5;i++) {
cout << "Please choose one of the following requests: " << endl;
cout << "(1) Create a new account." << endl;
cout << "(2) Close an existing account." << endl;
cout << "(3) Deposit an amount in one of your accounts." << endl;
cout << "(4) Withdraw an amount from one of your accounts." << endl;
cout << "(5) No request." << endl;
cout << "Enter your choice here: " << endl;
int request;
cin >> request;
switch (request) {
case 1:
createaccount(mybank);
printaccounts(mybank);
break;
case 2:
deleteaccount(mybank);
printaccounts(mybank);
break;
case 3:
depositaccount(mybank);
printaccounts(mybank);
break;
case 4:
break;
case 5:
break;
}
}
}
void fillaccounts(vector<bank>& newmybank) {
string name;
int accountnumber;
double balance;
for (int i=0; i<3;i++) {
cout << "Enter your first name: " << endl;
cin >> name;
cout << "Enter account number: " << endl;
cin >> accountnumber;
cout << "Enter the amount you want to deposit: " << endl;
cin >> balance;
bank newAccount(name, accountnumber,balance);
newmybank.push_back(newAccount);
cout << endl;
}
cout << endl;
}
void createaccount(vector<bank>& newmybank) {
string name;
int accountnumber;
double balance;
cout << "Enter your first name: " << endl;
cin >> name;
cout << "Enter account number: " << endl;
cin >> accountnumber;
cout << "Enter the amount you want to deposit: " << endl;
cin >> balance;
bank newAccount(name,accountnumber,balance);
newmybank.push_back(newAccount);
cout << endl;
}
void printaccounts(const vector<bank>& newmybank) {
for (unsigned int i=0; i<newmybank.size();i++) {
cout << "(" << newmybank[i].getname() << "," << newmybank[i].getaccountnumber() << "," << newmybank[i].getbalance() << ")" << endl;
}
cout << endl;
}
void deleteaccount(vector<bank>& newmybank) {
string name;
cout << "Please enter your name to delete your account: " << endl;
cin >> name;
cout << endl;
for (int i=0; i<newmybank.size(); i++) {
if (newmybank[i].newname == name) {
newmybank.erase(newmybank.begin()+i);
}
}
}
Bank.cpp
#include "Bank.hpp"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bank::bank() {
newname=' ';
newaccountnumber=0;
newbalance=0.0;
}
bank::bank(string name,int accnum,double amount) {
newname=name;
newaccountnumber=accnum;
newbalance=amount;
}
string bank::getname() const {
return newname;
}
int bank::getaccountnumber() const {
return newaccountnumber;
}
double bank::getbalance() const {
return newbalance;
}
void bank::setname(string name) {
newname=name;
}
void bank::setaccountnumber(int accountnumber) {
newaccountnumber=accountnumber;
}
void bank::setbalance(double balance) {
newbalance=balance;
}
Bank.hpp
class bank {
public:
string newname;
int newaccountnumber;
double newbalance;
bank(); //Constructor
bank(string,int,double); //Overload Constructor
string getname() const;
int getaccountnumber() const;
double getbalance() const;
void setname(string);
void setaccountnumber(int);
void setbalance(double);
};
有关如何跟进的任何建议? 谢谢你们!
答案 0 :(得分:1)
执行此操作的一种方法是向您的银行类添加一项功能,该功能会向帐户余额添加特定金额,然后在您想要更改帐户值(即存入/取款)或创建2时调用此方法单独的方法。
类似的东西:
void bank::deposit(int num)
{
newbalance += num;
}
然后,您将有一个用户指定银行帐号(或名称),因为我认为您不希望更改每个帐户的每个余额。函数getAccountIndex
可能遍历每个帐户以查找指定的数据,然后返回向量中的索引。
当您拥有索引时,您可以像deposit
mybank[index].deposit(amount);
方法
请注意,bank
类中的变量是公共的,因此您不需要获取/设置方法,但这被认为是不好的做法,因此您应该将这些变量设为私有。