Programming Language is Java
In class you learned how to work with collections and streams. This lab allows you to apply the skills you learned to a list of riders.
With your first two assignment complete, the coordinator at the Car Safety Academy decided to begin collecting names for a monthly mailing list. You've been asked to collect the names and perform some processing. First you will create a Riders class. Then you will create an array of riders with the names below (DO NOT SKIP THIS STEP!):
Full Name* | Address | City | State | Zip Code | Phone |
John Harbaugh | 8397 Zip Rd | Baltimore | MD | 21999 | 123-456-7890 |
Mike Tomlin | 147 LedStock Ave | Pittsburg | PA | 07001 | 890-123-4567 |
Kevin Stepanski | 34 Main St | Cleveland | OH | 07001 | 456-789-0123 |
Zac Taylor | 1222 Mover Rd | Cincinnati | OH | 90001 | 345-678-9012 |
Ron Rivera | 190 Princeton Ct | Washington | DC | 20001 | 678-901-2345 |
Do not divide name into first and last at any stage of this lab.
Next you will create and display a rider list.
Then you should display a menu to the user asking what he or she would like to do next based on the following options:
After displaying the menu, you should read in a number from the user. (You should check to ensure that the user enters a number 1-7.) Next you will perform the corresponding operation. (Operations 3,4, & 6 should be performed using a stream operations.)
Short Summary:
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
//This class holds the riders information
public class Riders {
//Instance variables
private String name;
private String address;
private String city;
private String state;
private String zipCode;
private String phone;
//Constructor to set values for instance variables
public Riders(String name, String address, String
city, String state, String zipCode, String phone) {
this.name = name;
this.address = address;
this.city = city;
this.state = state;
this.zipCode = zipCode;
this.phone = phone;
}
//Accessors
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public String getZipCode() {
return zipCode;
}
public String getPhone() {
return phone;
}
//Main method to test
public static void main(String args[])
{
//Hold the list of riders
ArrayList<Riders>
arrayOfRiders=new ArrayList<>();
arrayOfRiders.add(new Riders("John
Harbaugh", "8397 Zip Rd", "Baltimore", "MD", "21999",
"123-456-7890"));
arrayOfRiders.add(new Riders("Mike
Tomlin", "147 LedStock Ave", "Pittsburg", "PA", "07001",
"890-123-4567"));
arrayOfRiders.add(new Riders("Kevin
Stepanski", "34 Main St", "Cleveland", "OH", "07001",
"456-789-0123"));
arrayOfRiders.add(new Riders("Zac
Taylor", "1222 Mover Rd", "Cincinnati", "OH", "90001",
"345-678-9012"));
arrayOfRiders.add(new Riders("Ron
Rivera", "190 Princeton Ct", "Washington", "DC", "20001",
"678-901-2345"));
//display riders
displayRiders(arrayOfRiders);
//Get input from user
Scanner input=new
Scanner(System.in);
//Display menu
System.out.println("**********Menu***********");
System.out.println("1. Add a new
entry\n2. Remove an entry\n3. Sort by zip code\n4. Search by
state\n5. Search by full name\n"
+ "6. Print Riders\n7. Quit\nEnter your
choice:");
int choice=input.nextInt();
input.nextLine();
//Validate the input
while(choice<1 ||
choice>7)
{
System.out.println("Wrong choice! Please enter a number between 1
and 7");
choice=input.nextInt();
input.nextLine();
}
//Switch to perform the operations
based on user choice
switch(choice)
{
case 1:
//Get rider
details and add to list
System.out.print("Enter new Rider's full name:");
String
name=input.nextLine();
System.out.print("Enter address:");
String
address=input.nextLine();
System.out.print("Enter city:");
String
city=input.nextLine();
System.out.print("Enter state:");
String
state=input.nextLine();
System.out.print("Enter zip code:");
String
zip=input.nextLine();
System.out.print("Enter phone#:");
String
phone=input.nextLine();
Riders rider=new
Riders(name, address, city, state, zip, phone);
arrayOfRiders.add(rider);
System.out.println("Rider added successfully!");
displayRiders(arrayOfRiders);
break;
case 2:
//Remove by
rider name
System.out.print("Enter full name of the rider:");
String
fullName=input.nextLine();
Iterator
<Riders> iter=arrayOfRiders.iterator();
while(iter.hasNext())
{
Riders riderObj=iter.next();
if(riderObj.getName().equalsIgnoreCase(fullName))
iter.remove();
}
System.out.println("Rider removed successfully!");
displayRiders(arrayOfRiders);
break;
case 3:
//Sort by
zipcode
List<Riders> sortedList = arrayOfRiders.stream()
.sorted(Comparator.comparing(Riders::getZipCode))
.collect(Collectors.toList());
sortedList.forEach(System.out::println);
break;
case 4:
//Search by
state
System.out.print("Enter the state:");
String
enteredState=input.nextLine();
List<Riders> stateRidersList = arrayOfRiders.stream()
.filter(n ->
n.getState().equalsIgnoreCase(enteredState))
.collect(Collectors.toList());
stateRidersList.forEach(System.out::println);
break;
case 5:
//Get rider by
full name
System.out.print("Enter the full name:");
String
enteredName=input.nextLine();
List<Riders> nameRidersList = arrayOfRiders.stream()
.filter(n ->
n.getName().equalsIgnoreCase(enteredName))
.collect(Collectors.toList());
nameRidersList.forEach(System.out::println);
break;
case 6:
//display
riders
displayRiders(arrayOfRiders);
break;
case 7:
//Exit
System.exit(0);
break;
}
input.close();
}
@Override
public String toString() {
return "Rider [name=" + name + ",
address=" + address + ", city=" + city + ", state=" + state + ",
zipCode="
+ zipCode + ", phone=" + phone + "]";
}
//This method prints the list of riders
private static void displayRiders(List<Riders>
arrayOfRiders)
{
System.out.println("--------Displaying Riders list--------");
arrayOfRiders.forEach(s ->
System.out.println(s.toString()));
System.out.println();
}
}
Code Screenshot:
Output:
1. Add a Rider
2. Remove a rider by name
3. Sort by zipcode
4. Search by state
5. Search by Full Name
**************Please do upvote to appreciate our time. Thank you!******************
Get Answers For Free
Most questions answered within 1 hours.