Skip to content

Commit 6d68d19

Browse files
author
ashishyadav2
committedOct 20, 2024
add new files
1 parent d7ed67b commit 6d68d19

File tree

6 files changed

+305
-0
lines changed

6 files changed

+305
-0
lines changed
 

‎Java2/Temp/pra2/InputPractice2.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package Temp.pra2;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.Scanner;
6+
class CustomException extends Exception {
7+
public CustomException(String m){
8+
super(m);
9+
}
10+
}
11+
class cls {
12+
double id;
13+
String name;
14+
int nProducts;
15+
ArrayList<String> products;
16+
ArrayList<Integer> prices;
17+
18+
public cls(double id, String name, int nProducts, ArrayList<String> products, ArrayList<Integer> prices) {
19+
this.id = id;
20+
this.name = name;
21+
this.nProducts = nProducts;
22+
this.products = products;
23+
this.prices = prices;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "cls{" +
29+
"id=" + id +
30+
", name='" + name + '\'' +
31+
", nProducts=" + nProducts +
32+
", products=" + products +
33+
", prices=" + prices +
34+
'}';
35+
}
36+
}
37+
public class InputPractice2 {
38+
public static double findAvg(ArrayList<cls> arr, String op1){
39+
try{
40+
double sum = 0;
41+
int size = 0;
42+
for(cls c: arr){
43+
if(c.name.equalsIgnoreCase(op1)){
44+
for(int a: c.prices){
45+
sum+=a;
46+
size++;
47+
}
48+
}
49+
}
50+
if(size!=0){
51+
return sum/size;
52+
}
53+
else {
54+
throw new CustomException("Not Found");
55+
}
56+
}
57+
catch (CustomException e){
58+
e.printStackTrace();
59+
}
60+
return -1;
61+
}
62+
public static void main(String[] args) throws Exception {
63+
String path = "C:\\Users\\ashis\\IdeaProjects\\JavaLessons\\src\\Temp\\pra2\\input.txt";
64+
File fileObj = new File(path);
65+
Scanner sc = new Scanner(fileObj);
66+
int n = Integer.parseInt(sc.nextLine().trim());
67+
ArrayList<cls> arr = new ArrayList<>();
68+
for(int i=0;i<n;i++) {
69+
double id = Double.parseDouble(sc.nextLine().trim());
70+
String name = sc.nextLine();
71+
int nProduct = Integer.parseInt(sc.nextLine().trim());
72+
ArrayList<String> products = new ArrayList<>();
73+
ArrayList<Integer> prices = new ArrayList<>();
74+
for(int j =0;j<nProduct;j++){
75+
String s1 = sc.nextLine();
76+
int p = Integer.parseInt(sc.nextLine().trim());
77+
products.add(s1);
78+
prices.add(p);
79+
}
80+
arr.add(new cls(id,name,nProduct,products,prices));
81+
}
82+
int op1 = Integer.parseInt(sc.nextLine().trim());
83+
String op2 = sc.nextLine();
84+
int op3 = Integer.parseInt(sc.nextLine().trim());
85+
System.out.println(op1);
86+
System.out.println(op2);
87+
System.out.println(op3);
88+
System.out.println(arr);
89+
System.out.println(findAvg(arr,op2));
90+
}
91+
}

‎Java2/Temp/pra2/Practice1.java

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package Temp.pra2;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.ArrayList;
6+
import java.util.Scanner;
7+
8+
class Room {
9+
String rType;
10+
int pricePN;
11+
int nBooked;
12+
13+
public Room(String rType,int pricePN, int nBooked){
14+
this.rType = rType;
15+
this.pricePN = pricePN;
16+
this.nBooked = nBooked;
17+
}
18+
public int roomRevenue(){
19+
return this.pricePN*this.nBooked;
20+
}
21+
}
22+
class Hotel {
23+
int hId;
24+
String hName;
25+
ArrayList<Room> rooms;
26+
27+
public Hotel(int hId, String hName, ArrayList<Room> rooms) {
28+
this.hId = hId;
29+
this.hName = hName;
30+
this.rooms = rooms;
31+
}
32+
public int hotelRevenue(){
33+
int sum = 0;
34+
for(Room r: rooms){
35+
sum += r.roomRevenue();
36+
}
37+
return sum;
38+
}
39+
}
40+
class BM{
41+
public Hotel maxRevenue(ArrayList<Hotel> arr){
42+
int max = -1;
43+
Hotel ans = null;
44+
String name = "";
45+
for(Hotel h: arr){
46+
if(h.hotelRevenue()>max){
47+
max = h.hotelRevenue();
48+
ans = h;
49+
name = h.hName;
50+
}
51+
}
52+
return ans;
53+
}
54+
}
55+
public class Practice1 {
56+
public static void main(String[] args) throws FileNotFoundException {
57+
String path = "C:\\Users\\ashis\\IdeaProjects\\JavaLessons\\src\\Temp\\pra2\\input3.txt";
58+
File fileObj = new File(path);
59+
Scanner sc = new Scanner(fileObj);
60+
int n = Integer.parseInt(sc.nextLine().trim());
61+
ArrayList<Hotel> arr = new ArrayList<>();
62+
for(int i =0;i<n;i++){
63+
int id = Integer.parseInt(sc.nextLine().trim());
64+
String name = sc.nextLine();
65+
int nRooms = Integer.parseInt(sc.nextLine().trim());
66+
ArrayList<Room> rooms = new ArrayList<>();
67+
for( int j=0;j<nRooms;j++){
68+
String rName = sc.nextLine();
69+
int pricePN = Integer.parseInt(sc.nextLine().trim());
70+
int nBooked = Integer.parseInt(sc.nextLine().trim());
71+
rooms.add(new Room(rName,pricePN,nBooked));
72+
}
73+
arr.add(new Hotel(id,name,rooms));
74+
}
75+
for(int k=0;k<arr.size();k++){
76+
System.out.printf("Total Revenue for Hotel Paradise: %d%n",arr.get(k).hotelRevenue());
77+
}
78+
BM obj = new BM();
79+
System.out.printf("The hotel with the highest revenue is: %s",obj.maxRevenue(arr).hName);
80+
}
81+
}

‎Java2/Temp/pra2/Practice2.java

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package Temp.pra2;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.Scanner;
8+
class Student {
9+
int sid;
10+
String name;
11+
ArrayList<String> subjects;
12+
ArrayList<Integer> marks;
13+
14+
public Student(int sid, String name, ArrayList<String> subjects, ArrayList<Integer> marks) {
15+
this.sid = sid;
16+
this.name = name;
17+
this.subjects = subjects;
18+
this.marks = marks;
19+
}
20+
21+
public double getAvg(){
22+
double sum = 0;
23+
int size = 0;
24+
for(int a: this.marks){
25+
sum+=a;
26+
size+=1;
27+
}
28+
return sum/size;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "Student{" +
34+
"sid=" + sid +
35+
", name='" + name + '\'' +
36+
", subjects=" + subjects +
37+
", marks=" + marks +
38+
'}';
39+
}
40+
}
41+
public class Practice2 {
42+
public static ArrayList<Student> findAns(ArrayList<Student> arr, double param1){
43+
ArrayList<Student> ans = new ArrayList<>();
44+
for(Student s: arr){
45+
if(s.getAvg()>param1){
46+
ans.add(s);
47+
}
48+
}
49+
return ans;
50+
}
51+
public static ArrayList<Student> getStudents(ArrayList<Student> arr){
52+
ArrayList<Student> ans = new ArrayList<>();
53+
for(Student s: arr){
54+
HashSet<Integer> set = new HashSet<>(s.marks);
55+
if(s.marks.size()==set.size()){
56+
ans.add(s);
57+
}
58+
}
59+
return ans;
60+
}
61+
public static void main(String[] args) throws Exception{
62+
String path = "C:\\Users\\ashis\\IdeaProjects\\JavaLessons\\src\\Temp\\pra2\\input4.txt";
63+
File fileObj = new File(path);
64+
Scanner sc = new Scanner(fileObj);
65+
int n = Integer.parseInt(sc.nextLine().trim());
66+
ArrayList<Student> arr = new ArrayList<>();
67+
for( int i=0;i<n;i++){
68+
int sid = Integer.parseInt(sc.nextLine().trim());
69+
String name = sc.nextLine();
70+
int nSubs = Integer.parseInt(sc.nextLine().trim());
71+
ArrayList<String> subjects = new ArrayList<>();
72+
ArrayList<Integer> marks = new ArrayList<>();
73+
for(int j=0;j<nSubs;j++){
74+
String subject = sc.nextLine();
75+
int mark = Integer.parseInt(sc.nextLine().trim());
76+
subjects.add(subject);
77+
marks.add(mark);
78+
}
79+
arr.add(new Student(sid,name,subjects,marks));
80+
}
81+
double param1 = Double.parseDouble(sc.nextLine().trim());
82+
ArrayList<Student> ans = findAns(arr,param1);
83+
for(Student s: ans){
84+
System.out.println(s.name);
85+
}
86+
ArrayList<Student> ans2 = getStudents(arr);
87+
for(Student s: ans2){
88+
System.out.println(s);
89+
}
90+
}
91+
}

‎Java2/Temp/pra2/input2.txt

Whitespace-only changes.

‎Java2/Temp/pra2/input3.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2
2+
1
3+
Hotel Paradise
4+
2
5+
Deluxe Suite
6+
500
7+
3
8+
Standard Room
9+
200
10+
5
11+
2
12+
Hotel Sunshine
13+
2
14+
Suite
15+
700
16+
2
17+
Economy Room
18+
100
19+
8

‎Java2/Temp/pra2/input4.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
3
2+
101
3+
yash
4+
2
5+
math
6+
56
7+
eng
8+
89
9+
102
10+
raj
11+
2
12+
math
13+
89
14+
eng
15+
89
16+
103
17+
ashish
18+
2
19+
math
20+
56
21+
eng
22+
89
23+
75.15

0 commit comments

Comments
 (0)
Please sign in to comment.