Demo entry 6353083

Class and Polymorphisim

   

Submitted by anonymous on Mar 28, 2017 at 18:53
Language: C#. Code size: 4.4 kB.

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

namespace Libaray 
{
    public class Name
    {
        private String first;
        private char initial;
        private String last;
        public Name(String f, String l)
        {
            first = f;
            last = l;
        }
        public Name (String f, char i, String l) : this(f,l)
        {
            initial = i;
        }
        public override string ToString()
        {
            if (initial == "\u0000")
                return first + " " + last;
            else
                return first + " " + initial + " " + last;
        }
    }
    public class Address
    {
        private String street;
        private string city;
        private String state;
        private string zip;
        public Address(String st, String cy, String se, String zp)
        {
            street = st; 
            city = cy;
            state = se;
            zip = zp;
        }
        public override string ToString()
{
      return street + "\n" + city + ", " + state + " " + zip;
}
         public class Person 
    {
        private string id;
        public Name name;
        private Address address;

    
  
          public Person(String i, Name n, Address a)
{
    id = i;
    name = n;
    address = a;
}
    }
        public String getId()
        {
            return id;
        }
        public override string ToString()
        {
            return namespace + "\n" + address;
        }
   
}
    public class Account
    {
        private double balance;
        public Person holder;
        public Account (Person p) : this(0,p) {}
        public Account (double initialAmount, Person p)
        {
            balance = initialAmount;
            holder = p;

        }
        public String GetId()
        {
            return holder.GetId();
        }
        public void Deposit (double amount)
        {
            balance += amount;
        }
        public virtual double Withdraw(double amount)
        {
            if (balance >= amount)
            {
                balance -= amount;
                return balance;
            }
            else 
                return -1.0;
        }
        public double GetBalance()
        {
            return balance;
        }
    }
public class Checking : Account 
{
    private double minBalance;
    private double charge;
    public Checking(Person p, double minAmount, double charge) : base(p)
    {
        minBalance = minAmount;
        this.charge = charge
    }
    public double PrcessCheck(double amount)
    {
        if(GetBalance() >= minBalance)
            return base.Withdraw(amount);
        else
            return base.Withdraw(amount + charge);
    }
    public override double Withdraw(double amount)
{
      return ProcessCheck (amount);
}
}
public class Savings:Account
{
    private double interest;
    public Savings(double amount, Person p, double rate): base(amount, p)
    {
        interestRate = rate;
    }
    public void PostInterest()
    {
        double balance = GetBalance();
        double interest = interestRate / 100 * balance;
        Deposit(balance + interest);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Libaray;
namespace Assignment
{
Class Program
{
  public static Person [] PersonData()
{
Name n1 = new Name(Justin, Young);
Address a1 = new address(1234 Seasme Street, Philidaepha, United States, 1234);
Person p1 = new Person (90210, n2, a2);
Person[] people = new Person[2];
people[0];
return people.n1 + people.a1 + people.p1;
}
public static Account[] AccountData()
{
Person[] people = PersonData();
Account c1 = new Checking(people[0], 50.0, 0.0);
Account s1 = new Savings(5000.0, people[0], 4.0);
} 
Public static 
        static void Main(string[] args)
        {
Account[] data = Accountdata();
data[0].Deposit(23567.04);
data[2].Deposit(902.10);
Console.WriteLine(“\tAccount 1:);
Console.WriteLine(type: Checking);
Console.WriteLine(holder: {0}, data[0].holder);
Console.WriteLine(balance: {0:C}\n, data[0].GetBalance());

        }

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).