The program keeps track of the cars which have been sold from a showroom. The system should allow him to
Car details include the chassis number, make, model, engine_capacity and year of registration. The National ID number of the buyer is also stored along with the details of the car. All these details are kept in a single string, whereby each component is separated by '!'. The system should allow indexing of cars based on their registration number (which is a string). For example if a car is sold with the following details:
Registration No: : 1910JN2011 Chassis number : 23432-423 Make : BMW Model : X5 Engine Capacity : 3000 cc Year Registration : 2011 Buyer ID : S012345678910
A key-value pair in the map would be: "1910JN2011" -> 23432-423!BMW!X5!3000!2011!S012345678910”
Here is what I have worked out :
public class dsa_qu2c {
//Function display(String G) takes as input the string and displays the attributes
public static void display(String G){
String Details[]= G.split("!");
//split() allows splitting of a string based "!" and returns an array of strings
System.out.println("Chassis Number:\t"+Details[0]);
System.out.println("Make:\t"+Details[1]);
System.out.println("Model:\t"+Details[2]);
System.out.println("Engine Capacity:\t"+Details[3]);
System.out.println("Year of Registration:\t"+Details[4]);
System.out.println("Buyer NIC:\t"+Details[5]);
}
public static int menu(){
Scanner sc= new Scanner(System.in);
int choice;
System.out.println("1:Add a car.");
System.out.println("2:Display car based on registration number");
System.out.println("3:Display all details of car");
choice=sc.nextInt();
sc.nextLine();
return choice;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String S= ""; //Compiled string to be stored as value in the hashmap
String R= ""; //Registration number to be stored as key in the hashmap
Scanner sc= new Scanner(System.in);
int choice;
HashMap<String, String> HMap= new HashMap<String, String>();
while(true){
choice= menu();
switch(choice)
{
case 1 : {
System.out.println("Enter registration number");
R= sc.nextLine();
System.out.println("Enter compiled string");
S= sc.nextLine();
HMap.put(R,S);
break;
}
case 2: {
System.out.println("Enter registration number");
R= sc.nextLine();
String value= HMap.get(R);
display(value);
break;
}
case 3: {
for(Map.Entry entry: HMap.entrySet()){
display((String)entry.getValue());
break;
}
}
case 4: {
System.exit(0);
}
default: {
break;
}
}
}
}
}
Only Case 3
where the system has to retrieve data for all the cars is not working as nothing is being displayed when I choose that option.
You should remove break; from case 3:
case 3: {
for(Map.Entry entry: HMap.entrySet()){
display((String)entry.getValue());
//break;
}
}