In this C# tutorial we are "developing a simple program to display student's report and book report using classes" . In this program we have taken three classes i.e student, book and prg as you see in line 3, 32 and 70 respectively. Also, class student is for student's report, class book is for books report and class prg is our main class to call both classes student and book.
Also we have taken four methods i.e read() for input, print() for output, line() for displaying line and cal() to calculate total book amount. We also used switch for calling these classes. Also, you see in line 83 and 89 we have taken objects obj and obj1 to access members of classes student and book. Here for methods we have used access specifier public. Now, lets get started!!
using System; class student // class for student's report { string name; int age; int rollno; public void read() // input method for student's report { Console.WriteLine("Please enter name, age and rollno of student..."); name=Console.ReadLine(); age=Convert.ToInt32(Console.ReadLine()); rollno=Convert.ToInt32(Console.ReadLine()); } public void line() // method to display line { for(int i=0;i<30;i++) { Console.Write("*"); } } public void print() // method to display student's report { Console.WriteLine("\nName of student : "+ name); Console.WriteLine("\nAge : "+ age); Console.WriteLine("\nRoll no :"+ rollno); } } class book // class for book report { string name; int price; string edition; string author; int no_bk; public void read() // input method for book report { Console.WriteLine("Please enter name,price,author,edition and no of books..."); name=Console.ReadLine(); price=Convert.ToInt32(Console.ReadLine()); author=Console.ReadLine(); edition=Console.ReadLine(); no_bk=Convert.ToInt32(Console.ReadLine()); } public int cal() // method to calculate total amount { int a; a=no_bk * price; return (a); } public void print() // method to display book report { Console.WriteLine("Book Name : "+name); Console.WriteLine("Price : "+price); Console.WriteLine("Author : "+author); Console.WriteLine("Book Edition : "+edition); Console.WriteLine("Total Amount : "+ cal()); } } class prg // main class to call classes student and book { public static void Main(String []args) { int ch; char ch1= ' '; do { Console.WriteLine("Please enter your choice...(press 1 for student report and press 2 for book report)"); ch=Convert.ToInt32(Console.ReadLine()); // for choice switch(ch) { case 1: student obj = new student(); // object "obj" to access members of student's class obj.read(); obj.line(); obj.print(); break; case 2: book obj1 = new book(); // object "obj1" to access members of book's report class obj1.read(); obj1.cal(); obj1.print(); break; default: Console.WriteLine("Wrong Choice...."); break; } Console.WriteLine("Do you want to continue?? Press Y!!"); ch1=Convert.ToChar(Console.ReadLine()); }while(ch1=='Y' || ch1=='y'); } }
0 comments:
Post a Comment