I'm an old-time programmer from the 80s proficient with C and Pascal. I'm pretty new to Java and its concept. In trying to learn the new ways of Java using OOP techniques I'm rather confused about the right way of putting an application together.
I'm trying to build a basic program with the following rules.
An organisation has its own in-house library. The Staff class is general and has Name and Phone. Some of the employees is a librarian. The others are members. So Member and Librarian are specialised classes. I presume we call them inherited or extended in this context.
Now I'm trying to build basic functionality like input and print a member record. I'm trying to build a text-based menu. Perhaps later I'll consider sub-menus. For now it's just a simple menu.
I don't know if I should make the menu an object as well but that's what I think I'll do.
With this in mind, here's what I've done.
My main program:
public class Library extends Menus {
public static void main(String[] args) {
Menus Start = new Menus();
Start.ShowMainMenu();
}
}
StaffClass.java
public class StaffClass {
private String Name;
private String Phone;
public void InputData() {
Scanner UserInput = new Scanner(System.in);
System.out.print("Enter staff name "); Name = UserInput.nextLine();
System.out.print("Enter staff phone number "); Phone = UserInput.nextLine();
}
public void PrintData() {
System.out.println("Name : " + Name);
System.out.println("Phone : " + Phone);
}
}//end StaffClass
Menus.java
public class Menus extends MemberClass {
int c;
public void ShowMainMenu() {
Scanner ui = new Scanner(System.in);
while(1==1) {
System.out.println("Main menu");
System.out.println("1. Add student");
System.out.println("2. Display all");
System.out.println("3. exit");
System.out.print("Enter choice"); c = ui.nextInt();
switch(c) {
case 1 : getInputs(); /*System.out.println("option 1");*/ break;
case 2 : ShowAllInfo(); break;
case 3 : System.out.println("Leaving the program now..."); System.exit(0); break;
default : System.out.println("error.");
}
}
}
}
MemberClass.java
public class MemberClass extends StaffClass {
int TotalBooks;
public void getInputs() {
InputData();
UpdateTotalBooks();
}
public void ShowAllInfo() {
PrintData();
System.out.println("total books taken = " + TotalBooks);
}
public void UpdateTotalBooks() {
Scanner ui = new Scanner(System.in);
System.out.print("Enter number of books "); TotalBooks = ui.nextInt();
}
}
It's the first Java program that I've put together with so many classes in it + a menu system.
My key question is if this is the correct way of assembling a program in Java or is there another way.
Please feel free to suggest the best options/changes to the code. But I'd like to keep the concept of generalisation and specialisation.
Thanks!