You are on page 1of 5

import java.time.

LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

class Person {
protected String name;

public Person(String name) {


this.name = name;
}

public String getName() {


return name;
}
}

class Student extends Person {


private String id;
private String campus;
private boolean isCafeteriaUser;

public Student(String name, String id, String campus, boolean isCafeteriaUser)


{
super(name);
this.id = id;
this.campus = campus;
this.isCafeteriaUser = isCafeteriaUser;
}

public String getId() {


return id;
}

public String getCampus() {


return campus;
}

public boolean isCafeteriaUser() {


return isCafeteriaUser;
}
}

class Cafeteria {
private ArrayList<Student> registeredStudents = new ArrayList<>();
private String[] studentIds = new String[100]; // Assuming a maximum of 100
students
private boolean[] idTickStatus = new boolean[100]; // Assuming a maximum of
100 students

public void registerStudent(Scanner input) {


try {
System.out.println("\nRegistration Menu:");
System.out.println("1. Register as Cafeteria User");
System.out.println("2. Register as Non-Cafeteria User");
System.out.println("Enter your choice:");

int registrationChoice = input.nextInt();


input.nextLine(); // Consume newline character

switch (registrationChoice) {
case 1:
registerCafeteriaUser(input);
break;
case 2:
registerNonCafeteriaUser(input);
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
} catch (InputMismatchException e) {
System.out.println("An error occurred: " + e);
input.nextLine(); // Consume newline character to avoid infinite loop
}
}

private void registerCafeteriaUser(Scanner input) {


System.out.println("Enter student name:");
String name = input.nextLine();
System.out.println("Enter student ID:");
String id = input.nextLine();

// Check if the ID is already registered


if (findStudentIndexById(id) != -1) {
System.out.println("Error: Student with ID " + id + " is already
registered.");
return;
}

System.out.println("Enter student campus:");


String campus = input.nextLine();

registeredStudents.add(new Student(name, id, campus, true));

int index = findFirstAvailableIndex();


if (index != -1) {
studentIds[index] = id;
idTickStatus[index] = false;
System.out.println("Cafeteria user registered successfully.");
} else {
System.out.println("Error: Unable to register the student. Arrays are
full.");
}
}

private void registerNonCafeteriaUser(Scanner input) {


System.out.println("Enter student name:");
String name = input.nextLine();
System.out.println("Enter student ID:");
String id = input.nextLine();

// Check if the ID is already registered


if (findStudentIndexById(id) != -1) {
System.out.println("Error: Student with ID " + id + " is already
registered.");
return;
}
System.out.println("Enter student campus:");
String campus = input.nextLine();

registeredStudents.add(new Student(name, id, campus, false));

int index = findFirstAvailableIndex();


if (index != -1) {
studentIds[index] = id;
idTickStatus[index] = false;
System.out.println("Non-cafeteria user registered successfully.");
} else {
System.out.println("Error: Unable to register the student. Arrays are
full.");
}
}

public void tickId(Scanner input) {


try {
System.out.println("Enter student ID to tick:");
String id = input.nextLine();

int index = findStudentIndexById(id);

if (index != -1) {
Student student = registeredStudents.get(index);

if (student.isCafeteriaUser()) {
if (!idTickStatus[index]) {
if (student.getCampus().equalsIgnoreCase("Main")) {
// Set the time zone to East Africa Time (GMT+03:00)
LocalTime currentLocalTime =
LocalTime.now(ZoneId.of("Africa/Nairobi"));

// Check if the current time is within meal times


if ((currentLocalTime.isAfter(LocalTime.of(6, 30)) &&
currentLocalTime.isBefore(LocalTime.of(23, 0)))
|| (currentLocalTime.isAfter(LocalTime.of(11, 0))
&& currentLocalTime.isBefore(LocalTime.of(13, 0)))
|| (currentLocalTime.isAfter(LocalTime.of(17, 0))
&& currentLocalTime.isBefore(LocalTime.of(23, 0)))) {
idTickStatus[index] = true;
System.out.println("ID ticked successfully for student
with ID: " + id);
} else {
System.out.println("ID ticking is allowed only during
meal times.");
}
} else {
System.out.println("ID ticking is allowed only for students
in the main campus.");
}
} else {
System.out.println("The ID has already been ticked before.");
}
} else {
System.out.println("ID ticking is allowed only for cafeteria user
students.");
}
} else {
System.out.println("The ID is not registered.");
}
} catch (InputMismatchException e) {
System.out.println("An error occurred: " + e);
input.next();
}
}

// Helper method to find the first available index in arrays


private int findFirstAvailableIndex() {
for (int i = 0; i < studentIds.length; i++) {
if (studentIds[i] == null) {
return i;
}
}
return -1; // Return -1 if arrays are full
}

// Helper method to find the index of a student by ID


private int findStudentIndexById(String id) {
for (int i = 0; i < studentIds.length; i++) {
if (studentIds[i] != null && studentIds[i].equals(id)) {
return i;
}
}
return -1; // Return -1 if the student is not found
}
public void deleteStudent(Scanner input) {
try {
System.out.println("Enter student ID to delete:");
String id = input.nextLine();

int index = findStudentIndexById(id);

if (index != -1) {
registeredStudents.remove(index); // Remove student from the list
idTickStatus[index] = false; // Mark as not ticked
System.out.println("Student with ID " + id + " deleted successfully.");
}
else {
System.out.println("Student with ID " + id + " not found.");
}
} catch (InputMismatchException e) {
System.out.println("you entered Invalid input , integer is required:
"+e );
input.next();
}
}

public void displayRegisteredStudents() {


if (registeredStudents.isEmpty()) {
System.out.println("No students registered yet.");
} else {
System.out.println("Registered Students:");
for (Student student : registeredStudents) {
System.out.println(" Name: " + student.getName() +" ,ID: " +
student.getId() +
", Campus: " + student.getCampus() + ", Cafeteria User: " +
student.isCafeteriaUser());
}
}
}
}

public class Main {


public static void main(String[] args) {
Cafeteria cafeteria = new Cafeteria();
Scanner input = new Scanner(System.in);

int choice = 0;
do {
try {
System.out.println("\nMain Menu:");
System.out.println("1. Register new student");
System.out.println("2. ID ticking system");
System.out.println("3. Delete registered student information");
System.out.println("4. Display registered students");
System.out.println("5. Exit");
System.out.println("Enter your choice:");

choice = input.nextInt();
input.nextLine(); // Consume newline character

switch (choice) {
case 1:
cafeteria.registerStudent(input);
break;
case 2:
cafeteria.tickId(input);
break;
case 3:
cafeteria.deleteStudent(input);
break;
case 4:
cafeteria.displayRegisteredStudents();
break;
case 5:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please enter a valid
option.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input , integer is required: "+e );
input.next();
}
} while (choice != 5);

input.close();
}
}

You might also like