The following code snippet is a very basic example of how something like this could be designed – it should give you a basic idea of how it could be expanded, to add more details to the cases or maybe add some other methods such as only viewing a specific case carrying a certain name or deleting a case which has been added.
Adding verification to the user input or storing the date in an Object which is better suited to it such as java.time
might be first steps.
The java.util.ArrayList
is likely the best choice for storing cases.
import java.util.ArrayList;
import java.util.Scanner;
public class CriminalCase {
//properties & getters
private String name;
public String getName(){return name;}
private String date;
public String getDate(){return date;}
//constructor
public CriminalCase(String name, String date){
this.name = name;
this.date = date;
}
public static void main(String[] args) {
//this ArrayList will be used to store the cases
ArrayList<CriminalCase> cases = new ArrayList<>();
boolean quit = false;
Scanner s = new Scanner(System.in);
while (!quit) {
System.out.println("To view current cases enter v\nto add a case enter a\nto quit enter q");
String input = s.nextLine();
switch(input){
case ("v"): {
System.out.println("The following cases exist:");
for (CriminalCase c : cases)
System.out.println("Name: " + c.getName() + " Date: " + c.getDate());
break;
}
case("a"):{
System.out.println("Enter a name:");
String name = s.nextLine();
System.out.println("Enter a date (e.g. 17.09.2015)");
String date = s.nextLine();
cases.add(new CriminalCase(name,date));
break;
}
case("q"):{
quit = true;
break;
}
/*
case("d"):{
//method to delete a case
}
*/
}
}
}
}
1
solved Creating a program to file criminal cases and then view them, with new case being allowed to add