|
Sans compter que la capacité de créer une
collection, la classe d'ArrayList a le mécanisme intégré pour
la fabrication en série.
La classe d'ArrayList est définie dans le
namespace de System.Collections. Par conséquent, afin d'employer
la classe d'ArrayList dans votre application, vous devez d'abord
inclure le namespace de System.Collections dans le dossier qui
effectuerait des opérations d'ArrayList.
|
Étude pratique : Présentation de la classe d'ArrayList
|
|
- Commencer l'édition exprès visuelle de Microsoft C# et créer
une nouvelle application de console appelée RealEstate7
- Sauver le projet, sur le toolbar standard, cliquent l'économiser
tout le bouton
- Accepter les suggestions et cliquer l'économiser
- Créer une nouvelle classe, dans l'explorateur de solution,
droit-cliquent RealEstate7 - > ajouter - > classe…
- Placer le nom à la propriété et le clic s'ajoutent
- Changer le dossier comme suit :
using System;
namespace RealEstate5
{
public enum PropertyCondition
{
Unknown,
Excellent,
Good,
NeedsRepair,
BadShape
}
[Serializable]
public class Property
{
private string propNbr;
private PropertyCondition cond;
private short beds;
private float baths;
private int yr;
private decimal val;
public string PropertyNumber
{
get { return propNbr; }
set
{
if (propNbr == "")
propNbr = "N/A";
else
propNbr = value;
}
}
public string PropertyType;
public PropertyCondition Condition
{
get { return cond; }
set { cond = value; }
}
public short Bedrooms
{
get
{
if (beds <= 1)
return 1;
else
return beds;
}
set { beds = value; }
}
public float Bathrooms
{
get { return (baths <= 0) ? 0.00f : baths; }
set { baths = value; }
}
public int YearBuilt
{
get { return yr; }
set { yr = value; }
}
public decimal Value
{
get { return (val <= 0) ? 0.00M : val; }
set { val = value; }
}
}
}
|
- Créer une nouvelle classe, dans la vue de classe, droit-cliquent
RealEstate7 - > ajouter - > classe…
- Placer le nom au condominium et la pression entrent
- Changer le dossier comme suit :
using System;
namespace RealEstate5
{
[Serializable]
public class Condominium : Property
{
private bool handicap;
public Condominium()
{
this.PropertyType = "Condominium";
}
public bool HandicapAccessible
{
get { return handicap; }
set { handicap = value; }
}
}
}
|
|
- Pour créer une nouvelle classe, sur le menu principal, projet de
clic - > ajouter la classe…
- Placer le nom à HouseType et le clic s'ajoutent
- Changer le dossier comme suit :
using System;
namespace RealEstate5
{
[Serializable]
public class HouseType : Property
{
private short nbrOfStories;
private bool basement;
private bool garage;
public short Stories
{
get { return nbrOfStories; }
set { nbrOfStories = value; }
}
public bool FinishedBasement
{
get { return basement; }
set { basement = value; }
}
public bool IndoorGarage
{
get { return garage; }
set { garage = value; }
}
}
}
|
- Créer une nouvelle classe, dans l'explorateur de solution,
droit-cliquent RealEstate7 - > ajouter - > classe…
- Placer le nom à la maison urbaine et la pression entrent
- Changer le dossier comme suit :
using System;
namespace RealEstate5
{
[Serializable]
public class Townhouse : HouseType
{
private bool comm;
public Townhouse()
{
this.PropertyType = "Townhouse";
}
public bool IsCommunityManaged
{
get { return this.comm; }
set { this.comm = value; }
}
}
}
|
- Créer une nouvelle classe, dans la vue de classe, droit-cliquent
RealEstate7 - > ajouter - > classe…
- Placer le nom à unifamilial et le clic s'ajoutent
- Changer le dossier comme suit :
using System;
namespace RealEstate5
{
[Serializable]
public class SingleFamily : HouseType
{
public SingleFamily()
{
this.PropertyType = "Single Family";
}
}
}
|
|
- Économiser tous
Après la déclaration d'une variable d'ArrayList,
elle est vide. Pendant que des objets sont ajoutés à elle, la liste se
développe. La liste peut se développer énormément comme vous
souhaitez. Le nombre d'articles de la liste est contrôlé par la mémoire
qu'elle occupe et cette mémoire se développe comme nécessaire. Le
nombre d'articles que la mémoire assignée emploie actuellement est
représenté par la propriété d'ArrayList.Capacity. Ce sera
habituellement le mineur de vos soucis.
Si pour quelque raison, vous voulez intervenir et
commander le nombre d'articles que votre liste d'ArrayList peut
contenir, vous pouvez manoeuvrer la propriété de capacité. Par
exemple, vous pouvez lui assigner une constante pour placer la valeur
maximum que la liste peut contenir. De nouveau, vous aurez à peine
n'importe quelle raison d'employer la propriété de capacité :
le compilateur sait quoi faire avec lui.
Si vous placez un à taille fixe sur une liste d'ArrayList,
vous ne pouvez pas pouvoir ajouter un nouvel article au delà de la
limite. En fait, si vous essayez de faire ceci, vous pouvez recevoir une
erreur. Une manière sûre est de vérifier si la liste est fixe avant
d'effectuer une opération relative. Pour découvrir si une liste est
fixe, vous pouvez vérifier la propriété d'IsFixedSize de la
variable d'ArrayList.
Un de la raison de créer une liste est de pouvoir
ajouter des articles à lui, éditer ses articles, rechercher des
articles, ou supprimer des articles de lui. Ce sont les opérations de défaut.
Vous pouvez encore limiter ces opérations comme vous les jugez
inutiles. Par exemple, vous pouvez créer une liste et puis
l'initialiser avec les articles que vous voulez que la liste ait
seulement. Si vous n'avez pas l'intention d'avoir l'utilisateur ajouter
des articles à elle, vous pouvez créer la liste comme inaltérable.
Pour faire ceci, vous pouvez appeler la méthode d'ArrayList.ReadOnly(). Elle est surchargée avec deux versions comme suit :
public static ArrayList ReadOnly(ArrayList);
public static IList ReadOnly(IList);
|
Cette méthode est statique. Ceci signifie
que vous n'avez pas besoin de déclarer un exemple d'ArrayList
pour les appeler. Au lieu de cela, pour rendre la liste inaltérable,
appeler la méthode d'ArrayList.ReadOnly () et lui passer
votre variable d'ArrayList.
Car nous verrons dans les prochaines
sections, quelques opérations ne peuvent pas être effectuées
sur une liste inaltérable. Pour effectuer de telles opérations,
vous pouvez d'abord découvrir si une liste d'ArrayList
est inaltérable. Ceci est fait en vérifiant sa propriété d'IsReadOnly.
|
 |
L'opération primaire effectuée sur une liste est
de créer un. Un des plus grands avantages d'employer une liste liée
est que vous ne devez pas indiquer à l'avance le nombre d'articles de
la liste comme fait pour une rangée. Vous pouvez juste commencer à
ajouter des articles. La classe d'ArrayList rend ceci possible
avec la méthode d'ajouter(). Sa syntaxe est : public virtual int Add(object value);
L'argument de cette méthode est la valeur à
ajouter à la liste. Si la méthode réussit avec l'addition, elle
renvoie la position où la valeur a été ajoutée dans la liste. C'est
habituellement la dernière position dans la liste. Si la méthode échoue,
le compilateur jetterait une erreur. Une des erreurs qui pourraient résulter
de l'échec de cette opération serait basée sur le fait que l'un ou
l'autre un nouvel article ne peut pas être ajouté à la liste parce
que la liste est inaltérable, ou la liste était déjà pleine avant
d'ajouter le nouvel article. Normalement, une liste peut être pleine
seulement si vous aviez indiqué le nombre maximum des articles qu'il
peut contenir en utilisant la propriété d'ArrayList.Capacity.
Comme mentionné ci-dessus, la liste peut être rendue inaltérable en
passant sa variable à la méthode d'ArrayList.ReadOnly ().
|
Étude pratique : Ajouter des articles à une liste d'ArrayList
|
|
- Pour créer un inventaire, sur le menu principal, projet de clic -
> ajouter la classe…
- Placer le nom à PropertyManagement et la pression entrent
- Changer le dossier comme suit :
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
namespace RealEstate5
{
public class PropertyManagement
{
private Condominium Condo;
private Townhouse TownHome;
private SingleFamily House;
private ArrayList Condominiums;
private ArrayList Townhouses;
private ArrayList SingleFamilies;
private string strPropertyNumber;
private string strPropertiesDirectory;
public PropertyManagement()
{
Condo = new Condominium();
TownHome = new Townhouse();
House = new SingleFamily();
Condominiums = new ArrayList();
Townhouses = new ArrayList();
SingleFamilies = new ArrayList();
strPropertyNumber = "000000";
strPropertiesDirectory = @"C:\Altair Realty\Properties";
try
{
// If a directory for the properties has not yet
// been created, then create them
Directory.CreateDirectory(strPropertiesDirectory);
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("The directory could " +
"not be created");
}
}
public PropertyCondition GetPropertyCondition()
{
short condition = 0;
try
{
Console.WriteLine("\nProperties Conditions");
Console.WriteLine("1. Excellent");
Console.WriteLine("2. Good (may need minor repair)");
Console.WriteLine("3. Needs Repair");
Console.Write("4. In Bad Shape (property needs ");
Console.WriteLine("major repair or rebuild)");
Console.Write("Enter Property Condition: ");
condition = short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the condition of the property is not valid");
}
if (condition == 1)
return PropertyCondition.Excellent;
else if (condition == 2)
return PropertyCondition.Good;
else if (condition == 3)
return PropertyCondition.NeedsRepair;
else if (condition == 4)
return PropertyCondition.BadShape;
else
return PropertyCondition.Unknown;
}
public void CreateProperty()
{
char propType = '0';
Console.WriteLine("\n=======================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("-=- Property Creation -=-");
Console.WriteLine("------------------------");
// We will make sure that no two
// properties have the same number
Console.Write("\nEnter Property #: ");
this.strPropertyNumber = Console.ReadLine();
try
{
Console.WriteLine("\nTypes of Properties");
Console.WriteLine("1. Condominium");
Console.WriteLine("2. Townhouse");
Console.WriteLine("3. Single Family");
Console.Write("Enter Type of Property: ");
propType = char.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the type of property is invalid");
}
switch (propType)
{
case '1':
CreateCondominium();
break;
case '2':
CreateTownhouse();
break;
case '3':
CreateSingleFamily();
break;
default:
Console.WriteLine("Invalid Choice!!!");
break;
}
}
public void CreateCondominium()
{
char answer = 'n';
Condo.PropertyNumber = strPropertyNumber;
Condo.Condition = GetPropertyCondition();
try
{
Console.Write("\nHow many bedrooms? ");
Condo.Bedrooms = short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the number of bedrooms is not good");
}
try
{
Console.Write("How many bathrooms? ");
Condo.Bathrooms =
float.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid value");
}
try
{
Console.Write("Year built: ");
Condo.YearBuilt =
int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The house cannot have " +
"built in that year");
}
Console.Write("\nIs the building " +
"accessible to handicapped (y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
Condo.HandicapAccessible = true;
else
Condo.HandicapAccessible = false;
try
{
Console.Write("Condominium Value: ");
Condo.Value = decimal.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Property Value");
}
Condominiums.Add(Condo);
ShowTitle();
ShowCondominium();
SaveCondominium();
}
public void CreateTownhouse()
{
char answer = 'n';
this.TownHome.PropertyNumber = strPropertyNumber;
this.TownHome.Condition = GetPropertyCondition();
try
{
Console.Write("\nHow many stories (levels)? ");
this.TownHome.Stories =
short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The number of stories " +
"you entered is not valid");
}
try
{
Console.Write("How many bedrooms? ");
this.TownHome.Bedrooms =
short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the number of bedrooms is not good");
}
try
{
Console.Write("How many bathrooms? ");
this.TownHome.Bathrooms =
float.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid bathroom number.");
}
Console.Write("Does it have an indoor " +
"car garage (y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
this.TownHome.IndoorGarage = true;
else
this.TownHome.IndoorGarage = false;
Console.Write("Is the basement finished(y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
this.TownHome.FinishedBasement = true;
else
this.TownHome.FinishedBasement = false;
try
{
Console.Write("Year built: ");
this.TownHome.YearBuilt =
int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The house cannot have " +
"built in that year");
}
try
{
Console.Write("Is it community managed (y/n)? ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
this.TownHome.IsCommunityManaged = true;
else
this.TownHome.IsCommunityManaged = false;
}
catch (FormatException)
{
Console.WriteLine("Invalid Answer");
}
try
{
Console.Write("Property Value: ");
this.TownHome.Value = decimal.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Property Value");
}
Townhouses.Add(this.TownHome);
ShowTitle();
ShowTownhouse();
SaveTownhouse();
}
public void CreateSingleFamily()
{
char answer = 'n';
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("-=- Property Creation -=-");
this.House.PropertyNumber = strPropertyNumber;
House.Condition = GetPropertyCondition();
try
{
Console.Write("\nHow many stories (levels)? ");
House.Stories = short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The number of stories you " +
"entered is not allowed");
}
try
{
Console.Write("How many bedrooms? ");
House.Bedrooms =
short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the number of bedrooms is not good");
}
try
{
Console.Write("How many bathrooms? ");
House.Bathrooms =
float.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid number of bathrooms");
}
try
{
Console.Write("Does it have an indoor " +
"car garage (y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
House.IndoorGarage = true;
else
House.IndoorGarage = false;
}
catch (FormatException)
{
Console.WriteLine("Invalid Indoor Car Garage Answer");
}
try
{
Console.Write("Is the basement finished(y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
House.FinishedBasement = true;
else
House.FinishedBasement = false;
}
catch (FormatException)
{
Console.WriteLine("Invalid Basement Answer");
}
try
{
Console.Write("Year built: ");
House.YearBuilt =
int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The house cannot have " +
"built in that year");
}
try
{
Console.Write("House Value: ");
House.Value = decimal.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Property Value");
}
SingleFamilies.Add(House);
ShowTitle();
ShowSingleFamily();
SaveSingleFamily();
}
public void ShowTitle()
{
Console.WriteLine("==================================");
Console.WriteLine(" =//=//= Altair Realty =//=//=");
Console.WriteLine("-=-=-=- Properties Listing -=-=-=-");
}
public void ShowCondominium()
{
Console.WriteLine("----------------------------------");
Console.WriteLine("Property #: {0}",
Condo.PropertyNumber);
Console.WriteLine("Property Type: {0}",
Condo.PropertyType);
Console.WriteLine("Condition: {0}",
Condo.Condition);
Console.WriteLine("Bedrooms: {0}",
Condo.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
Condo.Bathrooms);
Console.WriteLine("Year Built: {0}",
Condo.YearBuilt);
Console.WriteLine("Handicapped Accessible Building: {0}",
Condo.HandicapAccessible);
Console.WriteLine("Market Value: {0:C}",
Condo.Value);
Console.WriteLine("----------------------------------");
}
public void ShowTownhouse()
{
Console.WriteLine("----------------------------------");
Console.WriteLine("Property #: {0}",
TownHome.PropertyNumber);
Console.WriteLine("Property Type: {0}",
TownHome.PropertyType);
Console.WriteLine("Stories: {0}",
TownHome.Stories);
Console.WriteLine("Has Indoor Car Garage: {0}",
TownHome.IndoorGarage);
Console.WriteLine("Finished Basement: {0}",
TownHome.FinishedBasement);
Console.WriteLine("Condition: {0}",
TownHome.Condition);
Console.WriteLine("Bedrooms: {0}",
TownHome.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
TownHome.Bathrooms);
Console.WriteLine("Year Built: {0}",
TownHome.YearBuilt);
Console.WriteLine("Community Managed? {0}",
TownHome.IsCommunityManaged);
Console.WriteLine("Market Value: {0:C}",
TownHome.Value);
Console.WriteLine("----------------------------------");
}
public void ShowSingleFamily()
{
Console.WriteLine("----------------------------------");
Console.WriteLine("Property #: {0}",
House.PropertyNumber);
Console.WriteLine("Property Type: {0}",
House.PropertyType);
Console.WriteLine("Stories: {0}",
House.Stories);
Console.WriteLine("Has Indoor Car Garage: {0}",
House.IndoorGarage);
Console.WriteLine("Finished Basement: {0}",
House.FinishedBasement);
Console.WriteLine("Condition: {0}",
House.Condition);
Console.WriteLine("Bedrooms: {0}",
House.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
House.Bathrooms);
Console.WriteLine("Year Built: {0}",
House.YearBuilt);
Console.WriteLine("Market Value: {0:C}",
House.Value);
Console.WriteLine("----------------------------------");
}
public void SaveCondominium()
{
FileStream fsCondo = null;
BinaryFormatter bfCondo = new BinaryFormatter();
string strFilename = strPropertiesDirectory +
@"\Condominiums.alr";
try
{
fsCondo = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
bfCondo.Serialize(fsCondo, Condominiums);
}
finally
{
fsCondo.Close();
}
}
public void SaveTownhouse()
{
FileStream fsTown = null;
BinaryFormatter bfTown =
new BinaryFormatter();
string strFilename = strPropertiesDirectory +
@"\Townhouses.alr";
try
{
fsTown = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
bfTown.Serialize(fsTown, Townhouses);
}
finally
{
fsTown.Close();
}
}
public void SaveSingleFamily()
{
FileStream fsHouse = null;
BinaryFormatter bfHouse = new BinaryFormatter();
string strFilename = strPropertiesDirectory +
@"\SingleFamilies.alr";
try
{
fsHouse = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
bfHouse.Serialize(fsHouse,
SingleFamilies);
}
finally
{
fsHouse.Close();
}
}
}
}
|
- L'Access le dossier de Program.cs et le changent comme suit :
using System;
namespace RealEstate5
{
public class Program
{
static int Main(string[] args)
{
char answer = 'q';
PropertyManagement listing = new PropertyManagement();
// Display the title
Console.WriteLine("=================================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("----------------------------------");
do
{
// Ask the user to select an option
try
{
Console.WriteLine("What do you want to do?");
Console.WriteLine("1. Create a property");
Console.WriteLine("0. Quit");
Console.Write("Your Choice? ");
answer = char.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Choice!!!");
}
switch (answer)
{
case '1':
listing.CreateProperty();
break;
default:
break;
}
} while(answer == '1');
Console.WriteLine();
return 0;
}
}
}
|
- Exécuter l'application et créer continuellement les propriétés
suivantes :
=================================
=//= Altair Realty =//=
----------------------------------
What do you want to do?
1. Create a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 000001
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 1
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 1
How many bedrooms? 1
How many bathrooms? 1
Year built: 1960
Is the building accessible to handicapped (y/n): n
Condominium Value: 10000
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 000001
Property Type: Condominium
Condition: Excellent
Bedrooms: 1
Bathrooms: 1.00
Year Built: 1960
Handicapped Accessible Building: False
Market Value: $10,000.00
----------------------------------
What do you want to do?
1. Create a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 000002
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 2
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 1
How many stories (levels)? 1
How many bedrooms? 1
How many bathrooms? 1
Does it have an indoor car garage (y/n): n
Is the basement finished(y/n): n
Year built: 1960
Is it community managed (y/n)? n
Property Value: 20000
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 000002
Property Type: Townhouse
Stories: 1
Has Indoor Car Garage: False
Finished Basement: False
Condition: Excellent
Bedrooms: 1
Bathrooms: 1.00
Year Built: 1960
Community Managed? False
Market Value: $20,000.00
----------------------------------
What do you want to do?
1. Create a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 000003
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 3
=//= Altair Realty =//=
-=- Property Creation -=-
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 1
How many stories (levels)? 1
How many bedrooms? 1
How many bathrooms? 1
Does it have an indoor car garage (y/n): n
Is the basement finished(y/n): n
Year built: 1960
House Value: 30000
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 000003
Property Type: Single Family
Stories: 1
Has Indoor Car Garage: False
Finished Basement: False
Condition: Excellent
Bedrooms: 1
Bathrooms: 1.00
Year Built: 1960
Market Value: $30,000.00
----------------------------------
What do you want to do?
1. Create a property
0. Quit
Your Choice? 0
Press any key to continue . . .
|
- Fermer la fenêtre de DOS
|
Le nombre d'articles dans la liste
|
|
En utilisant une liste, à tout moment, vous devriez
pouvoir savoir le nombre d'articles que la liste contient. Ces
informations sont fournies par la propriété d'ArrayList.Count.
La capacité et le compte ont ceci en commun : la valeur de chacune
augmente pendant que la liste se développe et la même valeur diminue
si la liste se rétrécit. Il est important de savoir que, bien qu'ils
identiques, là soient de diverses différences entre la capacité d'une
liste et le nombre d'articles elle contient. La capacité est une
propriété lecture/écriture. Ceci signifie que, comme nous avons vu en
haut, vous pouvez assigner une valeur à la capacité de fixer le nombre
d'articles que la liste peut contenir. Vous pouvez également rechercher
la valeur de la capacité. Le compte est inaltérable parce qu'il
est employé par le compilateur pour compter le nombre courant
d'articles des articles et ceci le compte est exécuté sans votre
intervention.
Une fois qu'une liste est prête, vous pouvez exécuter
différents types d'opérations là-dessus. Sans compter qu'ajouter des
articles, une des opérations les plus régulières effectuées sur une
liste se compose localiser et rechercher ses articles. Vous avez de
diverses options. Pour rechercher un article simple basé sur sa
position, vous pouvez appliquer les crochets des rangées à la
variable. Comme une rangée normale, une liste d'ArrayList zéro-est
basée. Une autre question à maintiennent dans l'esprit est que l'ArrayList
[] renvoie une valeur d'objet. Par conséquent, vous pouvez devoir
mouler cette valeur à votre type de valeur pour l'obtenir droit.
Sans compter qu'employer l'index pour accéder à un
article de la liste, la classe d'ArrayList applique la méthode
d'IEnumerable.GetEnumerator (). Pour cette raison, vous pouvez
employer la boucle de foreach pour accéder à chaque membre de
la collection.
|
Étude pratique : Recherche des articles d'une liste d'ArrayList
|
|
- Changer le dossier de PropertyManagement.cs comme suit :
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
namespace RealEstate5
{
public class PropertyManagement
{
private Condominium Condo;
private Townhouse TownHome;
private SingleFamily House;
ArrayList Condominiums;
ArrayList Townhouses;
ArrayList SingleFamilies;
string strPropertyNumber;
string strPropertiesDirectory;
public PropertyManagement()
{
Condo = new Condominium();
TownHome = new Townhouse();
House = new SingleFamily();
Condominiums = new ArrayList();
Townhouses = new ArrayList();
SingleFamilies = new ArrayList();
strPropertyNumber = "000000";
strPropertiesDirectory = @"C:\Altair Realty\Properties";
try
{
// If a directory for the properties has not yet
// been created, then create them
Directory.CreateDirectory(strPropertiesDirectory);
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("The directory could not be created");
}
}
public PropertyCondition GetPropertyCondition()
{
short condition = 0;
try
{
Console.WriteLine("\nProperties Conditions");
Console.WriteLine("1. Excellent");
Console.WriteLine("2. Good (may need minor repair)");
Console.WriteLine("3. Needs Repair");
Console.Write("4. In Bad Shape (property needs ");
Console.WriteLine("major repair or rebuild)");
Console.Write("Enter Property Condition: ");
condition = short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the condition of the property is not valid");
}
if (condition == 1)
return PropertyCondition.Excellent;
else if (condition == 2)
return PropertyCondition.Good;
else if (condition == 3)
return PropertyCondition.NeedsRepair;
else if (condition == 4)
return PropertyCondition.BadShape;
else
return PropertyCondition.Unknown;
}
public void CreateProperty()
{
char propType = '0';
bool found = false;
FileStream fsProperties = null;
BinaryFormatter bfProperties = new BinaryFormatter();
Console.WriteLine("\n=======================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("-=- Property Creation -=-");
Console.WriteLine("------------------------");
// We will make sure that no two
// properties have the same number
Console.Write("\nEnter Property #: ");
this.strPropertyNumber = Console.ReadLine();
try
{
fsProperties = new FileStream(strPropertiesDirectory +
@"\Condominiums.alr",
FileMode.Open,
FileAccess.Read);
Condominiums =
(ArrayList)bfProperties.Deserialize(fsProperties);
foreach (Condominium c in Condominiums)
{
if (c.PropertyNumber == this.strPropertyNumber)
{
found = true;
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine("The Condominiums list was not found");
}
finally
{
fsProperties.Close();
}
try
{
fsProperties = new FileStream(strPropertiesDirectory +
@"\Townhouses.alr",
FileMode.Open,
FileAccess.Read);
Townhouses =
(ArrayList)bfProperties.Deserialize(fsProperties);
foreach (Townhouse t in Townhouses)
{
if (t.PropertyNumber == this.strPropertyNumber)
{
found = true;
}
}
}
finally
{
fsProperties.Close();
}
try
{
fsProperties = new FileStream(strPropertiesDirectory +
@"\SingleFamilies.alr",
FileMode.Open,
FileAccess.Read);
SingleFamilies =
(ArrayList)bfProperties.Deserialize(fsProperties);
foreach (SingleFamily s in SingleFamilies)
{
if (s.PropertyNumber == this.strPropertyNumber)
{
found = true;
}
}
}
finally
{
fsProperties.Close();
}
if (found == true)
{
Console.WriteLine("A property with that " +
"number exists already");
return;
}
try
{
Console.WriteLine("\nTypes of Properties");
Console.WriteLine("1. Condominium");
Console.WriteLine("2. Townhouse");
Console.WriteLine("3. Single Family");
Console.Write("Enter Type of Property: ");
propType = char.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the type of property is invalid");
}
switch (propType)
{
case '1':
CreateCondominium();
break;
case '2':
CreateTownhouse();
break;
case '3':
CreateSingleFamily();
break;
default:
Console.WriteLine("Invalid Choice!!!");
break;
}
}
public void CreateCondominium()
{
char answer = 'n';
Condo.PropertyNumber = strPropertyNumber;
Condo.Condition = GetPropertyCondition();
try
{
Console.Write("\nHow many bedrooms? ");
Condo.Bedrooms = short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the number of bedrooms is not good");
}
try
{
Console.Write("How many bathrooms? ");
Condo.Bathrooms =
float.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid value");
}
try
{
Console.Write("Year built: ");
Condo.YearBuilt =
int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The house cannot have " +
"built in that year");
}
Console.Write("\nIs the building " +
"accessible to handicapped (y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
Condo.HandicapAccessible = true;
else
Condo.HandicapAccessible = false;
try
{
Console.Write("Condominium Value: ");
Condo.Value = decimal.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Property Value");
}
Condominiums.Add(Condo);
ShowTitle();
ShowCondominium();
SaveCondominium();
}
public void CreateTownhouse()
{
char answer = 'n';
this.TownHome.PropertyNumber = strPropertyNumber;
this.TownHome.Condition = GetPropertyCondition();
try
{
Console.Write("\nHow many stories (levels)? ");
this.TownHome.Stories =
short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The number of stories " +
"you entered is not valid");
}
try
{
Console.Write("How many bedrooms? ");
this.TownHome.Bedrooms =
short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the number of bedrooms is not good");
}
try
{
Console.Write("How many bathrooms? ");
this.TownHome.Bathrooms =
float.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid bathroom number.");
}
Console.Write("Does it have an indoor " +
"car garage (y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
this.TownHome.IndoorGarage = true;
else
this.TownHome.IndoorGarage = false;
Console.Write("Is the basement finished(y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
this.TownHome.FinishedBasement = true;
else
this.TownHome.FinishedBasement = false;
try
{
Console.Write("Year built: ");
this.TownHome.YearBuilt =
int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The house cannot have " +
"built in that year");
}
try
{
Console.Write("Is it community managed (y/n)? ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
this.TownHome.IsCommunityManaged = true;
else
this.TownHome.IsCommunityManaged = false;
}
catch (FormatException)
{
Console.WriteLine("Invalid Answer");
}
try
{
Console.Write("Property Value: ");
this.TownHome.Value =
decimal.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Property Value");
}
Townhouses.Add(this.TownHome);
ShowTitle();
ShowTownhouse();
SaveTownhouse();
}
public void CreateSingleFamily()
{
char answer = 'n';
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("-=- Property Creation -=-");
this.House.PropertyNumber = strPropertyNumber;
House.Condition = GetPropertyCondition();
try
{
Console.Write("\nHow many stories (levels)? ");
House.Stories = short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The number of stories you " +
"entered is not allowed");
}
try
{
Console.Write("How many bedrooms? ");
House.Bedrooms =
short.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The value you entered for " +
"the number of bedrooms is not good");
}
try
{
Console.Write("How many bathrooms? ");
House.Bathrooms =
float.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid number of bathrooms");
}
try
{
Console.Write("Does it have an indoor " +
"car garage (y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
House.IndoorGarage = true;
else
House.IndoorGarage = false;
}
catch (FormatException)
{
Console.WriteLine("Invalid Indoor Car Garage Answer");
}
try
{
Console.Write("Is the basement finished(y/n): ");
answer = char.Parse(Console.ReadLine());
if ((answer == 'y') || (answer == 'Y'))
House.FinishedBasement = true;
else
House.FinishedBasement = false;
}
catch (FormatException)
{
Console.WriteLine("Invalid Basement Answer");
}
try
{
Console.Write("Year built: ");
House.YearBuilt =
int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("The house cannot have " +
"built in that year");
}
try
{
Console.Write("House Value: ");
House.Value = decimal.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Property Value");
}
SingleFamilies.Add(House);
ShowTitle();
ShowSingleFamily();
SaveSingleFamily();
}
public void ShowTitle()
{
Console.WriteLine("==================================");
Console.WriteLine(" =//=//= Altair Realty =//=//=");
Console.WriteLine("-=-=-=- Properties Listing -=-=-=-");
}
public void ShowCondominium()
{
Console.WriteLine("----------------------------------");
Console.WriteLine("Property #: {0}",
Condo.PropertyNumber);
Console.WriteLine("Property Type: {0}",
Condo.PropertyType);
Console.WriteLine("Condition: {0}",
Condo.Condition);
Console.WriteLine("Bedrooms: {0}",
Condo.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
Condo.Bathrooms);
Console.WriteLine("Year Built: {0}",
Condo.YearBuilt);
Console.WriteLine("Handicapped Accessible Building: {0}",
Condo.HandicapAccessible);
Console.WriteLine("Market Value: {0:C}",
Condo.Value);
Console.WriteLine("----------------------------------");
}
public void ShowTownhouse()
{
Console.WriteLine("----------------------------------");
Console.WriteLine("Property #: {0}",
TownHome.PropertyNumber);
Console.WriteLine("Property Type: {0}",
TownHome.PropertyType);
Console.WriteLine("Stories: {0}",
TownHome.Stories);
Console.WriteLine("Has Indoor Car Garage: {0}",
TownHome.IndoorGarage);
Console.WriteLine("Finished Basement: {0}",
TownHome.FinishedBasement);
Console.WriteLine("Condition: {0}",
TownHome.Condition);
Console.WriteLine("Bedrooms: {0}",
TownHome.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
TownHome.Bathrooms);
Console.WriteLine("Year Built: {0}",
TownHome.YearBuilt);
Console.WriteLine("Community Managed? {0}",
TownHome.IsCommunityManaged);
Console.WriteLine("Market Value: {0:C}",
TownHome.Value);
Console.WriteLine("----------------------------------");
}
public void ShowSingleFamily()
{
Console.WriteLine("----------------------------------");
Console.WriteLine("Property #: {0}",
House.PropertyNumber);
Console.WriteLine("Property Type: {0}",
House.PropertyType);
Console.WriteLine("Stories: {0}",
House.Stories);
Console.WriteLine("Has Indoor Car Garage: {0}",
House.IndoorGarage);
Console.WriteLine("Finished Basement: {0}",
House.FinishedBasement);
Console.WriteLine("Condition: {0}",
House.Condition);
Console.WriteLine("Bedrooms: {0}",
House.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
House.Bathrooms);
Console.WriteLine("Year Built: {0}",
House.YearBuilt);
Console.WriteLine("Market Value: {0:C}",
House.Value);
Console.WriteLine("----------------------------------");
}
public void SaveCondominium()
{
FileStream fsCondo = null;
BinaryFormatter bfCondo = new BinaryFormatter();
string strFilename = strPropertiesDirectory +
@"\Condominiums.alr";
try
{
fsCondo = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
bfCondo.Serialize(fsCondo, Condominiums);
}
finally
{
fsCondo.Close();
}
}
public void SaveTownhouse()
{
FileStream fsTown = null;
BinaryFormatter bfTown = new BinaryFormatter();
string strFilename = strPropertiesDirectory +
@"\Townhouses.alr";
try
{
fsTown = new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
bfTown.Serialize(fsTown, Townhouses);
}
finally
{
fsTown.Close();
}
}
public void SaveSingleFamily()
{
FileStream fsHouse = null;
BinaryFormatter bfHouse =
new BinaryFormatter();
string strFilename = strPropertiesDirectory +
@"\SingleFamilies.alr";
try
{
fsHouse =
new FileStream(strFilename,
FileMode.Create,
FileAccess.Write);
bfHouse.Serialize(fsHouse, SingleFamilies);
}
finally
{
fsHouse.Close();
}
}
public void ShowProperties()
{
int choice = 0;
Console.WriteLine();
ShowTitle();
try
{
Console.WriteLine("What properties do you want to see?");
Console.WriteLine("1. One Particular Property");
Console.WriteLine("2. Condominiums Only");
Console.WriteLine("3. Townhouses Only");
Console.WriteLine("4. Single Families Only");
Console.WriteLine("5. All properties");
Console.WriteLine("6. None");
Console.Write("Your Choice? ");
choice = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Selection");
}
switch (choice)
{
case 1:
ShowParticularProperty();
break;
case 2:
ShowCondominiums();
break;
case 3:
ShowTownhouses();
break;
case 4:
ShowSingleFamilies();
break;
case 5:
ShowCondominiums();
ShowTownhouses();
ShowSingleFamilies();
break;
default:
break;
}
}
public void ShowParticularProperty()
{
bool found = false;
FileStream fsProperty = null;
BinaryFormatter bfProperty = new BinaryFormatter();
Console.WriteLine("\n=======================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("------------------------");
Console.Write("\nEnter Property #: ");
this.strPropertyNumber = Console.ReadLine();
try
{
fsProperty = new FileStream(strPropertiesDirectory +
@"\Condominiums.alr",
FileMode.Open,
FileAccess.Read);
Condominiums =
(ArrayList)bfProperty.Deserialize(fsProperty);
foreach (Condominium c in Condominiums)
{
if (c.PropertyNumber == this.strPropertyNumber)
{
found = true;
Condo = c;
ShowCondominium();
return;
}
}
}
finally
{
fsProperty.Close();
}
try
{
fsProperty = new FileStream(strPropertiesDirectory +
@"\Townhouses.alr",
FileMode.Open,
FileAccess.Read);
Townhouses =
(ArrayList)bfProperty.Deserialize(fsProperty);
foreach (Townhouse t in Townhouses)
{
if (t.PropertyNumber == this.strPropertyNumber)
{
found = true;
TownHome = t;
ShowTownhouse();
return;
}
}
}
finally
{
fsProperty.Close();
}
try
{
fsProperty = new FileStream(strPropertiesDirectory +
@"\SingleFamilies.alr",
FileMode.Open,
FileAccess.Read);
SingleFamilies =
(ArrayList)bfProperty.Deserialize(fsProperty);
foreach (SingleFamily s in SingleFamilies)
{
if (s.PropertyNumber == this.strPropertyNumber)
{
found = true;
House = s;
ShowSingleFamily();
return;
}
}
}
finally
{
fsProperty.Close();
}
if (found == false)
{
Console.WriteLine("No property with that " +
"number was found in the database");
return;
}
}
public void ShowCondominiums()
{
FileStream fsProperty = null;
BinaryFormatter bfProperty = new BinaryFormatter();
try
{
fsProperty = new FileStream(strPropertiesDirectory +
@"\Condominiums.alr",
FileMode.Open,
FileAccess.Read);
Condominiums =
(ArrayList)bfProperty.Deserialize(fsProperty);
Console.WriteLine("\n=================================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("----------------------------------");
Console.WriteLine("Condominiums Listing");
Console.WriteLine("----------------------------------");
foreach (Condominium c in Condominiums)
{
Console.WriteLine("Property #: {0}",
c.PropertyNumber);
Console.WriteLine("Condition: {0}",
c.Condition);
Console.WriteLine("Bedrooms: {0}",
c.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
c.Bathrooms);
Console.WriteLine("Year Built: {0}",
c.YearBuilt);
Console.WriteLine("Handicapped Accessible Building: {0}",
Condo.HandicapAccessible);
Console.WriteLine("Market Value: {0:C}",
c.Value);
Console.WriteLine("----------------------------------");
}
}
finally
{
fsProperty.Close();
}
}
public void ShowTownhouses()
{
FileStream fsProperty = null;
BinaryFormatter bfProperty = new BinaryFormatter();
try
{
fsProperty = new FileStream(strPropertiesDirectory +
@"\Townhouses.alr",
FileMode.Open,
FileAccess.Read);
Townhouses = (ArrayList)bfProperty.Deserialize(fsProperty);
Console.WriteLine("\n=================================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("----------------------------------");
Console.WriteLine("Townhouses Listing");
Console.WriteLine("----------------------------------");
foreach (Townhouse t in Townhouses)
{
Console.WriteLine("Property #: {0}",
t.PropertyNumber);
Console.WriteLine("Stories: {0}",
t.Stories);
Console.WriteLine("Has Indoor Car Garage: {0}",
t.IndoorGarage);
Console.WriteLine("Finished Basement: {0}",
t.FinishedBasement);
Console.WriteLine("Condition: {0}",
t.Condition);
Console.WriteLine("Bedrooms: {0}",
t.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
t.Bathrooms);
Console.WriteLine("Year Built: {0}",
t.YearBuilt);
Console.WriteLine("Community Managed? {0}",
t.IsCommunityManaged);
Console.WriteLine("Market Value: {0:C}",
t.Value);
Console.WriteLine("----------------------------------");
}
}
finally
{
fsProperty.Close();
}
}
public void ShowSingleFamilies()
{
FileStream fsProperty = null;
BinaryFormatter bfProperty = new BinaryFormatter();
try
{
fsProperty = new FileStream(strPropertiesDirectory +
@"\SingleFamilies.alr",
FileMode.Open,
FileAccess.Read);
SingleFamilies =
(ArrayList)bfProperty.Deserialize(fsProperty);
Console.WriteLine("\n================================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("----------------------------------");
Console.WriteLine("Single Families Listing");
Console.WriteLine("----------------------------------");
foreach (SingleFamily s in SingleFamilies)
{
Console.WriteLine("Property #: {0}",
s.PropertyNumber);
Console.WriteLine("Stories: {0}",
s.Stories);
Console.WriteLine("Has Indoor Car Garage: {0}",
s.IndoorGarage);
Console.WriteLine("Finished Basement: {0}",
s.FinishedBasement);
Console.WriteLine("Condition: {0}",
s.Condition);
Console.WriteLine("Bedrooms: {0}",
s.Bedrooms);
Console.WriteLine("Bathrooms: {0:F}",
s.Bathrooms);
Console.WriteLine("Year Built: {0}",
s.YearBuilt);
Console.WriteLine("Market Value: {0:C}",
s.Value);
Console.WriteLine("----------------------------------");
}
}
finally
{
fsProperty.Close();
}
}
}
}
|
- Access the Program.cs file and change it as follows:
using System;
namespace RealEstate7
{
class Program
{
static int Main(string[] args)
{
char answer = 'q';
PropertyManagement listing = new PropertyManagement();
// Display the title
Console.WriteLine("\n================================");
Console.WriteLine(" =//= Altair Realty =//=");
Console.WriteLine("----------------------------------");
do
{
// Ask the user to select an option
try
{
Console.WriteLine("What do you want to do?");
Console.WriteLine("1. Create a property");
Console.WriteLine("2. Show the properties");
Console.WriteLine("3. Delete a property");
Console.WriteLine("0. Quit");
Console.Write("Your Choice? ");
answer = char.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Invalid Choice!!!");
}
switch (answer)
{
case '1':
listing.CreateProperty();
Console.WriteLine("\n");
break;
case '2':
listing.ShowProperties();
break;
case '3':
break;
default:
break;
}
} while ((answer == '1') ||
(answer == '2') ||
(answer == '3'));
Console.WriteLine();
return 0;
}
}
}
|
- Execute the application and test it. Here is an example:
================================
=//= Altair Realty =//=
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 246185
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 1
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 2
How many bedrooms? 2
How many bathrooms? 2
Year built: 1996
Is the building accessible to handicapped (y/n): n
Condominium Value: 285660
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 246185
Property Type: Condominium
Condition: Good
Bedrooms: 2
Bathrooms: 2.00
Year Built: 1996
Handicapped Accessible Building: False
Market Value: $285,660.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 512664
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 2
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 1
How many stories (levels)? 3
How many bedrooms? 3
How many bathrooms? 2.5
Does it have an indoor car garage (y/n): n
Is the basement finished(y/n): y
Year built: 1992
Is it community managed (y/n)? y
Property Value: 435660
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 512664
Property Type: Townhouse
Stories: 3
Has Indoor Car Garage: False
Finished Basement: True
Condition: Excellent
Bedrooms: 3
Bathrooms: 2.50
Year Built: 1992
Community Managed? True
Market Value: $435,660.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 802664
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 3
=//= Altair Realty =//=
-=- Property Creation -=-
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 1
How many stories (levels)? 3
How many bedrooms? 4
How many bathrooms? 3.5
Does it have an indoor car garage (y/n): y
Is the basement finished(y/n): y
Year built: 1995
House Value: 755820
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 802664
Property Type: Single Family
Stories: 3
Has Indoor Car Garage: True
Finished Basement: True
Condition: Excellent
Bedrooms: 4
Bathrooms: 3.50
Year Built: 1995
Market Value: $755,820.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 1
=======================
=//= Altair Realty =//=
-=- Property Creation -=-
------------------------
Enter Property #: 693524
Types of Properties
1. Condominium
2. Townhouse
3. Single Family
Enter Type of Property: 3
=//= Altair Realty =//=
-=- Property Creation -=-
Properties Conditions
1. Excellent
2. Good (may need minor repair)
3. Needs Repair
4. In Bad Shape (property needs major repair or rebuild)
Enter Property Condition: 3
How many stories (levels)? 2
How many bedrooms? 3
How many bathrooms? 2.5
Does it have an indoor car garage (y/n): n
Is the basement finished(y/n): n
Year built: 1964
House Value: 515665
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
----------------------------------
Property #: 693524
Property Type: Single Family
Stories: 2
Has Indoor Car Garage: False
Finished Basement: False
Condition: NeedsRepair
Bedrooms: 3
Bathrooms: 2.50
Year Built: 1964
Market Value: $515,665.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 0
Press any key to continue . . .
|
- Close the DOS window
- Execute the application again and test different options. Here are
examples:
================================
=//= Altair Realty =//=
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 2
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
What properties do you want to see?
1. One Particular Property
2. Condominiums Only
3. Townhouses Only
4. Single Families Only
5. All properties
6. None
Your Choice? 1
=======================
=//= Altair Realty =//=
------------------------
Enter Property #: 802664
----------------------------------
Property #: 802664
Property Type: Single Family
Stories: 3
Has Indoor Car Garage: True
Finished Basement: True
Condition: Excellent
Bedrooms: 4
Bathrooms: 3.50
Year Built: 1995
Market Value: $755,820.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 2
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
What properties do you want to see?
1. One Particular Property
2. Condominiums Only
3. Townhouses Only
4. Single Families Only
5. All properties
6. None
Your Choice? 3
=================================
=//= Altair Realty =//=
----------------------------------
Townhouses Listing
----------------------------------
Property #: 000002
Stories: 1
Has Indoor Car Garage: False
Finished Basement: False
Condition: Excellent
Bedrooms: 1
Bathrooms: 1.00
Year Built: 1960
Community Managed? False
Market Value: $20,000.00
----------------------------------
Property #: 512664
Stories: 3
Has Indoor Car Garage: False
Finished Basement: True
Condition: Excellent
Bedrooms: 3
Bathrooms: 2.50
Year Built: 1992
Community Managed? True
Market Value: $435,660.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 2
==================================
=//=//= Altair Realty =//=//=
-=-=-=- Properties Listing -=-=-=-
What properties do you want to see?
1. One Particular Property
2. Condominiums Only
3. Townhouses Only
4. Single Families Only
5. All properties
6. None
Your Choice? 4
================================
=//= Altair Realty =//=
----------------------------------
Single Families Listing
----------------------------------
Property #: 000003
Stories: 1
Has Indoor Car Garage: False
Finished Basement: False
Condition: Excellent
Bedrooms: 1
Bathrooms: 1.00
Year Built: 1960
Market Value: $30,000.00
----------------------------------
Property #: 802664
Stories: 3
Has Indoor Car Garage: True
Finished Basement: True
Condition: Excellent
Bedrooms: 4
Bathrooms: 3.50
Year Built: 1995
Market Value: $755,820.00
----------------------------------
Property #: 693524
Stories: 2
Has Indoor Car Garage: False
Finished Basement: False
Condition: NeedsRepair
Bedrooms: 3
Bathrooms: 2.50
Year Built: 1964
Market Value: $515,665.00
----------------------------------
What do you want to do?
1. Create a property
2. Show the properties
3. Delete a property
0. Quit
Your Choice? 0
Press any key to continue . . .
|
- Close the DOS window
Instead of the square brackets that allow you to
retrieve an item based on its position, you can look for an item based
on its complete definition. You have various options. You can first
"build" an item and ask the compiler to check whether any item
in the list matches your definition. To perform this search, you can
call the ArrayList.Contains() method. Its syntax is:
public virtual bool Contains(object item);
The item to look for is passed as argument to the
method. The compiler would look for exactly the item, using its
definition, in the list. If any detail of the argument fails to match
any item of the ArrayList list, the method would return false. If
all characteristics of the argument correspond to an item of the list,
the method returns true.
Another option to look for an item in a list
consists of calling the ArrayList.BinarySearch() method. It is
overloaded in three versions and one of them uses the following syntax:
public virtual int BinarySearch(object value);
The item to look for is passed argument to the
method.
As opposed to adding an item to a list, you may want
to remove one. To perform this operation, you have various options. You
can ask the compiler to look for an item in the list and if, or once,
the compile finds it, it would delete the item. To perform this type of
deletion, you can call the ArrayList.Remove() method. Its syntax
is:
public virtual void Remove(object obj);
This method accepts as argument the item that you
want to delete from the list. To perform this operation, the list must
not be read-only.
The Remove() method allows you to specify the
exact item you want to delete from a list. Another option you have
consists of deleting an item based on its position. This is done using
the RemoveAt() method whose syntax is:
public virtual void RemoveAt(int index);
With this method, the position of the item is passed
as argument. If the position is not valid because either it is lower or
higher than the current Count, the compiler would throw an ArgumentOutOfRangeException
exception.
To remove all items from a list at once, you can
call the ArrayList.Clear() method. Its syntax is:
public virtual void Clear();
|
|