Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cruise_boarding_sys;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.BufferedReader;
- import java.io.FileReader;
- public class BoardingSys {
- //declaring scanner to take input
- static Scanner kb = new Scanner(System.in);
- public static void main(String[] args) throws Exception {
- //declaring cabin array
- String[] cabins = new String[12];
- while(true) {
- System.out.println("Enter option: "
- + "\nA - to add cabin crew, "
- + "\nV - to view all cabins,"
- + "\nE - print empty cabins,"
- + "\nD - delete customer from cabin,"
- + "\nF - find cabin using Customer name,"
- + "\nS - store program data into file,"
- + "\nL - load program data from the file,"
- + "\nO - view passengers in alphabetical order");
- char option = kb.next().charAt(0); // reads String from keyboard and save it into option
- System.out.println("You selected option: " + option);
- kb.nextLine(); //fixes issues with nextLine()
- //code for option A
- switch (option) {
- case 'A':
- cabins = addCustomer(cabins);
- break;
- case 'V':
- printCabins(cabins);
- break;
- case 'E':
- printEmptyCabins(cabins);
- break;
- case 'D':
- cabins = deleteCustomerFromCabin(cabins);
- break;
- case 'F':
- findCabinWithCusName(cabins);
- break;
- case 'S':
- storeDataInFile(cabins);
- break;
- case 'L':
- cabins = loadProgramData();
- break;
- case 'O':
- viewPassengerInOrder(cabins);
- break;
- default:
- System.out.println("Invalid option");
- break;
- }
- }
- }
- private static void viewPassengerInOrder(String[] cabins) {
- String[] cabinsSorted = new String[12];
- for(int i =0; i<cabins.length;i++) {
- if(!(cabins[i]== null)) {
- for(int j=i+1; j<cabins.length; j++) {
- //compares each elements of the array to all the remaining elements
- if(!(cabins[j]==null)) {
- if(cabins[i].compareTo(cabins[j])>0) {
- //swap elements with each other
- String temp = cabins[i];
- cabins[i] = cabins[j];
- cabins[j] = temp;
- }
- }
- }
- }
- }
- System.out.println("Viewing all cabins: ");
- for(int i=0;i<cabins.length;i++) {
- if(!(cabins[i] == null)) {
- System.out.println(cabins[i]);
- }
- }
- }
- private static String[] loadProgramData() throws Exception {
- System.out.println("Loading data from CabinData.txt");
- BufferedReader reader = new BufferedReader(new FileReader("CabinData.txt"));
- String[] cabins = new String[12];
- for (int i=0; i<cabins.length;i++) {
- String cabinCus = reader.readLine();
- //if cabinCus is string null, transform it to NULL type
- if(cabinCus.equals("null")) {
- cabins[i] = null;
- }else {
- cabins[i] = cabinCus;
- }
- }
- reader.close();
- return cabins;
- }
- private static void storeDataInFile(String[] cabins) throws Exception {
- System.out.println("Saving data in CabinData.txt");
- BufferedWriter writer = new BufferedWriter(new FileWriter("CabinData.txt", false));
- for(int i=0;i<cabins.length;i++) {
- String message = cabins[i];
- writer.write(message);
- writer.newLine();
- }
- writer.flush();
- System.out.println("Data successfully saved");
- }
- private static void findCabinWithCusName(String[] cabins) {
- System.out.println("Enter customer name:");
- String cusName = kb.nextLine();
- boolean found = false;
- for(int i=0; i<cabins.length;i++) {
- //check to see if its not null first so we can use .equals to check the customer name
- //Cannot invoke "String.equals(Object)" because "cabins[i]" is null
- if(!(cabins[i] == null)) {
- if(cabins[i].equals(cusName)) {
- System.out.println("Customer " + cusName + " is in cabin " + (i+1));
- found = true;
- break;
- }
- }
- }
- if(!found) {
- System.out.println("Customer " + cusName + " is not on the ship.");
- }
- }
- private static String[] deleteCustomerFromCabin(String[] cabins) {
- //delete customer by cabin index/id
- System.out.println("Please enter the index of the cabin you would like to empty: ");
- int cabinIndex = kb.nextInt() - 1;
- System.out.println("Customer "+ cabins[cabinIndex] +" has been removed from the cabin");
- cabins[cabinIndex] = null;
- return cabins;
- }
- private static void printEmptyCabins(String[] cabins) {
- System.out.println("Viewing all empty cabins: ");
- for(int i=0;i<cabins.length;i++) {
- if(cabins[i] == null) {
- System.out.println("Cabin number: " + (i+1) + " is empty.");
- }
- }
- }
- private static void printCabins(String[] cabins) {
- System.out.println("Viewing all cabins: ");
- for(int i=0;i<cabins.length;i++) {
- System.out.println("Cabin number: " + (i+1) + " is ocuppied by: " + cabins[i]);
- }
- }
- //add customer method that takes the old cabins array and returns the new cabins array
- private static String[] addCustomer(String[] cabins) {
- //check if customer is entered
- boolean entered = false;
- int cabinSpot = 0;
- String cusName = "";
- //iterate through cabin indexes in order to insert new cus
- for(int i=0;i<cabins.length;i++) {
- //check if cabin is empty
- if(cabins[i] == null) {
- //customer entered cabin
- entered = true;
- //ask for cus name
- System.out.println("Please enter customer name");
- //read cus name
- cusName = kb.nextLine();
- //save cus name into the cabin on the specific index
- cabins[i] = cusName;
- cabinSpot = i+1;
- break;
- }
- }
- if(!entered /* entered == false */) {
- System.out.println("There are no empty cabins");
- }else {
- System.out.println("Customer entered cabin " + cabinSpot + " and it's name is: " + cusName);
- }
- return cabins;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment