Logo

dev-resources.site

for different kinds of informations.

Creating a java library, a place where any problem can be resolved.

Published at
3/21/2022
Categories
java
develop
jvm
jar
Author
to_code
Categories
4 categories in total
java
open
develop
open
jvm
open
jar
open
Author
7 person written this
to_code
open
Creating a java library, a place where any problem can be resolved.

Starting with the next class, called SummerDelivery, a delivery service planned to refresh a hot summer day...

public class SummerDelivery {

    public static void main(String[] args) {
        for (String order: args) {
            switch(order) {
                case "PEACH_TENTATION":
                    System.out.println("Peach Tentation Order...");
                    //Anyone knows how to prepared it?
                    break;
                case "FROZEN_APPLE":
                    System.out.println("Frozen Apple Order...");
                    //Anyone knows how to prepared it?
                    break;
                default:
                    System.out.println("Unexpected Order...");
                    break;
            }
        }   
    }
}
Enter fullscreen mode Exit fullscreen mode

But if we running the command to order a PEACH_TENTATION or a FROZEN_APPLE:

➜ java .\SummerDelivery.java PEACH_TENTATION FROZEN_APPLE
#it prints
Peach Tentation Order...
Frozen Apple Order...
Enter fullscreen mode Exit fullscreen mode

The program doesn’t know how to prepare one of its offered beverages, it needs a source with beverages preparation knowledge.

So, having the source below, capable of resolving SummerDelivered needs, called TropicalJuice:

public class TropicalJuice {

    class Storage {
        public static final String PEACH_JUICE = "Peach juice";
        public static final String APPLE_JUICE = "Apple juice";
        public static final String LIMON_JUICE = "Limon juice";     
    }

    class Fridge {
        public static final String ICE_CUBES = "Ice cubes";
        public static final String CHERRY = "Cherry";
        public static final String LIMON_SHOT = "Limon shot";       
    }

    public class JuiceMan {
        public void preparePeachTentation() {
            System.out.println(String.format("Mixing a 250ml of %s with a %s and a few crushed %s.", 
                Storage.PEACH_JUICE, Fridge.LIMON_SHOT, Fridge.ICE_CUBES));
        }

        public void prepareFrozzenApple() {
            System.out.println(String.format("Mixing a 250ml of %s with 2 parts of crushed %s adding 100ml of %s.", 
                Storage.APPLE_JUICE, Fridge.ICE_CUBES, Storage.LIMON_JUICE));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

It's necessary to include it inside SummerDelivery class to attend juice orders.

First of all, we compile TropicalJuice to a class file, executing the command below:

➜ javac -d . .\TropicalJuice.java
#it generates TropicalJuice.class,TropicalJuice$Fridge.class,TropicalJuice$JuiceMan.class,TropicalJuice$Storage.class
Enter fullscreen mode Exit fullscreen mode

After, we package their .class files generated in a storage called tropical.jar, our java library, running the command below:

➜ jar --create --file tropical.jar .\TropicalJuice.class '.\TropicalJuice$Fridge.class' '.\TropicalJuice$JuiceMan.class' '.\TropicalJuice$Storage.class'
#it generates a jar called tropical.jar
Enter fullscreen mode Exit fullscreen mode

With the created tropical.jar, we add to SummerDeliver class the library new methods.

public class SummerDelivery {
    private static TropicalJuice tropicalJuice = new TropicalJuice();
    private static TropicalJuice.JuiceMan juiceMan = tropicalJuice.new JuiceMan();

    public static void main(String[] args) {
        for (String order: args) {
            switch(order) {
                case "PEACH_TENTATION":
                    System.out.println("Peach Tentation Order...");
                    juiceMan.preparePeachTentation();
                    break;
                case "FROZZEN_APPLE":
                    System.out.println("Frozen Apple Order...");
                    juiceMan.prepareFrozzenApple();
                    break;
                default:
                    System.out.println("Unexpected Order...");
                    break;
            }
        }   
    }
}
Enter fullscreen mode Exit fullscreen mode

Adding the library in the first executed command:

➜ java -cp .\tropical.jar .\SummerDelivery.java FROZZEN_APPLE PEACH_TENTATION WATER
#it prints
Frozen Apple Order...
Mixing a 250ml of Apple juice with 2 parts of crushed Ice cubes adding 100ml of Limon juice.
Peach Tentation Order...
Mixing a 250ml of Peach juice with a Limon shot and a few crushed Ice cubes.
Unexpected Order...
Enter fullscreen mode Exit fullscreen mode

Yep! Now SummerDelivery knows how to refresh this summer! But it still doesn’t know how to serve a Water glass. If you know how to resolve it, please feel free to add it and test it to practice.

That’s all!

  1. A problem happened.
  2. We create a solution to solve it.
  3. Package the solution as a library.
  4. Add it and test it.
  5. Problem fixed.

Command and options dictionary:

  • java run .class and .java files.
  • javaccompile .java files and creates .class files.
  • -dindicate output folder.
  • -cpindicate compiled folders directory(classpath).
  • jar to execute actions related to java libraries.
  • —create indicate jar action command.
  • —file indicates a name file.

Tech Stack:

  • Java 11.
  • Windows 10.

Repo:

develop Article's
30 articles in total
Favicon
AWS CLI: Instalación en Windows y Linux, y Uso Básico
Favicon
C Development with GNU Emacs
Favicon
Creating a Stunning WordPress Site from Scratch
Favicon
How to Develop a Game Like Garena Free Fire in Scratch?
Favicon
The 4 Essential Skills of the Software Developers
Favicon
Cracking Your Technical Interview with LeetCode: A Step-by-Step Guide
Favicon
Salesforce Developer : Learning the course
Favicon
Most useful chrome extensions for DEVELOPERS
Favicon
🐳 Announcing the Docker x Hacktoberfest 2022 Winners
Favicon
Business Needs Custom Software For
Favicon
How to get Paid as Web3.0 Developer
Favicon
CARREIRA DEV: por onde começar?
Favicon
How Chatbots Are Revolutionizing The Way Businesses Interact With Customers
Favicon
Understanding Flutter Pageview Widget(Making Instagram reels screen)
Favicon
Programmer's life
Favicon
🙊 What do the Developer Advocates do? - Dev Advocate Journal (#DAJ) Day 1
Favicon
Resizing of Roles in IT
Favicon
Creating charts with the Aha! Develop API and extensions
Favicon
Why do I like to learn?
Favicon
10 Trending & Different Types of Software Development
Favicon
Common Ninja Platform News: Payments | New E-Commerce APIs
Favicon
14 Exemplos de código limpo e encurtamento de código Javascript
Favicon
50 Integrações de API mais populares
Favicon
Creating a java library, a place where any problem can be resolved.
Favicon
Why Do Businesses Need To Hire Application Maintenance & Support Services?
Favicon
SoC - Separation of Concerns
Favicon
The top advantages of software developer jobs.
Favicon
Goodbye Adobe! Or: why we are leaving the Adobe product family!
Favicon
5 Online Websites To Help You Learn Web Development
Favicon
Developed an app to transcribe and translate from images

Featured ones: