Skip to content

Instantly share code, notes, and snippets.

@yassaa
Created November 7, 2014 12:25
Show Gist options
  • Save yassaa/8af0dba39b7a05b5274d to your computer and use it in GitHub Desktop.
Save yassaa/8af0dba39b7a05b5274d to your computer and use it in GitHub Desktop.
mainCinemaBookingUserMovie
import java.time.*;
import java.util.*;
import java.lang.*;
import java.io.*;
public class MainMovie {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("\nEnter your Name");
String Name = input.nextLine();
System.out.println("\nEnter your email");
String Email = input.nextLine();
System.out.println("\nEnter your Phonenumber");
String Phonenumber = input.next();
//if (Phonenumber.length() != 8){
//System.err.print("The Phonenumber is not valid, please enter a valid phonenumber in the format DDDDDDDD\n");
//}
Movie[] availableMovies = {
new Movie (
"Die Hard 12 - THE SMACKDOWNENING",
new String[] {" 12.30 - 14.00", " 14.30 - 16.00", " 18.30 - 20.00"},
"1"),
new Movie (
"Fast and Furious 10",
new String[] {" 12.30 - 14.00", " 14.30 - 16.00", " 18.30 - 20.00"},
"2"),
new Movie (
"Zombievers 2",
new String[] {" 12.30 - 14.00", "2: 14.30 - 16.00", " 18.30 - 20.00"},
"3")
};
// brugeren skal vælge en film
// selectedmovie er den specifikke movie som brugeren vælger
Movie selectedMovie = promtUserMovieSelection(availableMovies, input);
System.out.println("\nYou have selected: " + selectedMovie.getmovieName() );
String selectedTime = promtUsertimeSelection(selectedMovie.gettime(), input);
System.out.println("\n You have selected the movie: " + selectedMovie.getmovieName() + "\n in the timeslot: " + selectedTime + " \n in cinema: " + selectedMovie.getCinema());
}
public static Movie promtUserMovieSelection(Movie[] movies, Scanner scanner) throws IOException {
System.out.println("\n Please choose a movie ");
// make an index of the movies you can choose from.
for(int i = 0; i < movies.length; i++) {
Movie m = movies[i];
System.out.println((i+1) + ": " + m.getmovieName());
}
System.out.println("");
System.out.print("\n Enter the number of the movie you like to watch: ");
// This loop will run until the user has selected a movie.
// while(true) will run until a "return" statement will stop it (so user HAS to give some input)
while(true) {
try { // try = code that causes problem / catch (exeption ) = code to handle the exeption
// reads userinput. Output from console is of the type String, nextInt will convert it to Int.
int i = scanner.nextInt();
// number the user chooses has to be between 1 and movies-length (3)
if(i < 1 || i > movies.length) {
// if it is not, errormessage will show
System.err.println("Number has to be between 1 and " + movies.length);
}
else {
// if it is between 1-3 tit will print out the chosen number
System.out.print(i);
return movies[i-1];
}
}
catch(InputMismatchException ime) { // This exception is thrown by the scanner upon calling .nextInt() if the entered value is not an integer
System.err.println("\n Please try again. It has to be a WHOLENUMBER");
}
}
}
public static String promtUsertimeSelection(String[] timeSlots, Scanner scanner) throws IOException {
System.out.println("\n Please choose a time ");
for(int i = 0; i < timeSlots.length; i++) {
String timeSlot = timeSlots[i];
System.out.println((i+1) + ": " + timeSlot);
}
System.out.println("");
System.out.print("\n Enter the number of the timeslot: ");
// This loop will run until the user has selected a movie.
while(true) {
try {
int i = scanner.nextInt();
// Number has to be between 1 and number of timeslots for the movie (timeslots.length)
if(i < 1 || i > timeSlots.length) {
System.err.println("Number has to be between 1 and " + timeSlots.length);
}
else {
System.out.print(i);
return timeSlots[i-1];
}
}
catch(InputMismatchException nfe){ // This exception is thrown by the scanner upon calling .nextInt() if the entered value is not an integer
System.err.println("Please try again. It has to be a WHOLENUMBER");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment