Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Introduction To The Topic

Develop Program For


Medical Store Stock System
Using Java Language
 What Is Java Language ?
Java is a general-purpose, class-based, object-oriented
programming language designed for having lesser
implementation dependencies. It is a compiled language
and is executed by the Java Virtual Machine
(JVM). Java is one of the most popular programming
languages in use today, and is used to develop a wide
variety of applications, including web applications,
mobile applications, desktop applications, and
enterprise software.
Java is a platform-independent language, which means
that Java code can be run on any platform that has a
Java Virtual Machine (JVM). This makes Java a very
portable language, and is one of the reasons why it is
so popular.
Java is also a very secure language. Java bytecode is
verified by the JVM before it is executed, and this helps
to prevent security vulnerabilities. Java also has a
number of built-in security features, such as
sandboxing and access control.
 Here are some of the key features of Java:
 Object-oriented:
 Java is an object-oriented language, which means
that everything in Java is an object. This makes
Java code more modular and reusable.
 Platform-independent:
 Java code can be run on any platform that has a
Java Virtual Machine (JVM). This makes Java a
very portable language.
 Secure:
 Java is a very secure language. Java bytecode is
verified by the JVM before it is executed, and Java
has a number of built-in security features.
 Robust:
 Java is a very robust language. Java has a number
of features that help to prevent errors, such as
garbage collection and exception handling.
 Multithreaded:
 Java supports multithreading, which means that
multiple threads of execution can
runsimultaneously. This makes Java a good choice
for developing high-performance applications.
 What Is Inheritance In Java ?

Java, Inheritance is an important pillar of


OOP(Object-Oriented Programming). It is the
mechanism in Java by which one class is allowed
to inherit the features(fields and methods) of
another class. In Java, Inheritance means creating
new classes based on existing ones. A class that
inherits from another class can reuse the
methods and fields of that class. In addition, you
can add new fields and methods to your current
class as well.

Why Do We Need Java Inheritance?

 Code Reusability: The code written in the


Superclass is common to all subclasses.
Child classes can directly use the parent
class code.
 Method Overriding: Method Overriding is
achievable only through Inheritance. It is
one of the ways by which Java achieves Run
Time Polymorphism.
 Abstraction: The concept of abstract where
we do not have to provide all details is
achieved through
inheritance. Abstraction only shows the
functionality to the user.

Important Terminologies Used in Java


Inheritance

 Class: Class is a set of objects which shares


common characteristics/ behavior and
common properties/ attributes. Class is not
a real-world entity. It is just a template or
blueprint or prototype from which objects
are created.
 Super Class/Parent Class: The class whose
features are inherited is known as a
superclass(or a base class or a parent class).
 Sub Class/Child Class: The class that
inherits the other class is known as a
subclass(or a derived class, extended class,
or child class). The subclass can add its own
fields and methods in addition to the
superclass fields and methods.
 Reusability: Inheritance supports the
concept of “reusability”, i.e. when we want
to create a new class and there is already a
class that includes some of the code that
we want, we can derive our new class from
the existing class. By doing this, we are
reusing the fields and methods of the
existing class.

How to Use Inheritance in Java?

The extends keyword is used for inheritance in


Java. Using the extends keyword indicates you
are derived from an existing class. In other words,
“extends” refers to increased functionality.

Syntax :

class derived-class extends base-class


{
//methods and fields
}
Program :
package javafullcourse;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class MedicalStore {
private Map<String, Integer> inventory;

public MedicalStore() {
inventory = new HashMap<>();
}

public void addItem(String item, int quantity) {


inventory.put(item, quantity);
}

public void sellItem(String item, int quantity) {


if (inventory.containsKey(item)) {
int availableQuantity = inventory.get(item);
if (availableQuantity >= quantity) {
inventory.put(item, availableQuantity -
quantity);
System.out.println(quantity + " " + item +
"(s) sold successfully.");
} else {
System.out.println("Insufficient quantity
of " + item + " in stock.");
}
} else {
System.out.println(item + " not found in
inventory.");
}
}

public void displayInventory() {


System.out.println("Current Inventory:");
for (Map.Entry<String, Integer> entry :
inventory.entrySet()) {
System.out.println(entry.getKey() + ": " +
entry.getValue());
}
}
}

public class MedicalStoreSystem {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MedicalStore store = new MedicalStore();

while (true) {
System.out.println("\n1. Add Item to
Inventory");
System.out.println("2. Sell Item");
System.out.println("3. Display Inventory");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
character

switch (choice) {
case 1:
System.out.print("Enter item name: ");
String itemToAdd = scanner.nextLine();
System.out.print("Enter quantity: ");
int quantityToAdd = scanner.nextInt();
store.addItem(itemToAdd,
quantityToAdd);
break;
case 2:
System.out.print("Enter item name to
sell: ");
String itemToSell = scanner.nextLine();
System.out.print("Enter quantity to
sell: ");
int quantityToSell = scanner.nextInt();
store.sellItem(itemToSell,
quantityToSell);
break;
case 3:
store.displayInventory();
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice.
Please try again.");
}
}
}
}
Program Output :
Explanation Of Program :

The Car class inherits all of the fields and methods


of the Vehicle class, including
the brand and numWheels fields and
the start() method. The Car class also adds its own
field, numDoors, and overrides the start() method
to provide a more specific implementation.
Inheritance can be used to create hierarchies of
classes. For example, the Car class could be
further subclassed into Sedan, SUV,
and Truck classes. Each subclass could inherit the
fields and methods of the Car class and add its
own specific fields and methods.
Inheritance is a powerful feature that can be used
to create reusable and maintainable code. It is
one of the core concepts of object-oriented
programming and is essential for understanding
how Java works.
Conclusion :

We Successfully Develop Program


For
Medical Store Stock System
Using Java Language

You might also like