11
\$\begingroup\$

I made a simple marijuana information program in which I give the user a list of strains and give the user information about it, like: effects, medical, negatives, and its cannabis type (sativa-Indica).

It's pretty long and I'm sure some stuff can be removed.

First

package Marijuana;

import java.util.Scanner;
//SANCHIT SHARMA

public class First {

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


        System.out.println("Welcome to Marijuana info ");

        Strains strain = new Strains();


        strain.Type();
        strain.info();
        strain.Effects();


    }
}

Strains

package Marijuana;

import java.util.*;

public class Strains {

    String name;
    int ch;
    int ch1;
    Scanner sc = new Scanner(System.in);

    Strains() {

        System.out.println("Select a Strain.");
        System.out.println("1. Blue Dream");
        System.out.println("2. Sour Diesel");
        System.out.println("3. OG Kush");
        System.out.println("4. Girl Scout Cookies");
        System.out.println("5. Green Crack");
        System.out.println("6. Pineapple Express");
        System.out.println("7. GrandDaddy Purple");
        ch = sc.nextInt();

        switch (ch) {
        case 1:
            System.out.println("Thank you for your input! You chose Blue Dream");
            break;
        case 2:
            System.out.println("Thank you for your input! You chose Sour Diesel");
            break;
        case 3:
            System.out.println("Thank you for your input! You chose OG Kush");
            break;
        case 4:
            System.out.println("Thank you for your input! You chose Girl Scout Cookies");
            break;
        case 5:
            System.out.println("Thank you for your input! You chose Green Crack");
            break;
        case 6:
            System.out.println("Thank you for your input! You chose Pineapple Express");
            break;
        case 7:
            System.out.println("Thank you for your input! You chose GrandDaddy Purple");
            break;
        }
    }

    public void Type() {
        String type[] = new String[7];
        type[0] = "Sativa Dominant Hybrid.";
        type[1] = "Invigorating Sativa.";
        type[2] = "Remains a Debatable mystery.";
        type[3] = "OG Kush and Durban Poison hybrid. Type remains a mystery";
        type[4] = "75% indica variety of Green Crack is said to have come from an Afghani strain";
        type[5] = "The child of Trainwreck and Hawaiian. Type remains a mystery";
        type[6] = "GrandDaddy Purple is a famous indica cross between Purple Urkle and Big Bud";

        for (int i = 0; i < type.length; i++) {
            if (ch == 7) {
                System.out.println("What type of cannabis? " + type[6]);
                System.out.println();
                break;
            }
            if (i == ch) {
                System.out.println("What type of cannabis? " + type[i - 1]);
                System.out.println();
            }
        }
    }

    public void info() {
        String type[] = new String[7];
        type[0] = "Blue Dream balances full-body relaxation with gentle cerebral invigoration. Novice and veteran consumers alike enjoy the level effects of \nBlue Dream, which ease you gently into a calm euphoria.";
        type[1] = "This fast-acting strain delivers energizing, dreamy cerebral effects that have pushed Sour Diesel to its legendary status. \nStress, pain, and depression fade away in long-lasting relief that makes Sour Diesel a top choice among medical patients. ";
        type[2] = "It has an earthy pine scent with woody undertones, an aroma that has become the signature of OG Kush varieties and descendants. \nWith OG Kush, patients most commonly cite improvements in migraines, ADD/ADHD, and stress disorders.";
        type[3] = "Girl Scout Cookies launches you to euphoria’s top floor where full-body relaxation meets a time-bending cerebral space. \nA little goes a long way with this hybrid, whose THC heights have won Girl Scout Cookies numerous Cannabis Cup awards.";
        type[4] = "Few strains compare to Green Crack’s sharp energy and focus as it induces an invigorating mental buzz that keeps you going throughout the day. \nWith a tangy, fruity flavor redolent of mango, Green Crack is the perfect daytime medication for patients treating fatigue, stress, and depression.";
        type[5] = "The smell has been likened to fresh apple and mango, with a taste of pineapple, pine and cedar. \nThis hard-hitting sativa provides an energetic high up to 2 hours and has a THC content up to 17% with CBD of 0.17%. ";
        type[6] = "Its potent psychoactive effects are clearly detectable in both mind and body, delivering a fusion of cerebral euphoria and physical relaxation.\nGranddaddy Purple is typically pulled off the shelf to treat pain, stress, insomnia, appetite loss, and muscle spasms. ";

        for (int i = 0; i < type.length; i++) {
            if (ch == 7) {
                System.out.println(type[6]);
                break;
            }
            if (ch == i)
                System.out.println(type[i - 1]);
        }
        System.out.println();
    }

    public void Effects() {

        String BD = "Happy: 10 " + "Euphoric: 9 " + "Releaxed: 8.5 " + "Uplifted: 8 " + "Creative: 6.5 ";

        String BDM = "Stress: 10 " + "Depression: 7 " + "Pain:  6.75 ";

        String BDN = "Dry Mouth: 10 " + "Dry Eyes: 5 " + "Paranoid: 3.5 ";

        String SD = "Happy: 10 " + "Euphoric: 8.5 " + "Releaxed: 8 " + "Uplifted: 7.5 " + "Creative: 6 ";

        String SDM = "Stress: 10 " + "Depression: 7 " + "Pain:  6 ";

        String SDN = "Dry Mouth: 10 " + "Dry Eyes: 5.5 " + "Paranoid: 3 ";

        String OG = "Happy: 10 " + "Euphoric: 8 " + "Releaxed: 10 " + "Uplifted: 6 " + "Creative: 5 ";

        String OGM = "Stress: 10 " + "Depression: 6.5 " + "Pain:    6.75 ";

        String OGN = "Dry Mouth: 10 " + "Dry Eyes: 7 " + "Paranoid: 3 ";

        String GSC = "Happy: 10 " + "Euphoric: 9 " + "Releaxed: 8.5 " + "Uplifted: 6 " + "Creative: 5 ";

        String GSCM = "Stress: 10 " + "Depression: 7 " + "Pain: 6.75 ";

        String GSCN = "Dry Mouth: 10 " + "Dry Eyes: 5 " + "Paranoid: 2 ";

        String GC = "Happy: 9.5 " + "Euphoric: 7 " + "Energetic: 10 " + "Uplifted: 8.5 " + "Creative: 6 ";

        String GCM = "Stress: 10 " + "Depression: 8 " + "Pain:  6 ";

        String GCN = "Dry Mouth: 10 " + "Dry Eyes: 5 " + "Paranoid: 3.5 ";

        String PE = "Happy: 10 " + "Euphoric: 8 " + "Releaxed: 7" + "Uplifted: 7 " + "Creative: 6.5 ";

        String PEM = "Stress: 10" + "Depression: 7" + "Pain:    6";

        String PEN = "Dry Mouth: 10 " + "Dry Eyes: 5.5 " + "Paranoid: 2.5 ";

        String GDP = "Happy: 7" + "Euphoric: 6.5" + "Releaxed: 10" + "Hungry: 5 " + "Sleepy: 6.5 ";

        String GDPM = "Stress: 10 " + "Depression: 6 " + "Pain: 9 ";

        String GDPN = "Dry Mouth: 10 " + "Dry Eyes: 5.5 " + "Paranoid: 3";

        System.out.println("What Would You like to know");
        System.out.println("1. Effects");
        System.out.println("2. Medical attributes");
        System.out.println("3. Negatives");
        System.out.println("4. Everything altogether");
        ch1 = sc.nextInt();

        switch (ch1) {
        case 1:
            if (ch == 1) {
                System.out.println(BD);
            }
            if (ch == 2) {
                System.out.println(SD);
            }
            if (ch == 3) {
                System.out.println(OG);
            }
            if (ch == 4) {
                System.out.println(GSC);
            }
            if (ch == 5) {
                System.out.println(GC);
            }
            if (ch == 6) {
                System.out.println(PE);
            }
            if (ch == 7) {
                System.out.println(GDP);
            }
            break;

        case 2:
            if (ch == 1) {
                System.out.println(BDM);
            }
            if (ch == 2) {
                System.out.println(SDM);
            }
            if (ch == 3) {
                System.out.println(OGM);
            }
            if (ch == 4) {
                System.out.println(GSCM);
            }
            if (ch == 5) {
                System.out.println(GCM);
            }
            if (ch == 6) {
                System.out.println(PEM);
            }
            if (ch == 7) {
                System.out.println(GDPM);
            }
            break;

        case 3:
            if (ch == 1) {
                System.out.println(BDN);
            }
            if (ch == 2) {
                System.out.println(SDN);
            }
            if (ch == 3) {
                System.out.println(OGN);
            }
            if (ch == 4) {
                System.out.println(GSCN);
            }
            if (ch == 5) {
                System.out.println(GCN);
            }
            if (ch == 6) {
                System.out.println(PEN);
            }
            if (ch == 7) {
                System.out.println(GDPN);
            }
            break;
        case 4:
            if (ch == 1) {
                System.out.println(BD);
                System.out.println(BDM);
                System.out.println(BDN);
            }
            if (ch == 2) {
                System.out.println(SD);
                System.out.println(SDM);
                System.out.println(SDN);
            }
            if (ch == 3) {
                System.out.println(OG);
                System.out.println(OGM);
                System.out.println(OGN);
            }
            if (ch == 4) {
                System.out.println(GSC);
                System.out.println(GSCM);
                System.out.println(GSCN);
            }
            if (ch == 5) {
                System.out.println(GC);
                System.out.println(GCM);
                System.out.println(GCN);
            }
            if (ch == 6) {
                System.out.println(PE);
                System.out.println(PEM);
                System.out.println(PEN);
            }
            if (ch == 7) {
                System.out.println(GDP);
                System.out.println(GDPM);
                System.out.println(GDPN);
                break;
            }
        }
    }
}
\$\endgroup\$
1
  • 33
    \$\begingroup\$ Dude... Like... Use a data store maannnn. Like, you should totally load the data from a database or xml file at runtime cuz. \$\endgroup\$ – RubberDuck Aug 13 '15 at 23:46
17
\$\begingroup\$
Strains() {

    System.out.println("Select a Strain.");
    System.out.println("1. Blue Dream");
    System.out.println("2. Sour Diesel");
    System.out.println("3. OG Kush");
    System.out.println("4. Girl Scout Cookies");
    System.out.println("5. Green Crack");
    System.out.println("6. Pineapple Express");
    System.out.println("7. GrandDaddy Purple");
    ch = sc.nextInt();

    switch (ch) {
    case 1:
        System.out.println("Thank you for your input! You chose Blue Dream");
        break;
    case 2:
        System.out.println("Thank you for your input! You chose Sour Diesel");
        break;
    case 3:
        System.out.println("Thank you for your input! You chose OG Kush");
        break;
    case 4:
        System.out.println("Thank you for your input! You chose Girl Scout Cookies");
        break;
    case 5:
        System.out.println("Thank you for your input! You chose Green Crack");
        break;
    case 6:
        System.out.println("Thank you for your input! You chose Pineapple Express");
        break;
    case 7:
        System.out.println("Thank you for your input! You chose GrandDaddy Purple");
        break;
    }
}

I was confused by this for a while. I was wondering... "Is it even legal Java to not have a return type for a method?" And then I finally figured out... this is a constructor.

But this isn't even remotely how constructors should work. Unquestionably, a constructor should not to IO.

More appropriately, this constructor should look like this:

Strains(int type) {
    ch = type;
}

And that's it.

Though, I'd go one-step farther here and say that rather than an integer, we should have an enum to represent the strain types:

public enum StrainType {
    BlueDream, SourDiesel, OGKush, GirlScoutCookies, GreenCrack,
    PineappleExpress, GrandDaddyPurple
}

And that'd make our constructor look like this:

Strains(StrainType type) {
    ch = type;
}

Of course, ch is a terrible name that needs improvement, and we'd have to change its type from int to this StrainType enum, but you get the idea.

Anyway, this is all the responsibility our constructor should do. It's just a method. It's a special method, but it's still just a method. And methods should have a single responsibility.

Your version has 4 responsibilities.

  1. Displaying a menu.
  2. Collecting user input.
  3. Constructing the object.
  4. Displaying confirmation output.

Three of those things don't belong and should be handled elsewhere.

\$\endgroup\$
0
13
\$\begingroup\$
for (int i = 0; i < type.length; i++) {
    if (ch == 7) {
        System.out.println("What type of cannabis? " + type[6]);
        System.out.println();
        break;
    }
    if (i == ch) {
        System.out.println("What type of cannabis? " + type[i - 1]);
        System.out.println();
    }
}

This loop is completely confusing. And as it turns out, it's entirely unnecessary.

If we look closely, we'll find out that all that's actually happening here is...

System.out.println("What type of cannabis? " + type[ch - 1]);

That's all. Don't overthink your problems. Write the simplest solution. The only time you should loop is when you need to do something multiple times or possibly when you need to compare the actual values in the indices. If all you need is the value at a specific index, which you know ahead of time, then just grab the value at that index without looping.

\$\endgroup\$
8
\$\begingroup\$

Everything is a String

Welcome to Java and object-oriented programming! You are doing something really strange in Java in your Effects() method (which really should be called effects() or getEffects()):

String BD = "Happy: 10 " + "Euphoric: 9 " + "Releaxed: 8.5 " + "Uplifted: 8 " + "Creative: 6.5 ";
String BDM = "Stress: 10 " + "Depression: 7 " + "Pain:  6.75 ";
String BDN = "Dry Mouth: 10 " + "Dry Eyes: 5 " + "Paranoid: 3.5 ";

These can otherwise be represented as properties or fields of a Java class, if you choose to model each strain as an object. If you ever decide to add new effects/medicinal properties/negatives in the future, you'll have to keep updating these Strings. If you choose to model them as classes and enums, it makes it easier to add or remove these properties for each strain.

\$\endgroup\$
1
6
\$\begingroup\$

I would take a completely different approach: data-driven programming:

 with open(input("Which type of maryiuana are you interested in? ") + ".txt") as f:
     print(f.read())

(Python but should be easy to translate into Java).

Now the medical team just has to update text files, not code.

As a next iteration you may want to structure your file like:

Effects
... Content ...
***
Medical attributes
...Content...
***
Negatives
...Content...
***

You may then split at '***', ask the user the paragraph he wants and show only that.

\$\endgroup\$
2
  • \$\begingroup\$ Why should you invent your own data-format, when there are plenty reliable Parses of different formats for Java? Some included in the standard JRE-Libraries... \$\endgroup\$ – Falco Aug 14 '15 at 13:08
  • \$\begingroup\$ @Falco I wanted to keep it simple, more advanced parsers may be used as need arises. \$\endgroup\$ – Caridorc Aug 14 '15 at 13:09
4
\$\begingroup\$

Represent data which belongs together in one group! - This is one great thing one can do with Structs/Objects/Enums - you can have an Object with multiple attributes - so everything which belongs to one logical Object can also reside in a single place.

You have littered all information regarding a single Type all over the place - if I want to add a new Type or delete one, I have to edit 10 different spots somewhere in this program. If you want to make an exercise out of it you should put the data outside of your program in an XML, JSON or Properties-File.

But for a small program like this and some data I would probably solve it with ENUMS like this:

public enum Strain
{
    BlueDream
    ( "Blue Dream",
      "Sativa Dominant Hybrid.",
      "Blue Dream balances full-body relaxation "
      + "with gentle cerebral invigoration...",
      "Happy: 10 Euphoric: 9 Releaxed: 8.5 ",
      "Stress: 10 Depression: 7 Pain:  6.75 ",
      "Dry Mouth: 10 Dry Eyes: 5 Paranoid: 3.5 "
    ),

    MarleysChoice
    ( "Marleys Choice",
      "Extreme wonderful spices.",
      "Marleys Choice lets your head explode.",
      "Happy: 10 Euphoric: 9 Releaxed: 10 ",
      "Stress: 3 Depression: 5 Pain:  2 ",
      "Dry Mouth: 3 Dry Eyes: 2 Paranoid: 6 "
    );

    public final String name;
    public final String type;
    public final String description;
    public final String effectBasic;
    public final String effectMedical;
    public final String effectNegative;

    Strain( String name, String type, String description,
            String effectBasic, String effectMedical, String effectNegative )
    {
        this.name           = name;
        this.type           = type;
        this.description    = description;
        this.effectBasic    = effectBasic;
        this.effectMedical  = effectMedical;
        this.effectNegative = effectNegative ;
    }
}

You can then easily loop over all Enums, get their ordinals to access them and so forth. Your main program will shrink to about 15 lines of code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.