Created
December 1, 2018 08:03
-
-
Save yumatsuoka/b088293d8572728684702d4a314acbda to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* このプログラムでは、コピーコンストラクタを使用して | |
strtypeオブジェクトを関数に渡す */ | |
#include <iostream> | |
#include <cstdlib> | |
#include <string.h> | |
using namespace std; | |
class User{ | |
int id; | |
char *name; | |
char *pass; | |
int balance; | |
public: | |
User(){}; | |
User(const int id); | |
User(const User &user); | |
~User(); | |
void set_n_p(char *name, char *pass); | |
int get_id(); | |
char* get_name(); | |
void check_balance(); | |
void deposit(int add_money); | |
void withdraw(int sub_money); | |
}; | |
// 引数1つのコンストラクタ | |
User::User(const int id){ | |
this->id = id; | |
} | |
// コピーコンストラクタ | |
User::User(const User &user){ | |
id = user.id; | |
balance = user.balance; | |
int l_n; | |
int l_p; | |
l_n = strlen(name)+1; | |
l_p = strlen(pass)+1; | |
name = new char [l_n]; | |
pass = new char [l_p]; | |
if(!name){ | |
cout << "p_nメモリ割り当てエラー\n"; | |
exit(1); | |
} | |
if(!pass){ | |
cout << "p_pメモリ割り当てエラー\n"; | |
exit(1); | |
} | |
strcpy(name, user.name); | |
strcpy(pass, user.pass); | |
} | |
// デストラクタ | |
User::~User(){ | |
delete name; | |
delete pass; | |
} | |
int User::get_id(){ | |
return id; | |
} | |
char* User::get_name(){ | |
return name; | |
} | |
void User::check_balance(){ | |
cout << "残高は," << this->balance << "円になります." << endl; | |
} | |
void User::deposit(int add_money){ | |
this->balance += add_money; | |
} | |
void User::withdraw(int sub_money){ | |
if(this->balance < sub_money){ | |
cout << "残高が不足しています。処理が行えませんでした。" << endl; | |
} | |
else{ | |
this->balance -= sub_money; | |
} | |
} | |
void User::set_n_p(char *temp_name, char *temp_pass){ | |
int l_n; | |
int l_p; | |
l_n = strlen(temp_name) + 1; | |
l_p = strlen(temp_pass) + 1; | |
name = new char [l_n]; | |
pass = new char [l_p]; | |
if(!name){ | |
cout << "p_nメモリ割り当てエラー\n"; | |
exit(1); | |
} | |
if(!pass){ | |
cout << "p_pメモリ割り当てエラー\n"; | |
exit(1); | |
} | |
strcpy(name, temp_name); | |
strcpy(pass, temp_pass); | |
} | |
int select_user(User* users, int num_user){ | |
cout << "ユーザーIDに対応する番号を下記から選択し、入力してください。" << endl; | |
for(int i = 0; i < num_user; i++){ | |
cout << i << ". " << users[i].get_name() << endl; | |
} | |
int target_id; | |
while(true){ | |
cout << "<<"; | |
cin >> target_id; | |
if(0 <= target_id <= num_user-1){ | |
target_id = users[target_id].get_id(); | |
break; | |
} | |
} | |
return target_id; | |
} | |
int main(){ | |
cout << "======= 銀行のATMアプリ =======" << endl; | |
cout << "ユーザを作りますか?(Yes=>1, No=>0)" << endl; | |
int answer; | |
cin >> answer; | |
if(answer){ | |
cout << "何人のユーザを作りますか?(N>1)" << endl; | |
int num_user; | |
cin >> num_user; | |
if (num_user == 0){ | |
// For debug. | |
cout << "ユーザを作ります。" << endl; | |
int temp_id = rand(); | |
User user_1 = User(temp_id); | |
char temp_name[80]; | |
cout << "アカウント名の入力: "; | |
cin >> temp_name; | |
char temp_pass[80]; | |
cout << "パスワードの入力: "; | |
cin >> temp_pass; | |
user_1.set_n_p(temp_name, temp_pass); | |
cout << "アカウント名: " << *user_1.get_name(); | |
cout << ", ユーザのシステムID: " << user_1.get_id() << endl; | |
} | |
else{ | |
User* users = new User[num_user]; | |
for (int i = 0; i < num_user; i++){ | |
int temp_id = rand(); | |
users[i] = User(temp_id); | |
cout << "ユーザ[" << i << "]の"<< endl; | |
char temp_name[80]; | |
cout << "名前の入力: "; | |
cin >> temp_name; | |
char temp_pass[80]; | |
cout << "パスワードの入力: "; | |
cin >> temp_pass; | |
cout << endl; | |
users[i].set_n_p(temp_name, temp_pass); | |
} | |
cout << "ユーザを作成しました。" << endl; | |
cout << "\n行いたい処理を選んでください。" | |
<< "1. お金を預ける, 2. お金を引き出す, 3.残高を確認する, 4.取引をやめる: "; | |
int process_choice; | |
while(true){ | |
cin >> process_choice; | |
//if(process_choice == 1){ | |
if(process_choice == 1){ | |
int target_id = select_user(users, num_user); | |
for(int i = 0; i < num_user; i++){ | |
if(target_id == users[i].get_id()){ | |
cout << users[i].get_name() << "さんが預ける値を入力してください: "; | |
int deposit_value; | |
cin >> deposit_value; | |
users[i].deposit(deposit_value); | |
} | |
} | |
} | |
else if(process_choice == 2){ | |
int target_id = select_user(users, num_user); | |
for(int i = 0; i < num_user; i++){ | |
if(target_id == users[i].get_id()){ | |
cout << users[i].get_name() << "さんが引き出したい値を入力してください: "; | |
int with_value; | |
cin >> with_value; | |
users[i].withdraw(with_value); | |
} | |
} | |
} | |
else if(process_choice == 3){ | |
int target_id = select_user(users, num_user); | |
for(int i = 0; i < num_user; i++){ | |
if(target_id == users[i].get_id()){ | |
cout << users[i].get_name() << "さんの"; | |
users[i].check_balance(); | |
} | |
} | |
} | |
else if(process_choice == 4){ | |
break; | |
} | |
cout << "行いたい処理を選んでください。" | |
<< "1. お金を預ける, 2. お金を引き出す, 3.残高を確認する, 4.取引をやめる:"; | |
} | |
cout << "システムを終了します。ありがとうございました。" << endl; | |
} | |
} | |
else{ | |
cout << "システムを終了します。"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment