Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 the cabins array
- Cabin[] cabins = new Cabin[12];
- for(int i=0;i<cabins.length;i++) {
- cabins[i] = new Cabin();
- }
- 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(Cabin[] cabins) {
- System.out.println("Viewing all empty cabins: ");
- for(int i=0;i<cabins.length;i++) {
- Passenger[] passengers = cabins[i].getPassengers();
- for(int j=0;j<passengers.length;j++) {
- if(passengers[j] != null) {
- break;
- }
- if(j == passengers.length-1) {
- System.out.println("Cabin number: " + (i+1) + " is empty.");
- }
- //magic numbers; works if there are few passengers as it doesn't require much code to write
- //// if(passengers[0] == null && passengers[1] == null && passengers[2] == null) {
- // System.out.println("Cabin number: " + (i+1) + " is empty.");
- }
- }
- }
- private static void printCabins(Cabin[] cabins) {
- System.out.println("Viewing all cabins: ");
- for(int i=0;i<cabins.length;i++) {
- Passenger[] passengers = cabins[i].getPassengers();
- System.out.println("Passengers in cabin: " + (i+1));
- for(int j=0; j<passengers.length;j++) {
- if(!(passengers[j] == null)) {
- System.out.print(passengers[j].toString());
- System.out.println();
- }
- }
- }
- }
- //add customer method that takes the old cabins array and returns the new cabins array
- private static Cabin[] addCustomer(Cabin[] cabins) {
- boolean entered = false;
- for(int i=0;i<cabins.length;i++) {
- Passenger[] passengers = cabins[i].getPassengers();
- for(int j=0;j<passengers.length;j++) {
- if(passengers[j] == null) {
- Passenger p = new Passenger();
- //read from keyboard passenger details
- System.out.println("Insert First Name: ");
- p.setFirstName(kb.nextLine());
- System.out.println("Insert Sur Name: ");
- p.setSurName(kb.nextLine());
- System.out.println("Inser Expenses: ");
- //thorws error in case the value is not Double/Long
- p.setExpenses(kb.nextDouble());
- //add passenger to array of first cabin
- passengers[j] = p;
- entered = true;
- System.out.println("Passenger "+ p.getFirstName() +" is added to the cabin: " + (i+1));
- break;
- }
- }
- if(entered) {
- break;
- }
- }
- if(!entered) {
- System.out.println("No available places");
- }
- return cabins;
- }
- }
- public class Cabin {
- private Passenger[] passengers = new Passenger[3];
- public Cabin (Passenger[] passengers) {
- this.passengers = passengers;
- }
- public Cabin() {
- // this.passengers = null;
- }
- public Passenger[] getPassengers() {
- return passengers;
- }
- public void setPassengers(Passenger[] passengers) {
- this.passengers = passengers;
- }
- }
- public class Passenger {
- private String firstName, surName;
- private double expenses;
- public Passenger(String firstName, String surName, double expenses) {
- this.firstName = firstName;
- this.surName = surName;
- this.expenses = expenses;
- }
- public Passenger() {
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getSurName() {
- return surName;
- }
- public void setSurName(String surName) {
- this.surName = surName;
- }
- public double getExpenses() {
- return expenses;
- }
- public void setExpenses(double expenses) {
- this.expenses = expenses;
- }
- @Override
- public String toString() {
- return "Passenger " + firstName + " " + surName + ", with expenses of: " + expenses;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment