Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: To transform my code to be able to save the room booking into .txt file

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default To transform my code to be able to save the room booking into .txt file

    Hi, im new in java. I am doing the home work to create a mini hotel reservation system which should be able to write the data into .txt file.

    case study:
    1. Have 2 different types of room (1- Suite 2- Deluxe)
    2. Room Rate (Suite - $500per night, Deluxe - $300per night)
    3. Both rooms can accomodate 2adults and 2childs
    4. Checkin time: 2pm, Checkout 11am
    5. Room status = occupied once guest checkin, Room status = vacant once guest checkout
    6.For checking require (ID,Full Name,Address,Nationality,Flight no/Vehicle Reg no,number of people,check-in date, check-out-date,payment detail
    7.Front desk search the vacant room to be assigned to the guest

    Functions:
    1. View all rooms
    2. Add Customer to a room
    3. Display available room
    4. Delete customer from a room
    5. Find room from customer name
    6. Exit

    Files:
    One file use to store room info. (to be retrieved by program)
    One file use to store rate info. (to be retrieve by program)
    One file use to store the booking details (to be created and write by the program).

    Currently I have write a program but only compiling and display the output to the console. Could somebody help me to modify my code below to be able to save to the .txt file

    Here is the code:
    ----------------


    import java.util.*;
    import java.io.*;

    class Customer
    {
    private String name;
    private int room;

    public void setName(String name)
    {
    this.name=name;
    }

    public String getName()
    {
    return this.name;
    }

    public void setRoom(int room)
    {
    this.room=room;
    }

    public int getRoom()
    {
    return this.room;
    }
    }

    class Hotel
    {
    public static void initialize(Customer RoomList[])
    {
    for(int i=0; i<RoomList.length; i++)
    {
    RoomList[i]=new Customer();
    RoomList[i].setName("EMPTY");
    RoomList[i].setRoom(i+1);
    }
    }

    public static void viewList(Customer RoomList[])
    {
    for(int i=0; i<RoomList.length; i++)
    {
    if(RoomList[i].getName()=="EMPTY")
    System.out.println("Room number "+RoomList[i].getRoom()+" is vacant.");
    else
    System.out.println("Room number "+RoomList[i].getRoom()+" is ocupied by "+RoomList[i].getName()+".");
    }
    System.out.println();
    }

    public static boolean addCustomer(Customer RoomList[], String name)
    {
    for(int i=0; i<RoomList.length; i++)
    if(RoomList[i].getName().equals("EMPTY"))
    {
    RoomList[i].setName(name);
    return true;
    }
    return false;
    }

    public static void showEmptyRooms(Customer RoomList[])
    {
    System.out.println("Available rooms are:");
    for(int i=0; i<RoomList.length; i++)
    if(RoomList[i].getName()=="EMPTY")
    System.out.println(RoomList[i].getRoom());
    System.out.println();
    }

    public static boolean deleteCustomer(Customer RoomList[], String name)
    {
    for(int i=0; i<RoomList.length; i++)
    if(RoomList[i].getName().equals(name))
    {
    RoomList[i].setName("EMPTY");
    System.out.println("Deletion successful.\n");
    return true;
    }
    return false;
    }

    public static int getIndex(Customer RoomList[], String name)
    {
    for(int i=0; i<RoomList.length; i++)
    if(RoomList[i].getName().equals(name))
    return i;
    return -1;
    }

    public static void main(String[] args)
    {
    // File f=new File("C:/TestHotel.txt");
    Customer[] RoomList = new Customer[12];
    String name;
    initialize(RoomList);
    Scanner input = new Scanner(System.in);
    int option=0;



    do
    {
    System.out.println(" Hotel Booking Options");
    System.out.println("============================== =======");
    System.out.println("1: To View all rooms");
    System.out.println("2: To Add customer to a room");
    System.out.println("3: To Display empty rooms");
    System.out.println("4: To Delete customer from a room");
    System.out.println("5: Find room from customer name");
    System.out.println("0: Exit");

    System.out.print("\nEnter your choice: ");
    option = input.nextInt();
    System.out.println();

    switch(option)
    {
    case 1:
    {
    viewList(RoomList);
    break;
    }
    case 2:
    {
    System.out.print("Customer's name: ");
    name=input.next();
    System.out.println();
    if(!addCustomer(RoomList, name))
    System.out.println("No rooms available!");
    break;
    }
    case 3:
    {
    showEmptyRooms(RoomList);
    break;
    }
    case 4:
    {
    System.out.print("Customer's name: ");
    name=input.next();
    System.out.println();
    deleteCustomer(RoomList, name);
    break;
    }
    case 5:
    {
    System.out.print("Customer's name: ");
    name=input.next();
    System.out.println();
    System.out.println("Customer's room: "+RoomList[getIndex(RoomList, name)].getRoom()+"\n");
    break;
    }
    case 0:
    {
    System.out.println("\nThank you!\n");
    break;
    }
    default:
    {
    System.out.println("Invalid option!\n");
    break;
    }
    }


    }while(option!=0);
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    21
    My Mood
    Cheeky
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: To transform my code to be able to save the room booking into .txt file

    Things to remember while creating a new thread:
    1. Make sure you are posting under the right sub-forum
    2. Add a descriptive title that summarizes your message. Avoid vague titles like "Please solve my problem"
    3. For syntax highlighting please type [code=Java] before your code segment, and [/code] after your code segment

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: To transform my code to be able to save the room booking into .txt file

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    Very few, if any, will help you with so much code posted incorrectly. Focus on what you need help with, writing to a file apparently. What have you tried to write the necessary data to a file? What problems are you having with that?

Similar Threads

  1. Replies: 7
    Last Post: March 10th, 2014, 04:28 PM
  2. Replies: 1
    Last Post: July 26th, 2013, 03:25 AM
  3. Writing code to save in a file... help please
    By rchrist3 in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 13th, 2013, 11:51 AM
  4. Reading from a txt file into 3 arraylists and save it as double
    By depi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2011, 02:25 PM
  5. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM