Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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