0% found this document useful (0 votes)
32 views

Ilist and Arraylist Program: Using Using Using Using Namespace Class Static Void String New New New New

This document discusses using IList and ArrayList in C# to create a bookstore program. It defines classes for Book, BookStore, and a Main method. The Main method initializes a BookStore, adds sample books, and runs a menu loop where the user can add books, display all books, or search for a book by name and quantity. The BookStore class implements methods to add books to an ArrayList, display all books by iterating the list, and search the list to find a book matching the name and check quantity is available.

Uploaded by

Rachit Harlalka
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Ilist and Arraylist Program: Using Using Using Using Namespace Class Static Void String New New New New

This document discusses using IList and ArrayList in C# to create a bookstore program. It defines classes for Book, BookStore, and a Main method. The Main method initializes a BookStore, adds sample books, and runs a menu loop where the user can add books, display all books, or search for a book by name and quantity. The BookStore class implements methods to add books to an ArrayList, display all books by iterating the list, and search the list to find a book matching the name and check quantity is available.

Uploaded by

Rachit Harlalka
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ILIST and ARRAYLIST Program

using using using using System; System.Collections; System.Linq; System.Text;

namespace BookStoreDemo { class Program { static void Main(string[] args) { BookStore b = new BookStore(); Book b1 = new Book("C#", 5, 200); Book b2 = new Book("J#", 54, 2500); Book b3 = new Book("F#", 52, 2060); b.AddBook(b1); b.AddBook(b2); b.AddBook(b3); string c; do { Console.WriteLine("select 1 to add book: 2 to display and 3 to search and 4 to exit");

ILIST and ARRAYLIST


c = Console.ReadLine(); switch (c) { case "1": Console.WriteLine("book name"); string name = Console.ReadLine(); Console.WriteLine("enetr qty"); int Qty = int.Parse(Console.ReadLine()); Console.WriteLine("enter cost"); double cost = double.Parse(Console.ReadLine()); b.AddBook(new Book(name, Qty, cost)); break; case "2": b.Display(); break; case "3": Console.WriteLine("ENTER BOOK NAME"); String val = Console.ReadLine(); Console.WriteLine("ENETR QTY"); int qty = int.Parse(Console.ReadLine()); b.search(val, qty); break; default: c = "4"; break;

} } while (c != "4"); } } }

ILIST and ARRAYLIST Book


using using using using System; System.Collections; System.Linq; System.Text;

namespace BookStoreDemo { class Book { String BookName; int Qty; double Cost; public Book(String BookName, int Qty, double Cost) { this.BookName = BookName; this.Qty = Qty; this.Cost = Cost; } public void SetName(String name) { BookName = name; } public void SetQty(int qty) {

ILIST and ARRAYLIST


Qty = qty; } public void SetCost(double cost) { Cost = cost; } public String GetName() { return BookName; } public int GetQty() { return Qty; } public double GetCost() { return Cost; } } }

ILIST and ARRAYLIST BookStore


namespace BookStoreDemo { class BookStore { IList l; public BookStore() { l = new ArrayList(); } public void AddBook(Book b) { l.Add(b); } public void Display() { IEnumerator ar = l.GetEnumerator(); while (ar.MoveNext()) { Book bk = (Book)ar.Current; Console.WriteLine("Book name " + bk.GetName()+ " Qty " + bk.GetQty() + " Cost " + bk.GetCost()); }

ILIST and ARRAYLIST


} public void search(String Bookname, int qty) { IEnumerator ar = l.GetEnumerator(); int f = 0; while (ar.MoveNext()) { Book bk = (Book)ar.Current; int value = String.Compare(bk.GetName(), Bookname, true); if (value==0) { f = 1; if (bk.GetQty() >= qty) { Console.WriteLine("Book Found"); double cost = bk.GetCost()*qty; Console.WriteLine("total cost is : " + cost); break; } else Console.WriteLine("Book found but insufficient qty"); } } if (f == 0) Console.WriteLine("Book not found"); } } }

You might also like