In: Computer Science
Using Visual Studio, C# Programming
Lecture: Objects, Inheritance and abstract classes, member init list, shape, circle and cylinder.
Complete Exercises 4 and 5 (100pts)
4. Person and customer classes
Design a class named Person with properties for holding a person's name, address, and telephone number. Next, design a class named Customer, which is derived form the Person class. The Customer class should have a property for a customer number and a Boolean property indicating whether the customer wishes to be on a mailing list, Demonstrate an object of the Customer class in a simple application.
5. PreferredCustomer Class
A retail store has a preferred customer plan where customers can earn discounts on all their purchases. The amount of a customers discount is determined by the amount of the customers cummulative purchases in the store as follows:
- When a preferred customer spends $500, he or she gets a 5 percent discount on all future purchases.
- When a preferred customer spends $1,000, he or she gets a 6 percent discount on all future purchases.
- When a preferred customer spends $1,500, he or she gets a 7 percent discount on all future purchases.
- When a preferred customer spends $2,000, he or she gets a 10 percent discount on all future purchases.
Design a class named PreferredCustomer, which is derived from the Customer class you created in Exercise 4. The PreferredCustomer class should have properties for the amount of the customer's purchases and the customer's discount level. Demonstrate the class in a simple application.
Complete below for 100pts extra credit
Person[] people=new Person[2];
people[0]=new Customer(arguments here);
people[1]=new Preferredcustomer(arguments here);
people[0].calcdiscount();
people[1].calcdiscount();
| use overridden methods to effect polymorphism. | overload operators
to enable them to manipulate objects. |
| determine an
object’s type at execution time. |
create sealed
methods and classes. |
| create abstract
classes and methods. |
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks
Code:
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomerManagement
{
public class Customer : Person
{
public Customer(string name, string address, string phone, string
id,
bool onEmailList)
: base(name, address, phone)
{
CustomerID = id;
OnEmailList = onEmailList;
}
public string CustomerID { get; set; }
public bool OnEmailList { get; set; }
public override double calcDiscount()
{
return 0;
}
public override string ToString()
{
return
String.Format(
"CustomerID: {0}\nCustomer Name: {1}\nCustomerAddress: {2}\n"
+
"Customer Phone: {3} \n" +
"Customer On Email List: {4}\nDiscount: {5}%",
CustomerID, CustomerName, Address, Phone, OnEmailList, 0
);
}
}
}
Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomerManagement
{
public abstract class Person
{
public Person(string name, string address, string phone)
{
CustomerName = name;
Address = address;
Phone = phone;
}
public string CustomerName { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public virtual double calcDiscount(){
return -1;
}
}
}
PreferredCustomer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomerManagement
{
public class PreferredCustomer : Customer
{
public PreferredCustomer(string name, string address, string phone,
string id,
int spentAmount, bool onEmailList)
: base(name, address, phone, id, onEmailList)
{
this.spentAmount = spentAmount;
discountLevel = setDiscountLevel();
}
public double spentAmount { get; set; }
public double discountLevel { get; set; }
public double setDiscountLevel()
{
int range = (int)spentAmount / 500;
switch (range)
{
case 0:
return 0;
case 1:
return 0.05;
case 2:
return 0.06;
case 3:
return 0.07;
default:
return 0.10;
}
}
public override string ToString()
{
return
String.Format(
"CustomerID: {0}\nCustomer Name: {1}\nCustomerAddress: {2}\n"
+
"Customer Phone: {3} \n" +
"Customer Spending: {4:C2}\nCustomer On Email List: {5}\nEligible
for discount: {6}%",
CustomerID, CustomerName, Address, Phone, spentAmount,
OnEmailList,discountLevel*100
);
}
public override double calcDiscount()
{
setDiscountLevel();
return discountLevel;
}
}
}
Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomerManagement
{
class Program
{
static void Main(string[] args)
{
Person[] people=new Person[2];
people[0] = new Customer("Cust", "NYC", "98909", "1", false);
people[1]=new PreferredCustomer("Preffered Cust","NYC","98809","2",6000,true);
Console.WriteLine("Discount for normal customer
:"+people[0].calcDiscount()*100 + " %");
Console.WriteLine("Discount for preferred customer :" + people[1].calcDiscount() * 100 + " %\n\n");
Console.WriteLine(people[0]);
Console.WriteLine(people[1]);
Console.ReadKey();
}
}
}
Output:
