-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentService.java
More file actions
29 lines (23 loc) · 970 Bytes
/
Copy pathStudentService.java
File metadata and controls
29 lines (23 loc) · 970 Bytes
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
import java.util.Scanner;
// Service class to handle student creation and input
public class StudentService {
Scanner sc = new Scanner(System.in); // Scanner object for user input
// Method to create a new student (UG or PG)
public Student createStudent() {
System.out.print("Enter Name: ");
String name = sc.nextLine(); // Input student name
System.out.print("Enter Roll No: ");
int roll = sc.nextInt(); // Input roll number
System.out.print("Enter Marks: ");
int marks = sc.nextInt(); // Input marks
sc.nextLine(); // consume leftover newline
System.out.print("1. UG Student 2. PG Student : ");
int choice = sc.nextInt(); // Choose student type
sc.nextLine();
// Return UGStudent or PGStudent based on choice
if (choice == 1)
return new UGStudent(name, roll, marks);
else
return new PGStudent(name, roll, marks);
}
}