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!
You are overusing classes — classes are meant to represent data types, or be holders for static methods (which are essentially just functions). You however, are using classes as a means to distribute functions and then apply bogus inheritance. Also, you're inheriting from
Menus
and instatiatingMenus
in the same class — that makes no sense whatsoever.In other words, you don't need to use inheritance and a lot of classes just for the sake of it. There's nothing in Java other than a lot of incompetent developers who overuse classes that forces or recommends overuse of classes and inheritance.
Functions, preferrably ones with no side effects/global state, are much much easier to reason about and get right. Whereas complicated state variables scattered across a parent class chain is a shortcut to a big headache.
I would use something simple such as:
If you need to split this up in different modules, do NOT use inheritance. Instead, create something like a
State
orDatabase
class, and pass that around between static functions that either modify the passed in data or, if you want to peek into the world of functional programming and immutable data structures, return an altered copy of the data:And, as a final remark: all of this would look much nicer in a concise functional programming JVM language such as Scala (or even Frege). If you must stay on Java, at least read something up about Functional Programming and how you could benefit from it in the way you design your programs: