永发信息网

C#应用程序编程

答案:1  悬赏:40  手机版
解决时间 2021-04-14 06:20
  • 提问者网友:戎马万世
  • 2021-04-13 12:58

编写一个程序,用来模拟银行帐户的基本操作,如帐户开户的话,则最低存款额为100、存取现金操作以及在使用任意修改余额后都可以随时查看帐户余额。请使用重载的带参数的构造函数。

最佳答案
  • 五星知识达人网友:想偏头吻你
  • 2021-04-13 14:18

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace test2_1
{
class Account
{
private decimal balance;
private decimal currentInterestRate;
private decimal totalInterestPaid;
public Account()
{
balance = 0;
currentInterestRate = 0;
totalInterestPaid = 0;
}


public void SetInterestRate(decimal newInterestRate)
{
currentInterestRate = newInterestRate;
}
public decimal GetInterestRate()
{
return currentInterestRate;
}
public void UpdateInterest()
{
totalInterestPaid += balance * currentInterestRate;
balance += balance * currentInterestRate;
}
public void Withdraw(decimal amount)
{
balance = amount;
}
public void Deposit(decimal amount)
{
balance += amount;
}
public decimal GetBalance()
{
return balance;
}
public decimal GetTotalInterestPaid()
{
return totalInterestPaid;
}
}
class Bank
{
private Account[] accounts;
public Bank()
{
Console.WriteLine("Congratulations!You have created a new bank!");
Console.Write("Please enter number of accounts in bank:");
accounts=new Account[Convert.ToInt32(Console.ReadLine())];
for (int i = 0; i < accounts.Length; i++)
{
accounts[i] = new Account();
}
}
public void Deposit()
{
int accountNumber;
decimal amount;
Console.WriteLine("Deposit,Please enter account number:");
accountNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter amount to deposit:");
amount = Convert.ToDecimal(Console.ReadLine());
accounts[accountNumber - 1].Deposit(amount);
Console.WriteLine("New balance of account {0}:{1:C}",accountNumber,accounts[accountNumber-1].GetBalance());
}
public void Withdraw()
{
int accountNumber;
decimal amount;
Console.Write("Withdraw.Please enter amount number:");
accountNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter amount to withdraw:");
amount = Convert.ToDecimal(Console.ReadLine());
accounts[accountNumber--].Withdraw(amount);
Console.WriteLine("New balance of account {0}:{1:C}",accountNumber,accounts[accountNumber-1].GetBalance());
}
public void SetInterestRate()
{
int accountNumber;
decimal newInterestRate;
Console.Write("Set interest rate.Please enter account number:");
accountNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter interest rate:");
newInterestRate = Convert.ToDecimal(Console.ReadLine());
accounts[accountNumber - 1].SetInterestRate(newInterestRate);
}
public void PrintAllInterestRates()
{
Console.WriteLine("Interest rates for all accounts:");
for (int i = 0; i < accounts.Length; i++)
{
Console.WriteLine("Account{0,-3}:{1,-10}",(i+1),accounts[i].GetInterestRate());
}
}
public void PrintAllBalance()
{
Console.WriteLine("Account balances for all accounts:");
for (int i = 0; i < accounts.Length; i++)
{
Console.Write("Account{0,-3}:{1,12:C}",(i+1),accounts[i].GetBalance());
}
}
public void PrintTotalInterestPaidAllAccounts()
{
Console.WriteLine("Total interest paid for each indicidual account:");
for (int i = 0; i < accounts.Length; i++)
{
Console.WriteLine("Account {0,-3}:{1,12:C}",(i+1),accounts[i].GetTotalInterestPaid());
}
}
public void UpdateInterestAllAccounts()
{
for (int i = 0; i < accounts.Length; i++)
{
Console.WriteLine("Interest added to account number {0,-3}:{1,12:C}",(i+1),accounts[i].GetBalance()*accounts[i].GetInterestRate());
accounts[i].UpdateInterest();
}
}
}
class BankSimulation
{
private static Bank bigBucksBank;


public static void Main()
{
string command;
bigBucksBank = new Bank();
do
{
PrintMenu();
command = Console.ReadLine().ToUpper();
switch (command)
{
case "D":
bigBucksBank.Deposit();
break;
case "W":
bigBucksBank.Withdraw();
break;
case "S":
bigBucksBank.SetInterestRate();
break;
case "U":
bigBucksBank.UpdateInterestAllAccounts();
break;
case "P":
bigBucksBank.PrintAllBalance();
break;
case "T":
bigBucksBank.PrintTotalInterestPaidAllAccounts();
break;
case "I":
bigBucksBank.PrintAllInterestRates();
break;
case "E":
Console.WriteLine("Bye Bye!");
break;
default:
Console.WriteLine("Invalid choice");
break;
}
} while (command != "E");
}
private static void PrintMenu()
{
Console.WriteLine("\nWhat would you like to do?\n"+
"D)eposit\n"+
"W)ithdraw\n"+
"S)etinterest rate\n"+
"U)pdate all accounts for interest\n"+
"P)rint all balances\n"+
"T)otal interest paid printed for all accounts\n"+
"I)nterest rates printed for all accounts\n"+
"E)nd session\n"+
"Node:Forst account has account number one");
}
}
}
这个是我们书上的一个模拟银行的代码,楼主拿去看看,修改下应该可以达到你的要求!这种程序代码这么长恐怕没人会给你写!

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯