Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this exercise I suggest you to use an enumeration for the crimes, so you can add other crimes that the application will recognize from the input of the user as explained by the code.</p> <pre><code>public enum Crime { ARSON{ public String toString(){ return "arson"; } }, ASSAULT{ public String toString(){ return "assault"; } }, THEFT{ public String toString(){ return "theft"; } }; public static String types(){ return ARSON + ", " + ASSAULT + ", " + THEFT; } } </code></pre> <p>I would not use the name Database for a criminal because it is misleading. Criminal or Outlaw is more appropriate.</p> <pre><code>public class Outlaw { private String name; private Crime crime; private int year; public Outlaw(String name, Crime crime, int year){ this.name=name; this.crime=crime; this.year=year; } public String getName() { return name; } public Crime getCrime() { return crime; } public int getYear() { return year; } @Override public String toString(){ return new String(name + " was convicted in the year " + year + " for " + crime + "."); } } public class InputData { private static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); List&lt;Outlaw&gt; criminals = inputData(); showCriminals(criminals); showCriminalsForCrime(criminals); scanner.close(); } public static List&lt;Outlaw&gt; inputData(){ List&lt;Outlaw&gt; criminals = new ArrayList&lt;Outlaw&gt;(); int numCriminals; System.out.print("How many criminals are you going to enter? "); numCriminals = scanner.nextInt(); scanner.nextLine(); for(int i = 0; i &lt; numCriminals; i++){ criminals.add(inputOutlaw()); } return criminals; } public static Outlaw inputOutlaw() { System.out.print("Enter criminal first and last name: "); String name = scanner.nextLine(); Crime crime = inputCrime(); System.out.print("Enter the year of conviction: "); int year = scanner.nextInt(); scanner.nextLine(); return new Outlaw(name, crime, year); } public static void showCriminalsForCrime(List&lt;Outlaw&gt; criminals){ System.out.println("Search the criminals for their crime"); Crime crime = inputCrime(); List&lt;Outlaw&gt; criminalsForCrime = getCriminalsFor(crime,criminals); if(!criminals.isEmpty()){ for(Outlaw criminal : criminalsForCrime){ System.out.println(criminal.toString()); } } else { System.out.println("There are not criminals for" + crime); } } public static Crime inputCrime(){ Crime crime = null; while(crime == null){ System.out.print("Which crime would you like to select? " + Crime.types() + ": "); try{ crime = Crime.valueOf(scanner.nextLine().toUpperCase().trim()); } catch(IllegalArgumentException e){ System.out.println("The crime is not in the application!"); } } return crime; } public static List&lt;Outlaw&gt; getCriminalsFor(Crime crime, List&lt;Outlaw&gt; criminals){ List&lt;Outlaw&gt; criminalsForCrime = new ArrayList&lt;Outlaw&gt;(); for(Outlaw criminal : criminals){ if(criminal.getCrime().equals(crime)){ criminalsForCrime.add(criminal); } } return criminalsForCrime; } public static void showCriminals(List&lt;Outlaw&gt; criminals){ System.out.println("CRIMINALS: "); for(Outlaw criminal : criminals){ System.out.println(criminal.toString()); } } } </code></pre> <p>Tell your teacher to explain better :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload