Develop a program to solve the following problems. You need to identify and impl
ID: 3831819 • Letter: D
Question
Develop a program to solve the following problems. You need to identify and implement any
object classes where necessary.
1. Your lecturer requires a class attendance system. There is no limit to the number of
students that can register to this class. Every time, a lecture is conducted, students are
required to identify themselves to the system using their University ID e.g 14B1234, 15B2501. Once the student is
identified, her attendance will be incremented. You are required to implement:
a. register method – adding new student into the attendance system, specify their
ubd id and email address.
b. Login – increment attendance of a specified student into the system.
c. calculateAttendance – print the total attendance of a specified student.
2. A shop requires a cube stock inventory system. The shop has 30 food cubes for rent.
Each cube can holds any type of food. You are required to implement the system as
follows:
a. addFood method – adding the food type in each cube. Each cube can hold single
food type only e.g. sandwich, pasta etc., quantity, individual price and total price
of food type.
b. makeSale method – this method reduce the quantity of food stock for individual
cube by specified value and calculates the cost of total sale.
c. printStock method – this method print the current food stock for a specified
cube.
Explanation / Answer
As per the guidlines, I am solving first question here: -
Student.java
public class Student {
private int attendance;
private String univID;
private String emailID;
Student() {//Default constructer
}
Student(String b, String c) {//parametrized constructor
this.attendance = 0; // attendance will be zero on initializing
this.univID = b;
this.emailID = c;
}
public int getAttendance() {
return attendance;
}
public void setAttendance() {
this.attendance++; // increment attendance each time.
}
public String getUnivID() {
return univID;
}
public void setUnivID(String univID) {
this.univID = univID;
}
public String getEmailID() {
return emailID;
}
public void setEmailID(String emailID) {
this.emailID = emailID;
}
//Overriding equals and hashcode to enable the comparing of two Students
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((univID == null) ? 0 : univID.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (univID == null) {
if (other.univID != null)
return false;
} else if (!univID.equals(other.univID))
return false;
return true;
}
}
StudentDriver.java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDriver {
private static ArrayList<Student> students; // data structure ArrayList to
// hold the registered students
// in the class
public static void main(String[] args) {
students = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int op = -1;
while (op != 4) {
System.out.println("Please choose the option what you want to do.");
System.out.println("1. Register Student");
System.out.println("2. Login the attendance.");
System.out.println("3. Calculate Student's Attendance.");
System.out.println("4. Exit");
op = sc.nextInt();
sc.nextLine();// To consume the enter key pressed.
switch (op) {
case 1:
Student s = new Student();
System.out.print("Please Enter StudentID:");
s.setUnivID(sc.next());
System.out.print("Please Enter Student EmailID:");
s.setEmailID(sc.next());
register(s);
break;
case 2:
System.out.print("Please enter the university ID to login:");
String id = sc.next();
login(id);
break;
case 3:
System.out.print("Please enter the university ID to get the attendance record:");
String id1 = sc.next();
int att = calculateAttendance(id1);
if (att != -1) {
System.out.println("Attendance for this student =" + att);
}
break;
case 4:
break;
default:
System.out.println("Please enter a valid choice.");
}
}
sc.close();// closing scanner stream to prevent leakage
}
public static void register(Student st) {
if (students.contains(st)) {// If student is already registered
System.out.println("Student is already registered. Registration Failed.");
} else {
students.add(st);
System.out.println("Student Registered Successfully.");
}
}
public static boolean login(String id) {
Student s;
for (int i = 0; i < students.size(); i++) {
s = students.get(i);
if (s.getUnivID().equals(id)) {
s.setAttendance();
System.out.println("Attendance logged in successfully.");
return true;
}
}
System.out.println("You are not registered for this class. Please contact the Lecturer.");
return false;
}
public static int calculateAttendance(String id) {// method to get the
// attendance
// of student
Student s;
for (int i = 0; i < students.size(); i++) {
s = students.get(i);
if (s.getUnivID().equals(id)) {
return s.getAttendance();
}
}
System.out.println("You are not registered for this class. Please contact the Lecturer.");
return -1;
}
}
Sample Run: -
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
1
Please Enter StudentID:14B1234
Please Enter Student EmailID:14B1234@student.in
Student Registered Successfully.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
1
Please Enter StudentID:14B1234
Please Enter Student EmailID:14B1234@student.in
Student is already registered. Registration Failed.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
1
Please Enter StudentID:14B1234
Please Enter Student EmailID:14B1234
Student is already registered. Registration Failed.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
1
Please Enter StudentID:15B2501
Please Enter Student EmailID:15B2501@student.in
Student Registered Successfully.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
2
Please enter the university ID to login:15B2501
Attendance logged in successfully.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
2
Please enter the university ID to login:15B2501
Attendance logged in successfully.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
2
Please enter the university ID to login:15B25011
You are not registered for this class. Please contact the Lecturer.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
3
Please enter the university ID to get the attendance record:15B2501
Attendance for this student =2
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
5
Please enter a valid choice.
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
4
Please choose the option what you want to do.
1. Register Student
2. Login the attendance.
3. Calculate Student's Attendance.
4. Exit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.