You are on page 1of 13

GUDITO, DESSA N.

BSME-MECH-2 METE280 - P1

WEEK 4 EXERCISE:
Class Encapsulation

Problem 1:

Define a Name class


Name’s attributes: firstname, middlename, lastname
Name’s behavior: display all the attributes of Name’s class

Define a DebitCard class


DebitCard’s attributes: card number, name,balance.
DebitCard’s behavior: float inquireBalance()
o //return the balance
float depositCash(float amount)
o //increases the balance with amount
boolean withdrawCash(float amount)
o // returns true if sufficient amount is available for withdrawal and
decreases the balance by amount
float interest(float rate)
o //calculates the interest rate incurred.
Assume the parameter rate is considered as annual interest.
Update the balance available.
Display DebitCard attributes
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Exercise4_Debit_Card
{
class Name
{
private string firstname, middlename, lastname;

public Name()
{

}
public Name(string firstname, string middlename, string lastname)
{
this.firstname = firstname;
this.middlename = middlename;
this.lastname = lastname;
}
public void setFirstname(string firstname)
{
this.firstname = firstname;
}
public void setMiddlename(string middlename)
{
this.middlename = middlename;
}
public void setLastname(string lastname)
{
this.lastname = lastname;
}
public string getFirstname()
{
return firstname;
}
public string getMiddlename()
{
return middlename;
}
public string getLastname()
{
return lastname;
}
public void displayName()
{
Console.WriteLine("\n Name: " + getFirstname() + " " + getMiddlename() + " "
+ getLastname());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Exercise4_Debit_Card
{
class DebitCard
{
private string cardnum;
private double balance;
private Name fullname = new Name();

public DebitCard()
{
}
public DebitCard(string cardnum, double balance, Name fullname)
{
this.cardnum = cardnum;
this.balance = balance;
this.fullname = fullname;
}
public void setCardnum(string cardnum)
{
this.cardnum = cardnum;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public string getCardnum()
{
return cardnum;
}
public double getBalance()
{
return balance;
}
public double bal_inquiry()
{
return balance;
}
public void cash_deposit(double amount)
{
balance = balance + amount;
}

public bool cash_withdraw(double amount)


{
if (balance < amount)
{
return false;
}
else
{
balance = balance - amount;
return true;
}
}
public double interest(double rate)
{
double increase = balance * (rate / 1200);
balance = balance + increase;
return increase;
}

public void displayAttributes()


{
fullname.displayName();
Console.WriteLine(" Account Number: " + getCardnum());
Console.WriteLine(" Balance: " + getBalance().ToString("0.00"));
}
}
}
using System;

namespace Exercise4_Debit_Card
{
class Program
{
static void Main(string[] args)
{
char option = '0';
double initial, amount, rate;
string account;
DebitCard Debit = new DebitCard();
Name Fullname = new Name();

Console.Write("\n Enter name:\n\tFirst name: ");


Fullname.setFirstname(Console.ReadLine());
Console.Write("\tMiddle name: ");
Fullname.setMiddlename(Console.ReadLine());
Console.Write("\tLast name: ");
Fullname.setLastname(Console.ReadLine());
Console.Write(" Enter account number: ");
account = Console.ReadLine();
Console.Write(" Enter beginning balance: ");
initial = Convert.ToDouble(Console.ReadLine());

Debit = new DebitCard(account, initial, Fullname);

while (option != '5')


{
Console.WriteLine("\n DEBIT CARD TRANSACTION\n [1] Deposit Cash\n
[2] Withdraw Cash\n [3] Inquire Balance\n [4] Calculate Interest Rate\n [5] Exit");
Console.Write("\n Your choice: ");
option = Convert.ToChar(Console.ReadLine());

if (option == '1')
{
Console.Write(" Enter amount: ");
amount = Convert.ToDouble(Console.ReadLine());
Debit.cash_deposit(amount);
Debit.displayAttributes();
Console.WriteLine("\n*********Press ENTER for another transaction");
}
else if (option == '2')
{
Console.Write(" Enter amount: ");
amount = Convert.ToDouble(Console.ReadLine());
if (Debit.cash_withdraw(amount))
{
Debit.displayAttributes();
Console.WriteLine("\n*********Press ENTER for another
transaction");
}
else
{
Console.WriteLine("\n INSUFFICIENT FUNDS.");
Debit.displayAttributes();
Console.WriteLine("\n*********Press ENTER for another
transaction");
}
}
else if (option == '3')
{
Debit.displayAttributes();
Console.WriteLine("\n*********Press ENTER for another transaction");
}
else if (option == '4')
{
Console.Write(" Enter rate: ");
rate = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(" Interest incurred: " +
Debit.interest(rate).ToString("0.00"));
Debit.displayAttributes();
Console.WriteLine("\n*********Press ENTER for another transaction");
}
else if (option != '5')
{
Console.WriteLine("\n INVALID INPUT");
Console.WriteLine("\n*********Press ENTER for another transaction");
}
Console.ReadKey();
}
Console.Write("\n*********THANK YOU for your transaction!");
Console.ReadLine();
}
}
}

You might also like