Write a program of an array of object of room class to get, calculate and display the information
In this program, Room class is used for get details from the user and calculate amount of room charge and display details. Here member variables and member function is like this,
Member variable : roomNo, roomType, roomChargePerDay, roomAllocatedDays, totalPayment
Member Function : getRoomData() , calculatePayment() , displayRoomData()
getRoomData() is used to get all details from the user. calculatePayment() is used calculate all the task which fill up in details. displayRoomData() is used to display all the details with calculations.
Step of Execute Program :
1) Open Command Prompt
2) Set Path
3) Compile File //javac room.java
4) Run File // java room1
5) Fill the Details
Output :
Member variable : roomNo, roomType, roomChargePerDay, roomAllocatedDays, totalPayment
Member Function : getRoomData() , calculatePayment() , displayRoomData()
getRoomData() is used to get all details from the user. calculatePayment() is used calculate all the task which fill up in details. displayRoomData() is used to display all the details with calculations.
Step of Execute Program :
1) Open Command Prompt
2) Set Path
3) Compile File //javac room.java
4) Run File // java room1
5) Fill the Details
File Name : room.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import java.io.*; class room { private int roomNo; private int roomType; private double roomChargePerDay; private int roomAllocatedDays; private double totalPayment; room() { this.roomNo=0; this.roomChargePerDay=0; this.roomAllocatedDays=0; this.totalPayment=0; } public void getRoomData(int roomNo,int roomType,double roomChargePerDay,int roomAllocatedDays) { this.roomNo=roomNo; this.roomType=roomType; this.roomChargePerDay=roomChargePerDay; this.roomAllocatedDays=roomAllocatedDays; this.calculatePayment(); } private void calculatePayment() { this.totalPayment=roomChargePerDay*roomAllocatedDays; } public void displayRoomData() { if (roomType==1) { System.out.println("----------------------------------------------------------------------\n"); System.out.println("Room No: " + roomNo + "\n"); System.out.println("Room Type : Regular"); System.out.print("Room Charges:" + roomChargePerDay); System.out.println("\t\t\t Allocated Days : " + roomAllocatedDays+"\n"); System.out.println("\t \t \t Total charges : " + totalPayment); System.out.println("--------------------------------------------------------------------------\n"); } else if (roomType==2) { System.out.println("----------------------------------------------------------------------\n"); System.out.println("Room No: " + roomNo + "\n"); System.out.println("Room Type : Delux"); System.out.print("Room Charges:" + roomChargePerDay); System.out.println("\t\t\t Allocated Days : " + roomAllocatedDays+"\n"); System.out.println("\t \t \t Total charges : " + totalPayment); System.out.println("--------------------------------------------------------------------------\n"); } else if(roomType==3) { System.out.println("----------------------------------------------------------------------\n"); System.out.println("Room No: " + roomNo + "\n"); System.out.println("Room Type : Super Delux"); System.out.print("Room Charges:" + roomChargePerDay); System.out.println("\t\t\t Allocated Days : " + roomAllocatedDays+"\n"); System.out.println("\t \t \t Total charges : " + totalPayment); System.out.println("--------------------------------------------------------------------------\n"); } else { System.out.println("Wrong Input"); } } } class room1 { public static void main(String[] args) { try { DataInputStream in = new DataInputStream(System.in); System.out.print("\nHow many rooms allocating :"); int roomAllocate=Integer.parseInt(in.readLine()); room r[]=new room[roomAllocate]; for(int i=0;i<roomAllocate;i++) { r[i]=new room(); System.out.println("\n\t\t::::::::::Room Information Form::::::::::\n"); System.out.print("Enter Room No.: "); int roomNo=Integer.parseInt(in.readLine()); System.out.println("Select Room Type:-\n 1. Regular\n 2. Delux\n 3. Super Delux"); int roomType=Integer.parseInt(in.readLine()); System.out.print("Enter Room Charges Per Day: "); double roomChargesPerDay=Double.parseDouble(in.readLine()); System.out.print("Enter Room Allocated Days: "); int roomAllocatedDays=Integer.parseInt(in.readLine()); r[i].getRoomData(roomNo,roomType,roomChargesPerDay,roomAllocatedDays); } for(int i=0;i<roomAllocate;i++) r[i].displayRoomData(); } catch(IOException e) { System.out.println("Something Goes Wrong..."); System.out.println(e.getMessage()); } } } |
Output :
calculate and display the information, Create an array of object of room class to get, GTU Java Practical, Java Program, Java Tutorial, Write a program to create a room class
0 comments:
Post a Comment