Designing and Writing Routines and Modules

Designing and Writing Routines and Modules

Q1)

1-

  • Verifying that the groundwork has been laid so that construction can proceed successfully.
  • Designing and writing routines and modules
  • Creating data types and naming variables
  • Selecting control structures
  • Finding and fixing faults and errors
  • Reviewing other team members' low-level design and code and having them review yours
  • Polishing code by carefully formatting and commenting it
  • Integrating software components that have been built separately
  • Tuning code to make it smaller and faster

2-

Inheritance is to reuse the attributes and the methods of a base class while implementing an interface is to write the code of the methods in that interface.

We can inherit from only one class but we can implement many interfaces

inheritance

public class A

{

}

public class B : A

{

}

Implementation

Interface C

{

public void Method1();

}

public class B : C

{

public void Method1()

{

// code of method here

}

}

Q2)

1) Mapping to C#

using System;

namespace Const_First_16_1

{

classProduct

{

privateintproductId;

publicintProductId

{

get { returnproductId; }

set { productId = value; }

}

publicfloatProductPrice { get; set; }

publicstringProductType { get; set; }

}

}

using System;

usingSystem.Collections;

namespace Const_First_16_1

{

classOrder

{

privateintorderId;

publicintOrderId

{

get { returnorderId; }

set { orderId = value; }

}

publicintCustomerId { get; set; }

publicstringCustomerName { get; set; }

ArrayListprocucts = newArrayList();

publicvoidAddProduct(ProductmyProduct)

{

procucts.Add(myProduct);

}

}

}

using System;

usingSystem.Collections;

namespace Const_First_16_1

{

classCustomer

{

privateintcustomerId;

publicintCustomerId

{

get { returncustomerId; }

set { customerId = value; }

}

publicstringCustomerName { get; set; }

publicstring Address { get; set; }

publicint Phone { get; set; }

publicCustomermyCustomer { get; set; }

ArrayList orders = newArrayList();

}

}

using System;

usingSystem.Collections;

namespace Const_First_16_1

{

classStock

{

privateintproductId;

publicintProductId

{

get { returnproductId; }

set { productId = value; }

}

publicint Quantity { get; set; }

publicintShopNo { get; set; }

ArrayListprocucts = newArrayList();

}

}

2)

//method to search for a product

//input is : the product id

//output is: true if the product exists in an order and false otherwise

publicboolFindProduct(intmyId)

{

//loop through all products in the order

foreach (Product current inprocucts)

{

// if the current product id equals the id we are searching

// then return true

if (current.ProductId == myId)

returntrue;

}

returnfalse;

}

3)

/////////////////////////

//method to return a list of products with price > 10 JD

//input: none

//output: list of the required products

publicArrayList ProductsGreater10()

{

ArrayList g10 = newArrayList();

foreach (Product current inprocucts)

{

// if the current product price is greater than 10

// then store this product in a list

if (current.ProductPrice > 10)

g10.Add(current);

}

return g10;

}

Q3) Testing

privatevoidbtnTesting_Click(object sender, EventArgs e)

{

Order order1 = newOrder() { OrderId = 1, CustomerId = 50, CustomerName = "Samer" };

Order order2 = newOrder() { OrderId = 2, CustomerId = 20, CustomerName = "Ali" };

Product product1 = newProduct() { ProductId = 252, ProductPrice = 8, ProductType = "Chair" };

Product product2 = newProduct() { ProductId = 211, ProductPrice = 12, ProductType = "Table" };

Product product3 = newProduct() { ProductId = 652, ProductPrice = 20, ProductType = "Clock" };

order1.AddProduct(product1);

order1.AddProduct(product2);

order1.AddProduct(product3);

bool result1 = order1.FindProduct(211);

MessageBox.Show("result is " + result1);

bool result2 = order1.FindProduct(111);

MessageBox.Show("result is " + result2);

ArrayList g10Test = order1.ProductsGreater10();

foreach (Product current in g10Test)

{

MessageBox.Show("Product Id = " + current.ProductId + " Price = " + current.ProductPrice);

}

}