Java Programming
Would you like to react to this message? Create an account in a few clicks or log in to continue.


here you can ask help for your java related problems
 
HomeLatest imagesSearchRegisterLog in

 

 Data Struc students read and study

Go down 
2 posters
AuthorMessage
Ak
Java Freak
Java Freak
Ak


Posts : 23
Join date : 2009-02-07

Data Struc students read and study Empty
PostSubject: Data Struc students read and study   Data Struc students read and study EmptySat Feb 09, 2013 2:01 am

Code:

using System;

class Address
{
    public string name;
    public string address;
}

class MethodParams
{
    public static void Main()
    {
        string myChoice;

        MethodParams mp = new MethodParams();

        do
      {
            // show menu and get input from user
            myChoice = mp.getChoice();

            // Make a decision based on the user's choice
            mp.makeDecision(myChoice);

            // Pause to allow the user to see the results
            Console.Write("Press Enter key to continue...");
            Console.ReadLine();
            Console.WriteLine();
        } while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit
    }

    // show menu and get user's choice
    string getChoice()
    {
        string myChoice;

        // Print A Menu
        Console.WriteLine("My Address Book\n");

        Console.WriteLine("A - Add New Address");
        Console.WriteLine("D - Delete Address");
        Console.WriteLine("M - Modify Address");
        Console.WriteLine("V - View Addresses");
        Console.WriteLine("Q - Quit\n");

        Console.WriteLine("Choice (A,D,M,V,or Q): ");

        // Retrieve the user's choice
        myChoice = Console.ReadLine();

        return myChoice;
    }

    // make decision
    void makeDecision(string myChoice)
    {
        Address addr = new Address();

        switch(myChoice)
        {
            case "A":
            case "a":
                addr.name = "Joe";
                addr.address = "C# Station";
                this.addAddress(ref addr);
                break;
            case "D":
            case "d":
                addr.name = "Robert";
                this.deleteAddress(addr.name);
                break;
            case "M":
            case "m":
                addr.name = "Matt";
                this.modifyAddress(out addr);
                Console.WriteLine("Name is now {0}.", addr.name);
                break;
            case "V":
            case "v":
                this.viewAddresses("Cheryl", "Joe", "Matt", "Robert");
                break;
            case "Q":
            case "q":
                Console.WriteLine("Bye.");
                break;
            default:
                Console.WriteLine("{0} is not a valid choice", myChoice);
                break;
        }
    }

    // insert an address
    void addAddress(ref Address addr)
    {
        Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address);
    }

    // remove an address
    void deleteAddress(string name)
    {
        Console.WriteLine("You wish to delete {0}'s address.", name);
    }

    // change an address
    void modifyAddress(out Address addr)
    {
        [You must be registered and logged in to see this link.] {0}.", addr.name); // causes error!
        addr = new Address();
        addr.name = "Joe";
        addr.address = "C# Station";
    }

    // show addresses
    void viewAddresses(params string[] names)
    {
        foreach (string name in names)
        {
            Console.WriteLine("Name: {0}", name);
        }
    }
}
Back to top Go down
https://javafreak.board-directory.net
junnel024




Posts : 1
Join date : 2013-03-15

Data Struc students read and study Empty
PostSubject: Re: Data Struc students read and study   Data Struc students read and study EmptyFri Mar 15, 2013 4:19 am

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataBindingForm
{
public partial class MainForm : Form
{

//View of the datatable
DataView yugosOnlyView;

// collection of cars object
List<Car> listCars = null;

// inventory information.
DataTable inventoryTable = new DataTable();

public MainForm()
{
InitializeComponent();

// fill the list with some cars
listCars = new List<Car>
{
new Car { ID = 100, PetName = "Chucky", Make = "BMW", Color = "Green"},
new Car { ID = 101, PetName = "Tiny", Make = "Yugo", Color = "White"},
new Car { ID = 102, PetName = "Ami", Make = "Yugo", Color = "Tan"},
new Car { ID = 103, PetName = "Pain Inducer", Make = "Caravan", Color = "Pink"},
new Car { ID = 104, PetName = "Fred", Make = "BMW", Color = "Green"},
new Car { ID = 105, PetName = "Sidd", Make = "BMW", Color = "Black"},
new Car { ID = 106, PetName = "Mel", Make = "Firebird", Color = "Red"},
new Car { ID = 107, PetName = "Sarah", Make = "Colt", Color = "Black"},
};
CreateDataTable();
CreateDataView();
}

private void CreateDataTable()
{
//create table schema.
DataColumn carIDColumn = new DataColumn("ID", typeof(int));
DataColumn carMakeColumn = new DataColumn("Make", typeof(string));
DataColumn carColorColumn = new DataColumn("Color", typeof(string));
DataColumn carPetNameColumn = new DataColumn("PetName", typeof(string));
carPetNameColumn.Caption = "Pet Name";
inventoryTable.Columns.AddRange(new DataColumn[] { carIDColumn,
carMakeColumn, carColorColumn, carPetNameColumn});

// iterate over the list<t> to make rows.
foreach (Car c in listCars)
{
DataRow newRow = inventoryTable.NewRow();
newRow["ID"] = c.ID;
newRow["Make"] = c.Make;
newRow["Color"] = c.Color;
newRow["PetName"] = c.PetName;
inventoryTable.Rows.Add(newRow);
}

//bind the datatable to the carInventoryGridview.
carInventoryGridview.DataSource = inventoryTable;
}

private void CreateDataView()
{
//set the table that is used to construct this view.
yugosOnlyView = new DataView(inventoryTable);

//now configure the views using a filter.
[You must be registered and logged in to see this link.] = "ID = '(0)' ";

//bind to the new grid.
dataGridYugosView.DataSource = yugosOnlyView;
}

//remove this row from the datarowcollection
private void btnRemoveCar_Click(object sender, EventArgs e)
{
try
{
if (carInventoryGridview.SelectedRows.Count > 0)
{
// remove multiple rows
/*foreach (DataGridViewRow row in carInventoryGridview.SelectedRows)
{
carInventoryGridview.Rows.RemoveAt(row.Index);
}*/

// remove rows 1 at a time
/*carInventoryGridview.Rows.RemoveAt(carInventoryGridview.SelectedRows[0].Index);
*/
// remove row using a filter
// find the right row to delete

}

DataRow[] rowToDelete = inventoryTable.Select(string.Format
("ID = {0}", txtCarToRemove.Text));

rowToDelete[0].Delete();
inventoryTable.AcceptChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void btnDisplayMake_Click(object sender, EventArgs e)
{
//build a filter based on user input.
string filterStr = string.Format
("Make = '{0}' ", txtMakeToView.Text);

// find all rows matching the filter.
DataRow[] makes = inventoryTable.Select(filterStr);
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Make");
dt.Columns.Add("PetName");
dt.Columns.Add("Color");
if (makes.Length == 0)
MessageBox.Show("Sorry, no cars...", "Selection error!");
else
{
string strMake = "";
for (int i = 0; i < makes.Length; i++)
{
//from the current row, get the PetName value.
dt.ImportRow(makes[i]);
}
dataGridYugosView.DataSource = dt;
}
}

//find the rows you want to edit with a filter.
private void btnChangeMake_Click(object sender, EventArgs e)
{
// confirm selection.
if (DialogResult.Yes ==
MessageBox.Show("Are you sure?? BMW are much nicer than Yugos!",
"Please Confirm!", MessageBoxButtons.YesNo))
{
//build a filter.
string filterStr = "Make='BMW'";
string strMake = string.Empty;

//find all rows matching the filter.
DataRow[] makes = inventoryTable.Select(filterStr);

//change all beemers to Yugos!
for (int i = 0; i < makes.Length; i++)
{
makes[i]["Make"] = "Yugo";
}
}
}
private void txtMakeToView_TextChanged(object sender, EventArgs e)
{
if (txtMakeToView.Text == string.Empty)
{
carInventoryGridview.DataSource = inventoryTable;
}
}

private void dataGridYugosView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
}
}
Back to top Go down
 
Data Struc students read and study
Back to top 
Page 1 of 1
 Similar topics
-
» Data Struc completion project read this
» 1st year students read and study this GoTo command
» read file from notepad using c#

Permissions in this forum:You cannot reply to topics in this forum
Java Programming :: Programming :: C# Programming-
Jump to: