Code:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class classrecord {
private Map<String, Map<String, String>> myRecord = new HashMap<String, Map<String, String>>();
final Scanner scanIn = new Scanner(System.in);
private int studentSize, gradeSize;
String myChoice = "";
private void startHere(){
try{
switch(menu()){
case 0: System.exit(0);
break;
case 1: initializeRecord();
break;
case 2: newStudentRecord();
break;
case 3: showClassRecord();
break;
case 4: deleteStudentRecord();
break;
case 5: editStudentRecord();
break;
default:
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::: Invalid choice! ::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
break;
}
}catch(NumberFormatException e){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::: Invalid choice! ::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
}
}
private int menu(){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::: My Class Record ::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("[1] Initialize Record");
System.out.println("[2] Enter Student Record");
System.out.println("[3] Show Class Record");
System.out.println("[4] Delete Student");
System.out.println("[5] Edit Student");
System.out.println("[0] Exit");
System.out.println(":::::::::::::::::::::::::::::");
return Integer.parseInt(scanIn.nextLine().trim());
}
private void initializeRecord(){
if(getStudentSize()<=0){
try{
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Initiliaze Record::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("[C] to cancel.");
System.out.println("Enter class size: ");
myChoice = scanIn.nextLine().trim();
setStudentSize(Integer.parseInt(myChoice));
initNumberOfGrades();
}catch(NumberFormatException nfe){
if(myChoice.trim().equalsIgnoreCase("c")){
notifyCancelled();
setStudentSize(0);
startHere();
}
System.out.println("Please enter INTEGER only!");
initializeRecord();
}
}else{
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Done Initialized! :::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
}
}
private void initNumberOfGrades(){
if(getStudentSize()<=0){
initializeRecord();
}
if(getGradeSize()>0 || getStudentSize()<=0){
System.out.println("Invalid Access");
}
if(getGradeSize()<=0 && getStudentSize()>0){
try{
System.out.println("[C] to cancel.");
System.out.println("Enter number of grades: ");
myChoice = scanIn.nextLine().trim();
setGradeSize(Integer.parseInt(myChoice));
}catch(NumberFormatException nfe){
if(myChoice.trim().equalsIgnoreCase("c")){
notifyCancelled();
//Reset sizes to 0
setStudentSize(0);
setGradeSize(0);
//go back to menu
startHere();
}
System.out.println("Please enter INTEGER only!");
initNumberOfGrades();
}
System.out.println("Class Size: "+getStudentSize());
System.out.println("Number of Grades: "+getGradeSize());
startHere();
}
}
private void newStudentRecord(){
//Checks if na-init na ang record
if(getStudentSize()<=0){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Init Record First :::::");
System.out.println(":::::::::::::::::::::::::::::");
startHere();
}
if(myRecord.size()<getStudentSize() && getStudentSize()>0){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::::: New Student ::::::::");
System.out.println(":::::::::::::::::::::::::::::");
Map<String, String> newStudent = new HashMap<String, String>();
String id;
//Enters ID of student
System.out.println("[C] to Cancel");
System.out.println("Enter ID:");
id = scanIn.nextLine().trim();
if(id.equalsIgnoreCase("c")){
notifyCancelled();
startHere();
}
if(idExists(id)){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::: Already in Database! :::");
System.out.println("::::: Please try again! :::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
newStudentRecord();
}else{
//put ID in database
newStudent.put("ID", id);
//Enters name of student
System.out.println("[C] to Cancel");
System.out.println("Enter student name: ");
myChoice = scanIn.nextLine().trim();
if(myChoice.equalsIgnoreCase("c")){
notifyCancelled();
startHere();
}
newStudent.put("name",myChoice);
//Enters grade of student
for(int x=0;x<getGradeSize();x++){
System.out.println("Enter Grade ["+x+"]: ");
myChoice = scanIn.nextLine().trim();
newStudent.put(String.valueOf(x),myChoice);
}
myRecord.put(id,newStudent);
startHere();
}
}else{
System.out.println("Class Record is full!");
startHere();
}
}
private void showClassRecord(){
checkpoint();
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::: Start of Record ::::::");
for(String keys : myRecord.keySet()){
System.out.println("ID: ["+myRecord.get(keys).get("ID")+"]");
System.out.println("Name: "+myRecord.get(keys).get("name"));
System.out.println("::::::::::: Grades ::::::::::");
for(int x=0;x<getGradeSize();x++){
try{
System.out.println("Grade No.["+x+"]: "+myRecord.get(keys).get(String.valueOf(x)));
}catch(Exception e){
System.out.println("No record on Grade no.: ["+x+"]");
}
}
System.out.println("!---------------------------!");
}
System.out.println("::::::: End of Record :::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
}
private void deleteStudentRecord(){
checkpoint();
//lahos diri kung naa'y record or na-initialize na..
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::: Delete Student :::::::");
System.out.println(":::::::::::::::::::::::::::::");
for(String keys : myRecord.keySet()){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("ID: "+myRecord.get(keys).get("ID"));
System.out.println("Name: "+myRecord.get(keys).get("name"));
System.out.println(":::::::::::::::::::::::::::::");
}
System.out.println("[C] to Cancel");
System.out.println("Please Choose ID:");
myChoice = scanIn.nextLine().trim();
if(myChoice.equalsIgnoreCase("c")){
notifyCancelled();
startHere();
}
if(idExists(myChoice)){
myRecord.remove(myChoice);
System.out.println(":::::::::::::::::::::::::::::");
System.out.println(":::::: Student Deleted ::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
}else{
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Student Not Found :::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
deleteStudentRecord();
}
startHere();
}
private void editStudentRecord(){
checkpoint();
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::::: Edit Student ::::::::");
System.out.println(":::::::::::::::::::::::::::::");
for(String keys : myRecord.keySet()){
System.out.println(":::::::::::::::::::::::::::::::::::");
System.out.println("ID: "+myRecord.get(keys).get("ID"));
System.out.println("Name: "+myRecord.get(keys).get("name"));
System.out.println(":::::::::::::::::::::::::::::::::::");
}
System.out.println("[C] to Cancel");
System.out.println("Please Choose ID:");
String id = scanIn.nextLine().trim();
if(id.equalsIgnoreCase("c")){
notifyCancelled();
startHere();
}
if(idExists(id)){
Map<String, String> modifyStudent = new HashMap<String, String>();
modifyStudent.put("ID", id);
System.out.println("Enter student name: ");
myChoice = scanIn.nextLine().trim();
modifyStudent.put("name",myChoice);
//Enters grade of student
for(int x=0;x<getGradeSize();x++){
System.out.println("Enter grade no. ["+x+"]: ");
myChoice = scanIn.nextLine().trim();
modifyStudent.put(String.valueOf(x),myChoice);
}
myRecord.put(id, modifyStudent);
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Student Modified ::::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
}else{
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Record not found! :::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
editStudentRecord();
}
}
private boolean idExists(String id){
boolean exists = false;
for(String keys : myRecord.keySet()){
if(keys.equalsIgnoreCase(id)){
exists = true;
}
}
return exists;
}
public static void main(String[] args){
classrecord cr = new classrecord();
cr.startHere();
}
private int getGradeSize() {
return gradeSize;
}
private void setGradeSize(int gradeSize) {
this.gradeSize = gradeSize;
}
private int getStudentSize() {
return studentSize;
}
private void setStudentSize(int studentSize) {
this.studentSize = studentSize;
}
private void notifyCancelled(){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::: Transaction cancelled :::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
}
private void checkpoint(){
//Checks if na-init na ang record
if(getStudentSize()<=0){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Init Record First :::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
}
//Check if naa record...
if(myRecord.size()<=0){
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("::::: Record not found! :::::");
System.out.println(":::::::::::::::::::::::::::::");
System.out.println("!---------------------------!");
startHere();
}
}
}
Kani pud TS, try lang.. ![Cheesy](images/smilies/cheesy.gif)
Delete lang mga ::::::: kung ganahan ka bro..
kamo nalang edit sa pagka Console GUI niya.. ![Cheesy](images/smilies/cheesy.gif)
UPDATED