i need help seperate this main method to a class - java

I have this movie ticket program, which I want to have a class so it becomes a main method and a class and it's aching my mind .
import java.util.Scanner;
public class MovieTicket {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] movielist ={"1) Shutter Island","2) Devil's Advocate","3) Pulp Fiction","4) Resvouar Dogs"};
Ticket obj=new Ticket();
System.out.println("Welcome to Vox Cinemas");
System.out.println("Please ,Selcet a movie please");
for (int i =0; i<movielist.length;i++){
System.out.println(movielist[i]);
}
int number = sc.nextInt();
System.out.println("The movie you selected is:"+" "+movielist[number-1]);
System.out.println("How many seats would you lie:");
String seats = sc.next();
System.out.println("You've selected:"+seats+"Seats");
}
}

You need to create separate classes for Theatre, Movie, Ticket etc.
Something like below.
class Movie{
private String name;
private int runtime;
//add other fields like producer/actors/genre
public Movie(String name, int runtime) {
this.name = name;
this.runtime = runtime;
}
public String getName() {
return name;
}
public int getRuntime() {
return runtime;
}
}
class Ticket{
private int number;
private Movie movie;
private String screen; //Screen 1
private String seat; //A14
public Ticket(int number, Movie movie, String screen, String seat) {
//.. initiate variables here.
}
//write getters here
}
class VoxCinema{
private List<Movie> movies = new ArrayList<>();
private String address;
//theater details like name/timings etc.
public VoxCinema() {
}
public void addMovie(Movie movie) {
this.movies.add(movie);
}
public List<Movie> getAllMovies() {
return this.movies;
}
//write getters and setters
}
Then use them in your application like this:
public class TicketBookingApplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
VoxCinema vc = new VoxCinema();
vc.addMovie(new Movie("Shutter Island", 130));
vc.addMovie(new Movie("Devil's Advocate", 180));
/// add all movies
System.out.println("Welcome to Vox Cinemas");
System.out.println("Please ,Selcet a movie please");
List<Movie> movies = vc.getAllMovies();
for (int i = 0; i < movies.size(); i++) {
System.out.println(i + ") " + movies.get(i).getName());
}
int number = sc.nextInt();
Movie selectedMovie = movies.get(number);
System.out.println("The movie you selected is: " + selectedMovie.getName());
System.out.println("How many seats would you lie:");
String seats = sc.next();
System.out.println("You've selected:" + seats + "Seats");
// create tickets
Ticket t = new Ticket(3341, selectedMovie, "Screen 4", "D15");
//store tickets in VoxCinema object
}
}
Implement remaining parts or modify code as per your needs.

Related

What is the relationship between two classes they are tied to a Third class?

Getting stuck with my Travail System Project, Confusing a little bit about understanding that if there Classes called Bookable, Hotel and BookingSystem.
Hotel class is implements Bookable. Furthermore, BookingSystem Class is composition from Bookable, So, I need to create method at BookingSystem class which called addHotel.
what I must do about it to make a relationship between Hotel Class and BookingSystem Class.
Thanks In Advance.
Israa
Hotal Class:
public class Hotel implements Bookable {
private String name, location;
private int noOfRooms;
private double roomPrice;
private Date bookingDate;
private ArrayList<Integer> bookedRooms = new ArrayList<Integer>();
private ArrayList<Integer> numberOfrooms = new ArrayList<Integer>();
public Hotel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getNoOfRooms() {
return noOfRooms;
}
public void setNoOfRooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
}
public double getRoomPrice() {
return roomPrice;
}
public void setRoomPrice(double roomPrice) {
this.roomPrice = roomPrice;
}
public Date getBookingDate() {
return bookingDate;
}
public void setBookingDate(Date bookingDate) {
this.bookingDate = bookingDate;
}
public ArrayList<Integer> getBookedRooms() {
return bookedRooms;
}
public void setBookedRooms(ArrayList<Integer> bookedRooms) {
this.bookedRooms = bookedRooms;
}
public String Book() {
if ( numberOfrooms.size() != (bookedRooms.size())) {
for (int i = 0; i < bookedRooms.size(); i++) {
int oldVal = bookedRooms.get(i);
int newVal = oldVal + 1;
bookedRooms.add(bookedRooms.set(i, newVal));
}
}
return null;
}
}
Bookable class:
public interface Bookable {
public String Book();
}
BookingSytsem Class:
public class BookingSystem {
private ArrayList<Customer> customer = new ArrayList<Customer>();
private ArrayList<Bookable> bookable = new ArrayList<Bookable>();
private ArrayList<Operation> operation = new ArrayList<Operation>();
public BookingSystem() {
}
// **
public void addCustomer(String name, int id) {
Customer customers = new Customer(id, name);
customer.add(customers);
System.out.println("new customer " + customers.getName() + " added");
}
// **
public void deleteCustomer(String name, int id) {
Customer customers = new Customer(id, name);
if (customer.contains(name)) {
customer.remove(name);
}
System.out.println("Customer " + customers.getName() + " deleted");
}
public Customer findCustomer(int id) {
for (Customer c : customer) {
if (c.getId() == id) {
return c;
}
}
return null;
}
public void addHotel() {
Hotel H1 = new Hotel();
Scanner name = new Scanner(System.in);
System.out.println("Please Enter the name of Hotel: ");
String n1 = name.nextLine();
bookable.add(H1);
System.out.println("The Hotel " + name + "added");
}
public void makeABooking(Customer c, Bookable b) {
Scanner input =new Scanner(System.in);
System.out.println("Please Enter your name: ");
String name = input.nextLine();
System.out.println("Please Enter your ID: ");
int ID = input.nextInt();
while(true) {
if(ID == -1 && ID == 0) {
System.out.println("Invalid ID. Enter again: ");
name = input.nextLine();
System.out.println("Please Enter your ID: ");
ID = input.nextInt();
}
}
}
}
(Your question is more suitable to https://softwareengineering.stackexchange.com/ - recommend asking it there...)
General speaking it wouldn't make sense to have a Hotel without a name, location or number of rooms so I'd recommend adding a constructor with minimal required information:
public Hotel (String name, String location, int rooms) {
this.name = name;
this.location = location;
this.noOfRooms = rooms;
}
bookingDate makes no sense as a single property of a hotel but rather a property of each booked room so you have a design issue - this is not addressed here.
Again, roomPrice usually varies by room so in a robust solution this would be a property of a room not a hotel - not addressed here.
Why is there a noOfRooms and a numberOfRooms list. In fact, the numberOfRooms list doesn't make sense as a list. I'd just keep the noOfRooms and get rid of numberOfRooms.
An implied property, nbrOfAvailableRooms can be derived from noOfRooms - bookedRooms.size();
I would assume your bookedRooms is a list of room numbers which are booked but that's not possible to tell from your implementation. You should focus on what you want Book to do.
The Book interface method is not documented but it looks like it should simply take an available asset (room) and consider it booked. It should return a boolean success not a String - especially not null.
I recommend writing (in pseudo code) what you want a Book implementation to do - include that in question. That is the core issue you are having.

System.out.print doesnt display customer data

I am writing a client database. I want to know the customer's name and hometown based on the customer number. When I enter number 2, I want to see Arya Stark, Edinburgh and when I enter number 1, I want to see Jon Snow, London. Why doesn't my program work? How to fix this?
package app;
import java.util.Scanner;
class Person {
String name;
String homeCity;
int customerNumber;
}
public class Customers {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String name;
System.out.print ("Give a customer card number: ");
name = input.next();
Person person1 = new Person();
person1.name = "Jon Snow";
person1.homeCity = "London";
person1.customerNumber = 1;
Person person2 = new Person();
person2.name = "Arya Stark";
person2.homeCity = "Edinburgh";
person2.customerNumber = 2;
System.out.println();
}
}
This should work:
class Person {
private String name;
private String homeCity;
private int customerNumber;
public Person(String name, String homeCity, int customerNumber) {
this.name = name;
this.homeCity = homeCity;
this.customerNumber = customerNumber;
}
public boolean isMatch(int num) {
return num == customerNumber;
}
#Override
public String toString() {
return name + " from " + homeCity;
}
}
import java.util.Scanner;
class Main {
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Person person1 = new Person("Jon Snow", "London", 1);
Person person2 = new Person("Arya Stark", "Edinburgh", 2);
while(true) {
System.out.print("Give a customer card number: ");
String num = input.next();
if (person1.isMatch(Integer.parseInt(num))) {
System.out.println(person1);
} else if (person2.isMatch(Integer.parseInt(num))) {
System.out.println(person2);
} else {
System.out.println("Not found");
}
}
}
}

Finding Percentages of elements inside an ArrayList<model>

So I am doing a project which I want to built a library with two ArrayLists one of the ArrayList'<'Book'>' BookList contains an element named quantity has to greater or equivalent to zero if the quantity of the book is above zero another element called status in the BookList is set to In-stock if it's equal to zero it's set to borrowed. I'm trying to create a method that looks at the BookList and shows the percentage of books that are borrowed. I have done this by going through the list and each time it finds a book with quantity below 1 in other words 0 the counter goes up by one so in the end I just substract the counter from the BookList.size(), divide the result with the BookList.size(), multiply it by 100 and fially print it.
Main Class
public class Main {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
int uinput;
Library nag;
try{
nag = new Library();
do{
System.out.println("Type 1 to add a book.");
System.out.println("Type 2 to show how many books are borrowed.");
uinput = keyb.nextInt();
if (uinput==1){
nag.addBook();
}
if (uinput==2){
nag.statistics();
}
}while (uinput > 0);
}
catch(Exception e){
System.out.println("Invalid entry.");
}
}//end of main
}//end of class
Book Class
public class Book {
private String Title;
private String Author;
private String ISBN;
private String Publisher;
private String Publication_Date;
private String Price;
private int Quantity;
private String Status;
public Book(){
Title= "";
Author="";
ISBN="";
Publisher="";
Publication_Date="";
Price="";
Quantity=1;
Status="IN-STOCK";
}
//getters
public String gettitle(){return Title;}
public String getauthor(){return Author;}
public String getisbn(){return ISBN;}
public String getpublisher(){return Publisher;}
public String getpublication_date(){return Publication_Date;}
public String getprice(){return Price;}
public int getquantity(){return Quantity;}
public String getstatus(){return Status;}
//setters
public void settitle(String t){Title = t;}
public void setauthor(String a){Author = a;}
public void setisbn(String is){ISBN = is;}
public void setpublisher(String p){Publisher = p;}
public void setpublication_date(String pd){Publication_Date = pd;}
public void setprice(String pr){Price = pr;}
public void setquantity(int q){Quantity = q;}
public void setstatus(String s){Status = s;}
}//end of class
Library Class
public class Library {
private ArrayList<Book> BookList;
public Library(){
BookList = new ArrayList<Book>();
}//end of constructor 1
public Library(ArrayList<Book> l) {
BookList = l;
}//end of constructor 3
public void addBook(){
try{
Scanner keyb = new Scanner(System.in);
Book bo = new Book();
System.out.println("Type the title: ");
String title_input;
title_input = keyb.nextLine();
bo.settitle(title_input);
System.out.println("Type the author: ");
String author_input;
author_input = keyb.nextLine();
bo.setauthor(author_input);
System.out.println("Type the isbn: ");
String isbn_input;
isbn_input = keyb.nextLine();
bo.setisbn(isbn_input);
System.out.println("Type the publisher: ");
String publisher_input;
publisher_input = keyb.nextLine();
bo.setpublisher(publisher_input);
System.out.println("Type the publication date: ");
String publication_date_input;
publication_date_input = keyb.nextLine();
bo.setpublication_date(publication_date_input);
System.out.println("Type the price: ");
String price_input;
price_input = keyb.nextLine();
bo.setprice(price_input);
System.out.println("Type the quantity: ");
int quantity_input = Integer.parseInt(keyb.nextLine());
if (quantity_input >= 0){
bo.setquantity(quantity_input);
if (quantity_input > 0){
bo.setstatus("IN_STOCK");
}
if(quantity_input == 0){
bo.setstatus("BORROWED");
}
BookList.add(bo);
System.out.println("Book added successfully.");
}
}catch(Exception e){
System.out.println("Invalid entry");
}//end of addBook()
public void statistics(){
Book bo = new Book();
int counter = 0;
for(int i=0; i < BookList.size();i++){
bo= BookList.get(i);
int holdquantity = bo.getquantity();
if (holdquantity < 1){
counter++;
}
}
double substraction=BookList.size() - counter;
double division= substraction/BookList.size();
double percentage = division * 100;
System.out.print(percentage + "%");
}//end of statistics()
}//end of class
The problem is that it keeps printing 100.0% when I have a book with quantity of zero and another book with quantity above zero.
So I wanted to know if the problem lies within this code or elsewhere.
Let's say your Book class is as follows:
package com.company;
public class Book {
private int quantity;
public Book(int quantity) {
this.quantity = quantity;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
And your Main is as follows:
package com.company;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main (String[]args) {
List<Book> bookList = new ArrayList<>(){{
add(new Book(4));
add(new Book(0));
add(new Book(3));
add(new Book(7));
add(new Book(0));
add(new Book(0));
add(new Book(1));
add(new Book(9));
add(new Book(0));
add(new Book(5));
}};
int booksOutOfStock = 0;
for (int i = 0; i < bookList.size(); i++) {
if (bookList.get(i).getQuantity() == 0)
booksOutOfStock++;
}
double percentage = 100d / bookList.size() * booksOutOfStock;
System.out.printf("Out of total %d books, %d are out of stock, which makes %.2f%%", bookList.size(), booksOutOfStock,
percentage);
}
}
So check this code, compare it to yours and see where could the error be, or post complete code here so we can help more.
Output of code above:
Out of total 10 books, 4 are out of stock, which makes 40.00%
OK, so after you posted your code I reviewed it, and apart from doing some things the hard way, your code is "working", all I had to do was add a closing brace in Library class at the end of addBook() method after catch block:
catch(Exception e){
System.out.println("Invalid entry");
} --> } <--- (added)
//end of addBook()
And I fixed your percentage code logic, you code would print out the number of non borrowed books as borrowed percentage, here is that code changed:
double division= (double)counter/BookList.size();
double percentage = division * 100;
System.out.printf("%.2f", percentage);

For loop with user input whilst encapsulating in Java

I want to use a for loop to ask the user for the title, genre and rating of 3 movies and store the information in an array and then display the information back to them. The instance variables are private and getters and setters are used but I don't know how use the getters and setters in the MovieTestDriver class whilst trying to use a for loop to get information.
class Movie {
// Create instance variables for the Movie class.
private String title;
private String genre;
private int rating;
// Use getters and setters to set and display the variables.
// Getters.
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public int getRating() {
return rating;
}
// Setters.
public void setTitle(String newTitle) {
title = newTitle;
}
public void setGenre(String newGenre) {
genre = newGenre;
}
public void setRating(int newRating) {
rating = newRating;
}
void playIt() {
System.out.println(getTitle() + "- Now Playing!");
}
}
import java.util.Scanner;
public class MovieTestDriver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Creates a Movie array of size 3.
Movie[] movies = new Movie[3];
String getTitle;
String getGenre;
int getRating;
// Allows user to enter variables 3 times and populate movie object and store them in an array.
for (int i=0; i< movies.length; i++) {
System.out.println("Please enter the title of Movie " + (i+1));
getTitle = input.nextLine();
System.out.println("Please enter the genre of Movie " + (i+1));
getGenre = input.nextLine();
System.out.println("Please enter the rating (1-5) of Movie " + (i+1));
getRaing = input.nextInt();
}
}
Use another method to create a movie instance. And mayby another to display the movie:) That way your code will be clean and readable.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Creates a Movie array of size 3.
Movie[] movies = new Movie[3];
// Allows user to enter variables 3 times and populate movie object and store them in an array.
for (int i=0; i< movies.length; i++) {
movies[i] = inputMovie(input);
}
}
private static Movie inputMovie(Scanner input){
Movie movie = new Movie();
System.out.println("Please enter the title of Movie " + (i+1));
movie.setTitle(input.nextLine());
System.out.println("Please enter the genre of Movie " + (i+1));
movie.setGenre(input.nextLine());
System.out.println("Please enter the rating (1-5) of Movie " + (i+1));
movie.setRating(input.nextInt());
return movie;
}

Issue with a method for a library of objects

I'm trying to make a program that creates a library of different books, I have set a number of copies for each item in the library and every time I check out an Item I want it to deduct 1 copy from only the particular object I check out but instead it takes a copy away from all the objects. not sure how to fix the problem.
public abstract class Item{
private int identify;
private String title;
private int copies;
public Item(){
identify=0;
title="N/A";
copies=0;
}
public Item(int id, int copy, String t){
identify=id;
copies=copy;
title=t;
}
public void setIdentificationNumber(int id){
identify = id;
}
public void setTitle(String t){
title=t;
}
public void setNumberCopies(int num){
copies=num;
}
public int getIdentificationNumber(){
return identify;
}
public String getTitle(){
return title;
}
public int getNumberCopies(){
return copies;
}
public void checkOut(){
if(copies>0){
copies-=1;
System.out.println("You have checked out "+title+". Thank You");
}
else{
System.out.println("All copies of "+title+" are checked out!");
}
}
public void checkIn(){
copies+=1;
}
}
The problem may also be in my client method I have posted the code for that as well below.
import java.util.Arrays;
import java.util.Scanner;
public class Library{
static String title;
static String author;
static int id;
static int copies;
static String date;
static Book[] database = new Book[100];
static int count=0;
public static void main(String[] args){
int i;
Scanner s = new Scanner(System.in);
do{
addBook();
System.out.println("would you like to add another book?");
i=s.nextInt();
}while(i == 0);
database[0].viewDetails();
database[1].viewDetails();
checkingOut();
database[0].viewDetails();
database[1].viewDetails();
}
public static void addBook(){
Scanner s = new Scanner(System.in);
System.out.println("Enter the title of the book you want to add to the collection");
title=s.nextLine();
System.out.println("Enter the author of the book you want to add to the collection");
author=s.nextLine();
System.out.println("Enter the publishing date of the book you want to add to the collection");
date=s.nextLine();
System.out.println("Enter the ID number of the book you want to add to the collection");
id=s.nextInt();
System.out.println("Enter the the number of copies that will be added into the collection");
copies=s.nextInt();
Book Book1 = new Book(date, author, copies, id, title);
database[count] = Book1;
count++;
}
public static void checkingOut(){
boolean found=false;
int idSearch;
int i=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch=s.nextInt();
while(i<database.length && found!=true){
if(database[i].getIdentificationNumber() == idSearch){
found = true;
}
i++;
}
if(found==true){
database[i].checkOut();
System.out.println("There are "+database[i].getNumberCopies()+" copies left");
}
else{System.out.println("There is no book with that ID number!");}
}
}
In my addBook method I create a new object called book1 every time I make a new book, so I think that it may be changing all of the book objects every time I add a book. I'm not really sure of a better way to write the method.
here is my method for book also
public class Book extends WrittenItem{
public Book(){
super();
}
public Book(String date, String a, int copy, int id, String t){
super(a, date, copy, id, t);
}
public void viewDetails(){
System.out.println("ID: "+getIdentificationNumber()+"\nTitle: "+getTitle()+"\nAuthor: "+getAuthor()+" Date written: "+getDate()+"\nCopies available: "+getNumberCopies());
}
}
Any help would be greatly appreciated.
I can't 100% tell from your code, but I'm assuming Book extends Item. In that case, try something like this for Book constructor
public class Book extends Item {
String author;
String date;
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title); // Item constructor matches this super() call
// public Item(int id, int copy, String t)
this.author = author;
this.date = date;
}
}
You want Book to have the same copies at Item. So when you checkOut(), the Item number equals you input from the Book constructor. If you don't put the super() in the constructor, your Item copies will remain 0 and you will always get System.out.println("All copies of "+title+" are checked out!"); because copies is never > 0.
If your book is something like this,
public class Book extends Item {
private String date;
private String author;
public Book() {
}
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title);
this.author = author;
this.date = date;
}
void viewDetails() {
System.out.println("date:" + date + " author:" + author + " copies:" + getNumberCopies() + " id:" + getIdentificationNumber() + " title:" + getTitle());
}
}
Then your code should work fine, as i've tested, if you also add a break in your checkingOut() method,
public static void checkingOut() {
boolean found = false;
int idSearch;
int i = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch = s.nextInt();
while (i < database.length && found != true) {
if (database[i].getIdentificationNumber() == idSearch) {
found = true;
break; //add this
}
i++;
}
if (found == true) {
database[i].checkOut();
System.out.println("There are " + database[i].getNumberCopies() + " copies left");
} else {
System.out.println("There is no book with that ID number!");
}
}

Categories

Resources