Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C# I attached a solution of Exercise 15-1. So please use it to do this exercise below. I also attached figure 15-11 that is mentioned

C# I attached a solution of Exercise 15-1. So please use it to do this exercise below. I also attached figure 15-11 that is mentioned in this exercise.

In this exercise, you'll modify your solution to exercise 15-1 so the clones are stored in a CustomerList object that implements an enumerator. 1. Implement the IEnumerable interface for the CustomerList class. (This class wasn't used by the previous exercise.) To do that, implement the GetEnumerator method as described in Figure 15-11.

2. Modify the code in the form class so it stores the cloned Customer object in a CustomerList object rather than in a List object

3. Run the application and test it to be sure it works properly.image text in transcribed

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CloneCustomer.DataValidation;

namespace CloneCustomer { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private Customer customer; private List customers;

private void Form1_Load(object sender, System.EventArgs e) { customer = new Customer("John", "Mendez", "jmendez@msysco.com"); lblCustomer.Text = customer.GetDisplayText(); }

private void btnClone_Click(object sender, System.EventArgs e) { if (IsValidData()) { int count = Convert.ToInt16(txtCopies.Text); customers = MakeCustomerList(customer, count); lstCustomers.Items.Clear(); foreach (Customer c in customers) { lstCustomers.Items.Add(c.GetDisplayText()); } } }

private bool IsValidData() { return Validator.IsPresent(txtCopies) && Validator.IsInt32(txtCopies); }

private List MakeCustomerList(Customer c, int count) { List customerList = new List(); for (int i = 0; i

private void btnExit_Click(object sender, System.EventArgs e) { this.Close(); }

} } ___________________________________________________________________

using System;

namespace CloneCustomer { public class Customer : ICloneable { private string firstName; private string lastName; private string email;

public Customer() { }

public Customer(string firstName, string lastName, string email) { this.FirstName = firstName; this.LastName = lastName; this.Email = email; }

///

/// The Customer First Name. ///

public string FirstName { get { return firstName; } set { firstName = value; } }

public string LastName { get { return lastName; } set { lastName = value; } }

public string Email { get { return email; } set { email = value; } }

public string GetDisplayText() => firstName + " " + lastName + ", " + email;

public object Clone() => new Customer(this.FirstName, this.LastName, this.Email); } } _____________________________________________________

namespace CloneCustomer { internal interface ICloneCustomers { } }

_____________________________________________________

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

namespace CloneCustomer.DataValidation { ///

/// Validator Utility Class to Validate inputs ///

public static class Validator { private static string title = "Entry Error";

///

/// Title Propertu of Validator Class Object ///

public static string Title { get { return title; } set { title = value; } }

///

/// Check if Input is Present ///

///TextBox Type /// bool public static bool IsPresent(TextBox textBox) { if (textBox.Text == "") { MessageBox.Show(textBox.Tag + " is a required field.", Title); textBox.Focus(); return false; } return true; }

public static bool IsDecimal(TextBox textBox) { decimal number = 0m; if (Decimal.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(textBox.Tag + " must be a decimal value.", Title); textBox.Focus(); return false; } }

public static bool IsInt32(TextBox textBox) { int number = 0; if (Int32.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(textBox.Tag + " must be an integer.", Title); textBox.Focus(); return false; } }

public static bool IsWithinRange(TextBox textBox, decimal min, decimal max) { decimal number = Convert.ToDecimal(textBox.Text); if (number max) { MessageBox.Show(textBox.Tag + " must be between " + min + " and " + max + ".", Title); textBox.Focus(); return false; } return true; }

public static bool IsValidEmail(TextBox textBox) { if (textBox.Text.IndexOf("@") == -1 || textBox.Text.IndexOf(".") == -1) { MessageBox.Show(textBox.Tag + " must be a valid email address.", Title); textBox.Focus(); return false; } else { return true; } } } } ____________________________________________

using System; using System.Collections.Generic;

namespace CloneCustomer { public class CustomerList { private List customers = new List();

public int Count => customers.Count;

public Customer this[int i] => customers[i];

public void Add(Customer customer) => customers.Add(customer); } }

Chapter 15 How to work with interfaces and generics Chapter 15 rcise 15-1 505 How to work with interfaces implements the lEnumerable interface and generies 501 Implement the ICloneable interface you'll create an application that i Aass Custontist i IEnumerable that ncludes a Customer class that private Laster> 1iat ed Customer develop, we'll give you the starting form and classes, litiosier to bject and displays the cloned a list box as shown below. To make this application easier to other nenbers publie stomers in a list box as shown below. 1T Enumerator det Enumerator foreach (T tea in list) The design of the Clone Customer form yield return item collections IEnumerator System.collections. IEnunerable.Getknumerator) throw new NotImplementedException0: Code that uses the class Product pi new Product ("vB15 Product p2 new ("cs15 customlistcProduct listnew CustomList ): List.add (pl); list.add (p2) ; foreach (Product p in list) urach's Visual Basic 2015, 56.50m)1 -Murach. C# Product("CS15-, Development procedure 2015, S630m) , Open the application in the C/C# 2015 Chapter 15CloneCustomer directory. 2. Display the code for the form, and notice that the Load event handler creates a Customer object, stores it in a variable named customer, and displays the customer in the label at the top of the form. Console.WriteLine (p.TostringO) 3. 4. Modify the Customer class so it implements the ICloneable interface. Add an event handler for the Click event of the Clone button. This event handler should use the methods in the Validator class to check the value in Description . The foreach loop only works on generic collections that implement the the Copies text box to be sure it's present and also an integer. Then, it should create a List object that contains the required number of clones of the Customer object. Finally, it should display the cloned customers in the list box. TEnumerable> interface. As a result, if you want to use the foreach loop on a generic collection that you've defined, you must implement the IEnumerable interface ' When implementing the GetEnumerator method, you can use the yield keyword with the return keyword to return the current element to the object that implements the lEnumerable interface and yield contro 5. Run the application and test it to make sure it works properly. l. Figure 15-11 timnlement the IEnumerable>interface Chapter 15 How to work with interfaces and generics Chapter 15 rcise 15-1 505 How to work with interfaces implements the lEnumerable interface and generies 501 Implement the ICloneable interface you'll create an application that i Aass Custontist i IEnumerable that ncludes a Customer class that private Laster> 1iat ed Customer develop, we'll give you the starting form and classes, litiosier to bject and displays the cloned a list box as shown below. To make this application easier to other nenbers publie stomers in a list box as shown below. 1T Enumerator det Enumerator foreach (T tea in list) The design of the Clone Customer form yield return item collections IEnumerator System.collections. IEnunerable.Getknumerator) throw new NotImplementedException0: Code that uses the class Product pi new Product ("vB15 Product p2 new ("cs15 customlistcProduct listnew CustomList ): List.add (pl); list.add (p2) ; foreach (Product p in list) urach's Visual Basic 2015, 56.50m)1 -Murach. C# Product("CS15-, Development procedure 2015, S630m) , Open the application in the C/C# 2015 Chapter 15CloneCustomer directory. 2. Display the code for the form, and notice that the Load event handler creates a Customer object, stores it in a variable named customer, and displays the customer in the label at the top of the form. Console.WriteLine (p.TostringO) 3. 4. Modify the Customer class so it implements the ICloneable interface. Add an event handler for the Click event of the Clone button. This event handler should use the methods in the Validator class to check the value in Description . The foreach loop only works on generic collections that implement the the Copies text box to be sure it's present and also an integer. Then, it should create a List object that contains the required number of clones of the Customer object. Finally, it should display the cloned customers in the list box. TEnumerable> interface. As a result, if you want to use the foreach loop on a generic collection that you've defined, you must implement the IEnumerable interface ' When implementing the GetEnumerator method, you can use the yield keyword with the return keyword to return the current element to the object that implements the lEnumerable interface and yield contro 5. Run the application and test it to make sure it works properly. l. Figure 15-11 timnlement the IEnumerable>interface

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Carry out an interview and review its success.

Answered: 1 week ago