In: Computer Science
In C# thanks please,
Design a class named Person with properties for holding a person’s name, address, and telephone number.
Design a class named Customer, which is derived from the Person class. The Customer class should have the variables and properties for the customer number, customer email, a spentAmount of the customer’s purchases, and a Boolean variable indicating whether the customer wishes to be on a mailing list. It also includes a function named calcAmount that calculates the spentAmount.
All retail store has a preferred customer plan where customers can earn discounts on all their purchases. The amount of a customer’s discount is determined by the amount of the customer’s cumulative purchases in the store as follows:
Design a class named PreferredCustomer, which is derived from the Customer class. The PreferredCustomer class should have a variable, discountLevel, with a read-only property. It alsoincludes a setDiscountLevel function that determine the discount level based on the purchases amount using switch statement and an override function, calcAmount, calculates the spentAmount with the current discount level.
Create a CustomerDemo class. In the main function, the program calls the getData function to read the data from the “CustomerInfo.txt” file and create a dynamic array of PreferredCustomer object. Then, it prompts user to enter a customer number and displays a menu:
After update the spent Amount, the program writes the updated information back to file.
Example for customer info.txt below (there are 5 persons)
Kyle Jones
879 hobbs st. boston,MA 84758
456-789-0001
JLA9876A
3000
true
.
.
.
.
SOLUTION -
I have solve the problem in C# code with comments and screenshot for easy understanding :)
Screenshot
Program
using System;
using System.IO;
namespace CustomerDemo
{
//MAin Program
class Program
{
static void
Main(string[] args)
{
//Array to store customers
PrefferedCostumer[] pc = new PrefferedCostumer[] { };
pc=getData();
//Loop until exit
while (true)
{
//Each option
Console.WriteLine("OPTIONS:-\n1. Display Cutomer Information\n2.
Update Spend Amount\n3. Exit");
int opt = Convert.ToInt32(Console.ReadLine());
//ErrorEventArgs check
while(opt<1 || opt > 3)
{
Console.WriteLine("\nIncorrect option|||\n");
Console.WriteLine("\nOPTIONS:-\n1. Display Cutomer Information\n2.
Update Spend Amount\n3. Exit");
opt = Convert.ToInt32(Console.ReadLine());
}
//Execute options
if (opt == 1)
{
bool check = false;
//Get customer number
Console.WriteLine("\nEnter customer number: ");
string number = Console.ReadLine();
//If match display details
for(int i = 0; i < pc.Length; i++)
{
if (pc[i].GetNumber.CompareTo(number) == 0)
{
Console.WriteLine(pc[i]);
check = true;
break;
}
}
if (check == false)
{
Console.WriteLine("\nNot found!!!\n");
}
}
else if (opt == 2)
{
bool check = false;
//Get customer number
Console.WriteLine("\nEnter customer number: ");
string number = Console.ReadLine();
//If match get cutomer spend amout and write into file
for (int i = 0; i < pc.Length; i++)
{
if (pc[i].GetNumber.CompareTo(number) == 0)
{
Console.WriteLine("Enter spend amount: ");
double amt = Convert.ToDouble(Console.ReadLine());
pc[i].GetSpend = amt;
pc[i].setDiscount();
pc[i].calculateAmt();
using (System.IO.StreamWriter file = new
System.IO.StreamWriter(@"C:/Users/deept/source/repos/CustomerDemo/CustomerDemo/CustomerInfo.txt"))
{
file.WriteLine(pc.Length);
foreach (PrefferedCostumer p in pc)
{
file.WriteLine(p);
}
}
check = true;
break;
}
}
if (check == false)
{
Console.WriteLine("\nNot found!!!\n");
}
}
else if (opt == 3)
{
break;
}
}
}
//Method read data from
file and store into array and return array
static
PrefferedCostumer[] getData()
{
PrefferedCostumer p=new PrefferedCostumer();
PrefferedCostumer[] pc = new PrefferedCostumer[] { };
using (StreamReader file = new
StreamReader("C:/Users/deept/source/repos/CustomerDemo/CustomerDemo/CustomerInfo.txt"))
{
int counter = 0,i=0;
string ln;
int cnt = Convert.ToInt32(file.ReadLine());
pc = new PrefferedCostumer[cnt];
while ((ln = file.ReadLine()) != null)
{
if (i == 0)
{
p.GetName = ln;
i++;
}
else if (i == 1)
{
p.GetAddress = ln;
i++;
}
else if (i == 2)
{
p.GetPhone = ln;
i++;
}
else if (i == 3)
{
p.GetNumber = ln;
i++;
}
else if (i == 4)
{
p.GetEmail = ln;
i++;
}
else if (i == 5)
{
p.GetSpend = Convert.ToDouble(ln);
i++;
}
else if (i == 6)
{
p.GetMail =ln;
pc[counter] = new
PrefferedCostumer(p.GetName,p.GetAddress,p.GetPhone,p.GetNumber,p.GetEmail,p.GetSpend,bool.Parse(p.GetMail));
i=0;
counter += 1;
}
}
file.Close();
return pc;
}
}
}
//Base class
class Person
{
//Attributes
private string name,
phoneNum, address;
// Default
Constructor
public Person()
{
name = phoneNum = address = "";
}
//Parameterized
constructor
public Person(string
n,string a,string p)
{
name = n;
phoneNum = p;
address = a;
}
//Getters and
setters
public string
GetName
{
get { return name; }
set
{
name = value;
}
}
public string
GetPhone
{
get { return phoneNum; }
set
{
phoneNum = value;
}
}
public string
GetAddress
{
get { return address; }
set
{
address = value;
}
}
//Get details as
string
public override string
ToString()
{
return name + "\n" + address + "\n" + phoneNum ;
}
}
//Derived class
class Customer : Person
{
//Attributes
private string
number;
private string
email;
private double
spendAmt;
private bool
mailingListWish;
//Default
constructor
public Customer():
base()
{
number="";
email="";
spendAmt=0;
mailingListWish=false;
}
//DParameterized
constructor
public Customer(string
n, string a, string p,string num,string em,double sAmt,bool wish) :
base(n,a,p)
{
number = num;
email = em;
spendAmt = sAmt;
mailingListWish = wish;
}
//Getters and
setters
public string
GetNumber
{
get { return number;}
set { number = value; }
}
public string
GetEmail
{
get { return email; }
set { email = value; }
}
public double
GetSpend
{
get { return spendAmt; }
set { spendAmt = value; }
}
public string
GetMail
{
get { if (mailingListWish) return "true"; else return "false";
}
set { if (value=="true") mailingListWish=true; else
mailingListWish=false; }
}
//Details a string
representation
public override string
ToString()
{
return base.ToString() + "\n" + number + "\n" + email + "\n" +
spendAmt.ToString() + "\n" + GetMail;
}
//Overriding function
for spend amount calculation
public virtual void
calculateAmt() { }
}
//Derived class
class PrefferedCostumer : Customer
{
//Attribute
private double
discountLevel;
//Constructors
public
PrefferedCostumer() : base() { }
public
PrefferedCostumer(string n, string a, string p, string num, string
em, double sAmt, bool wish) : base(n,a,p,num,em,sAmt,wish) {
}
//Read only
property
public double
GetDiscount { get { return discountLevel; } }
//SetDiscount
property
public void
setDiscount()
{
switch (base.GetSpend)
{
case 500:
discountLevel = .05;
break;
case 1000:
discountLevel = .06;
break;
case 1500:
discountLevel = .08;
break;
case 2000:
discountLevel = .10;
break;
default:
discountLevel = 0;
break;
}
}
//Calculate spend
amount
public override void
calculateAmt() { base.GetSpend = base.GetSpend - (base.GetSpend *
discountLevel); }
}
}
The CustomerInfo.txt file is:
5 Anna Alaniz 123 10th st, McAllen, TX 78504 956-456-7899 A0000001 aalaniz @ g mail.com 2500 true Bob Ho 456 15th st, McAllen, TX 78504 956-456-1000 A0000002 bobho @ g mail.com 2500 true Hector Alcoser 2004 44th st, McAllen, TX 78501 956-456-5555 A0000003 hAlcoser @ g mail.com 495 false Jorge Lee 123 10th st, McAllen, TX 78504 956-555-6666 A0000004 jlee @ g mail.com 1200 true Pablo Gonzalez 777 2th st, McAllen, TX 78502 956-100-2000 A0000005 pGonzalez @ yahoo.com 1750 false
Output
OPTIONS:-
1. Display Cutomer Information
2. Update Spend Amount
3. Exit
1
Enter customer number:
A0000001
Anna Alaniz
123 10th st, McAllen, TX 78504
956-456-7899
A0000001
aalaniz @ g mail.com
2500
true
OPTIONS:-
1. Display Cutomer Information
2. Update Spend Amount
3. Exit
2
Enter customer number:
A0000001
Enter spend amount:
2600
OPTIONS:-
1. Display Cutomer Information
2. Update Spend Amount
3. Exit
1
Enter customer number:
A0000001
Anna Alaniz
123 10th st, McAllen, TX 78504
956-456-7899
A0000001
aalaniz @ g mail.com
2600
true
OPTIONS:-
1. Display Cutomer Information
2. Update Spend Amount
3. Exit
3
Press any key to continue . . .
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------