Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Assignment01Driver {
- Scanner input = new Scanner(System.in);
- public static void main(String[] args) {
- new Assignment01Driver();
- }
- // This will act as our program switchboard
- public Assignment01Driver() {
- String[] cargohold = new String[10];
- System.out.println("Welcome to the BlackStar Cargo Hold interface.");
- System.out.println("Please select a number from the options below");
- System.out.println("");
- while (true) {
- // Give the user a list of their options
- System.out.println("1: Add an item to the cargo hold.");
- System.out.println("2: Remove an item from the cargo hold.");
- System.out.println("3: Sort the contents of the cargo hold.");
- System.out.println("4: Search for an item.");
- System.out.println("5: Display the items in the cargo hold.");
- System.out.println("0: Exit the BlackStar Cargo Hold interface.");
- try{
- // Get the user input
- int userChoice = input.nextInt();
- input.nextLine();
- switch (userChoice) {
- case 1:
- addItem(cargohold);
- break;
- case 2:
- removeItem(cargohold);
- break;
- case 3:
- sortItems(cargohold);
- break;
- case 4:
- searchItems(cargohold);
- break;
- case 5:
- displayItems(cargohold);
- break;
- case 0:
- System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!");
- System.exit(0);
- default:
- System.err.println("Please select a valid option");
- continue;
- }
- }catch (InputMismatchException e){
- System.out.println("Please select an option 1-5 or 0 to exit the program");
- input.nextLine();
- }
- }
- }
- // TODO: Add an item that is specified by the user
- private void addItem(String cargohold[]) {
- System.out.print("Enter the name of the item: ");
- String item = input.nextLine();
- int index = -1;
- for(int i=0; i<cargohold.length;i++){
- if (cargohold[i]==(null)){
- index = i;
- break;
- }
- }
- if(index!=cargohold.length&&index != -1){
- cargohold[index]=item;
- System.out.println("You have added " + cargohold[index] + "\n");
- System.out.println("Press Enter to continue");
- input.nextLine();
- }else{
- System.out.println("No more items can be stored");
- System.out.println("Press Enter to continue");
- input.nextLine();
- }
- }
- // TODO: Remove an item that is specified by the user
- // removeItem() to remove an item from the array
- private void removeItem(String cargohold[]) {
- String item;
- int index = 0;
- // prompt the user for the item name to remove
- System.out.println("Enter item you would like to remove: ");
- item = input.nextLine();
- // search for the item
- for (int i = 0; i < cargohold.length; i++) {
- if (!cargohold[i].equals(null)) {
- if (cargohold[i].equalsIgnoreCase(item)) {
- index = i;
- break;
- }
- }
- }
- // if index is 0 display an error message
- if (index == -1) {
- System.out.println("Sorry, your item is not found in the list.\n");
- System.out.println("Press Enter to continue");
- input.nextLine();
- }else {
- // if item is found
- for (int i = index; i < cargohold.length - 1; i++) {
- cargohold[i] = cargohold[i];
- System.out.println("You have removed " + item + " from the hold\n");
- break;
- }
- System.out.println("Press Enter to continue");
- input.nextLine();
- }
- // TODO: Sort the items in the cargo hold (No need to display them here) - Use Selection or Insertion sorts
- // NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings
- }
- private void sortItems(String cargohold[]) {
- String sorting;
- for (int i = 0; i < cargohold.length; i++) {
- for (int j = i + 1; j < cargohold.length; j++) {
- if (cargohold[i] != null && cargohold[j] != null && cargohold[i].compareTo(cargohold[j]) > 0) {
- sorting = cargohold[i];
- cargohold[i] = cargohold[j];
- cargohold[j] = sorting;
- }
- }
- }
- System.out.println("Sort complete\nPress Enter to continue");
- input.nextLine();
- }
- // TODO: Search for a user specified item
- private void searchItems(String cargohold[]) {
- System.out.print("What Item would you like to search? ");
- boolean found = false;
- int counter = 0;
- String choice = input.nextLine();
- for (int i = 0; i < cargohold.length; i++) {
- if (cargohold[i] != null && cargohold[i].equals(choice)) {
- counter++;
- found = true;
- }
- }
- if (found) {
- System.out.print(choice + " - " + counter + "\n");
- }
- if (!found) {
- System.out.print("Item " + choice + " not found\n");
- }
- System.out.print("Press any key to continue");
- input.nextLine();
- }
- // TODO: Display only the unique items along with a count of any duplicates
- private void displayItems(String cargohold[]) {
- ArrayList replicatedData = new ArrayList();
- for (int i = 0; i < cargohold.length; i++) {
- boolean found = false;
- for(int k=0;k<replicatedData.size();k++){
- if(replicatedData.get(k).equals(cargohold[i])){
- found = true;
- }
- }
- if(!found){
- replicatedData.add(cargohold[i]);
- int count = 0;
- for(int j=0;j<cargohold.length;j++){
- if(cargohold[i].equals(cargohold[j])){
- count++;
- }
- }
- System.out.print(cargohold[i] + "-"+count+"\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement