patternjavaModerate
Marijuana information program
Viewed 0 times
marijuanaprograminformation
Problem
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.
```
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:
Sy
It's pretty long and I'm sure some stuff can be removed.
Firstpackage 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:
Sy
Solution
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.
- Displaying a menu.
- Collecting user input.
- Constructing the object.
- Displaying confirmation output.
Three of those things don't belong and should be handled elsewhere.
Code Snippets
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;
}
}Strains(int type) {
ch = type;
}public enum StrainType {
BlueDream, SourDiesel, OGKush, GirlScoutCookies, GreenCrack,
PineappleExpress, GrandDaddyPurple
}Strains(StrainType type) {
ch = type;
}Context
StackExchange Code Review Q#100869, answer score: 17
Revisions (0)
No revisions yet.