×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Program to Create Rental System in Java Assignment Solution

July 01, 2024
Ellie Icely
Ellie Icely
🇬🇧 United Kingdom
Java
PhD in Computer Science from the University of Hertfordshire, with 8 years of experience in Java assignments. Expert in delivering high-quality, efficient solutions for complex programming problems. Passionate about teaching and mentoring students.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

Instructions

Objective

Write a java assignment program to create rental system.

Requirements and Specifications

program to create rental system in java

program to create rental system in java 1

program to create rental system in java 2

Source Code

import java.util.Scanner; import javax.swing.JOptionPane; /* * Description: a template for the project...it is optional to use thsi exact template and feel free * to design and develop your solution your way as long as you follow the requirements i.e. individual effort, only covered tools and techniques) */ public class PP1_template { public static void main(String[] args) { // Create Scanner //declarations for cabanas and customers: //declare all parallel arrays for cabanas attibutes: number, type, max occupancy, fees per day, availability, das String cabNO[] = new String[5]; char cabType[] = new char[5]; int cabMaxAvail[] = new int[5]; double cabFees[] = new double[5]; boolean cabAvail[] = new boolean[5]; //declares all parallels arrays for customer: id, first, last, cabana rented Number, total balance (days times daily fees) int CustomerID[] = new int[100]; String CustomerFirstName[] = new String[100]; String CustomerLastName[] = new String[100]; int CabanaID[] = new int[100]; double RentalBalance[] = new double[100]; // Initialize all values in CabanaID to -1 for(int i = 0; i < 100; i++) { CabanaID[i] = -1; } //declare all working variables: cabana count, customer count, cabanFountAt, customerFoundAt int customer_count = 0; // count number of customers int cabana_count = 0; int cabanFoundAt = -1; int custFoundAt = -1; //...declare menu option populateCabanas(cabNO, cabType, cabFees, cabMaxAvail, cabAvail);//send references to all arrays for a cabanas int option = 0; while(option != 6) { option = menu(); switch(option) { case 0: // invalid option entered JOptionPane.showMessageDialog(null, "Please enter a valid menu option."); break; case 1: //get customer //increment count for customers customer_count++; // Check that there is space for customers if(customer_count < 100) { getCustomer(CustomerID, CustomerFirstName, CustomerLastName, customer_count-1); } else { JOptionPane.showMessageDialog(null, "You cannot create more customers!"); } break; case 2: //process rental by calling the get rental function cabana_count++; rent(cabNO, cabType, cabMaxAvail, cabFees, cabAvail, CustomerID, CabanaID, RentalBalance, customer_count); break; case 3: //prompt user to enter an ID for a customer String customer_id_str = JOptionPane.showInputDialog("Enter Customer ID"); int customer_id = -1; //validate try { customer_id = Integer.valueOf(customer_id_str); } catch(Exception ex) { JOptionPane.showMessageDialog(null, "Enter a valid Customer ID"); } custFoundAt = searchCustomer(customer_id, CustomerID, customer_count); if(custFoundAt != -1) // customer found { dispCustomer(CustomerID, CustomerFirstName, CustomerLastName, CabanaID, RentalBalance, custFoundAt, cabana_count, cabNO, cabType); } else { JOptionPane.showMessageDialog(null, "No customer found with that id"); } //call the search function for customers (pass the ref of id's and the count //and if found, //call to display this customer at position i.e. custFoundAt pass references of needed arrays break; case 4: //prompt user to enter a number for a cabana //validate //call the search function for cabanas: pass a ref of numbers and the count //and if found,: //call to display this cabana at position i.e. custFoundAt pass references of needed arrays String cabana_id = JOptionPane.showInputDialog("Enter Cabana ID"); cabanFoundAt = searchCabana(cabana_id, cabNO); if(cabanFoundAt != -1) // cabana found { dispCabana(cabNO, cabType, cabFees, cabMaxAvail, cabAvail, cabanFoundAt); } else { JOptionPane.showMessageDialog(null, "No Cabana found with that id"); } break; case 5: //call the search rental: pass all arrays and the count for current customers break; case 6: //end of program...goodbye!@ unless extra credits are to be assigned and be done after the user quit JOptionPane.showMessageDialog(null, "Good bye!"); break; default: //invalid option JOptionPane.showMessageDialog(null, "Please enter a valid menu option."); break; }//end switch(option) }//end while(option != 6) }//end main //------------------------------------------------------------------------ public static void populateCabanas(String[] cabNO, char[] cabType, double cabFees[], int cabMaxAvail[], boolean[] cabAvail) //receive the references for all arrays for cabanas { /* * cabNO[0] = "101"; * cabType[0] = 'D'; * cabFees[0] = 54.95 * cabMaxAvail[0] = 4; * cabAvail[0] = true; * *repeat for all other 4 cabanas populating position 1, 2, 3, 4 * */ cabNO[0] = "101"; cabType[0] = 'D'; cabFees[0] = 54.95; cabMaxAvail[0] = 4; cabAvail[0] = true; cabNO[1] = "102"; cabType[1] = 'R'; cabFees[1] = 25.75; cabMaxAvail[1] = 10; cabAvail[1] = true; cabNO[2] = "103"; cabType[2] = 'R'; cabFees[2] = 39.99; cabMaxAvail[2] = 8; cabAvail[2] = true; cabNO[3] = "104"; cabType[3] = 'D'; cabFees[3] = 95.85; cabMaxAvail[3] = 2; cabAvail[3] = true; cabNO[4] = "105"; cabType[4] = 'R'; cabFees[4] = 40.75; cabMaxAvail[4] = 6; cabAvail[4] = true; }//end populateCabanas public static int menu() { int option = 0; /* * -1-Create a customer -2-Process rental -3-Search Customer -4-Search for Cabana -5-Search for a Rental -6-Quit program */ // Display //validate user selection try { option = Integer.valueOf(JOptionPane.showInputDialog("1) Create a customer\n2) Process rental\n3) Search Customer\n4) Search for Cabana\n5) Search for a Rental\n6) Quit program")); } catch(Exception ex) { // do nothing } //parse return option; }//end menu //---------------------------------------------------------------------------- public static void getCustomer(int CustomerID[], String CustomerFirstName[], String CustomerLastName[], int customer_count) //arguments are reference to all affected array to create a customer...also current customer { String input = " "; //prompts to get the customer ID input = JOptionPane.showInputDialog("Enter Customer ID"); //validate int customer_id = -1; boolean valid_customer = true; try { customer_id = Integer.valueOf(input); if(input.length() != 6) { // id must be 6-digits long valid_customer = false; } } catch(Exception ex) { valid_customer = false; } //store in current location in array custID //prompts to get the customer first name String customer_first = ""; input = JOptionPane.showInputDialog("Enter Customer's First Name"); //validate if(input.length() > 0) { customer_first = input; } else { valid_customer = false; } //store in current location in array custLast //prompts to get the customer last name String customer_last = ""; input = JOptionPane.showInputDialog("Enter Customer's Last Name"); //validate if(input.length() > 0) { customer_last = input; } else { valid_customer = false; } //store in current location in array custLast if(valid_customer) { CustomerID[customer_count] = customer_id; CustomerFirstName[customer_count] = customer_first; CustomerLastName[customer_count] = customer_last; System.out.println("OK"); } //the cabana number for the cabana rented and the balance are determined in the rental process/function //make sure ou code all our validation functions at the end }//end getCustomer //-------------------------------------------------------------------------------- public static int searchCustomer(int customer_id, int CustomerID[], int customer_count) //pass the id of the customer to search for and the arra of id's { //run a loop through the array of id's and if found: for(int i = 0; i < customer_count; i++) { if(CustomerID[i] == customer_id){ return i; } } //return position //after the loop/not found return -1 return -1; }//end searchCustomer //--------------------------------------------------------------------------------- public static int searchCabana(String cabID, String cabNO[]) //pass references of the cabana ..you can do a similar and additional search by type for the purpose of rental { for(int i = 0; i < 5; i++) { if(cabID.compareTo(cabNO[i]) == 0) { return i; } } //run a loop through the array of cabana numbers and if found: //return position //after the loop/not found return -1 return -1; }//end searchCabana //---------------------------------------------------------------------------------- public static void rent(String cabNO[], char cabType[], int cabMaxAvail[], double cabFees[], boolean cabAvail[], int CustomerID[], int CabanaID[], double RentalBalance[], int customer_count) //pass references of all needed arrays and variables { //ask customer for ID int customer_id = Integer.valueOf(JOptionPane.showInputDialog("Enter Customer ID")); //call the search, and if found, int id = searchCustomer(customer_id, CustomerID, customer_count); int cabana_id = -1; if(id != -1) // customer found { //as for the type of the cabana ('D' for Delux or 'R' for regular char cab_type = ' '; while(true) { cab_type = JOptionPane.showInputDialog("Enter type of Cabana").charAt(0); if(cab_type == 'D' || cab_type == 'R') break; } //search the array of Cabanas for the customer type choice and if found for(int i = 0; i < 5; i++) { if(cabType[i] == cab_type) { //check if available and if available/true: if(cabAvail[i]) { cabana_id = i; break; } } } if(cabana_id != -1) // one available { // ask for occupants int occupants = Integer.valueOf(JOptionPane.showInputDialog("Enter number of occupants: ")); if(occupants <= cabMaxAvail[cabana_id]) { // Ask for days int days = Integer.valueOf(JOptionPane.showInputDialog("Enter number of days")); // Reservation is complete so process CabanaID[id] = cabana_id; // calculate balance double balance = cabFees[cabana_id]*days; RentalBalance[id] = balance; JOptionPane.showMessageDialog(null, String.format("Your rental has been processed for a total of $%.2f", balance)); } else { JOptionPane.showMessageDialog(null, "Sorry, the number of occupants exceeds the size of the Cabana"); } } else { JOptionPane.showMessageDialog(null, "There is no Cabana available."); } //ask how many guest and check against the max occupancy and if larger //reject reservation //if customer not found, ask that the customer be registered first (add a customer via the menu //if type not found ask again for the correct type (accepted D or R) //if not available if false then go back //if reservation is complete then: //calculate balance to be: fees times days //update the customer record by copying the Cabana number to the cabana array under customer and //copy the balance to the array balance under customer } else { JOptionPane.showMessageDialog(null, "You must create an account first."); } }//end rent //------------------------------------------------------------------------------------ public static void dispCustomer(int CustomerID[], String CustomerFirstName[], String CustomerLastName[], int CabanaID[], double RentalBalance[], int custFoundAt, int cabana_count, String cabNO[], char cabType[]) //pass the array ref of customer and the position (i.e. custFoundAt) { String result = String.format("ID: %d\nFirst Name: %s\nLast Name: %s\n", CustomerID[custFoundAt], CustomerFirstName[custFoundAt], CustomerLastName[custFoundAt]); // Check if there is a cabana processed for this customer if(CabanaID[custFoundAt] != -1) { int cabana_id = CabanaID[custFoundAt]; result += String.format("\nCabana ID: %s\nCabana Type: %s\nRental Price $%.2f", cabNO[cabana_id], cabType[cabana_id], RentalBalance[custFoundAt]); } else { result += "\nThis Customer has not processed any rent."; } JOptionPane.showMessageDialog(null, result); //display all information about this customer: id, first, last, cabanan rented if rental completed, balance }//end dispCustomer //------------------------------------------------------------------------------------ public static void dispCabana(String[] cabNO, char[] cabType, double cabFees[], int cabMaxAvail[], boolean[] cabAvail, int cabFoundAt) //pass the ref to all arrays of cabana and the position (i.e. cabFoundAt) { String availability = "yes"; if(!cabAvail[cabFoundAt]) { availability = "no"; } //display all information about this cabana: number, type, fees, availability, days JOptionPane.showMessageDialog(null, String.format("Cabana ID: %s\nCabana Type: %s\nDay rental rate: $%.2f\nOccupancy: %d\nAvailable: %s", cabNO[cabFoundAt], cabType[cabFoundAt], cabFees[cabFoundAt], cabMaxAvail[cabFoundAt], availability)); }//end dispCabana //-------------------------------------------------------------------------------------- public static void dispRental(int CustomerID[], String CustomerFirstName[], String CustomerLastName[], int CabanaID[], double RentalBalance[], int cabana_count, String cabNO[], char cabType[], int customer_count) //pass everything { String input = JOptionPane.showInputDialog("Enter Customer ID or Cabana ID"); boolean found = false; try{ int customer_id = Integer.valueOf(input); int custFoundAt = searchCustomer(customer_id, CustomerID, customer_count); if(custFoundAt != -1) { dispCustomer(CustomerID, CustomerFirstName, CustomerLastName, CabanaID, RentalBalance, custFoundAt, cabana_count, cabNO, cabType); found = true; } else // then it is probably a Cabana { int cabanFoundAt = searchCabana(input, cabNO); int cusFoundAt = -1; if(cabanFoundAt != -1) { // search a customer such that has this cabana id for(int i = 0; i < cabana_count; i++) { if(CabanaID[i] == cabanFoundAt){ cusFoundAt = i; break; } } if(cusFoundAt != -1) { dispCustomer(CustomerID, CustomerFirstName, CustomerLastName, CabanaID, RentalBalance, custFoundAt, cabana_count, cabNO, cabType); } else { JOptionPane.showMessageDialog(null, "No Customer or Cabana Found"); } } else { JOptionPane.showMessageDialog(null, "No Customer or Cabana Found"); } } } catch(Exception ex) { JOptionPane.showMessageDialog(null, "No Customer or Cabana Found"); } //ask if search by customer ID or cabana Number //conduct the selected search //if transaction/rental is found and search by cust ID then //display all the info about this customer by calling the dispCustomer and display the related Cabana and balance //if search y cabana number is selected then //call the cabana search function and if found //display everything about the customerr and the cabana. }//end dispRental //---------------------------------------------------------------------------------------- //code all your validations here....i.e. checkCustID, checkName, checkcabanNO, checkGuests, checkDays, ...etc..etc //code an extra search by cabana type where you will be searching for 'D' for Dlux or 'R' for regular //note that D and R are single char so you do not need to use the .equal(0 or similar methods..a normal comparison using '==' is required }//end class PP1

Similar Samples

Explore our sample projects and assignments to see the quality and expertise we bring to every task. From basic programming to complex algorithms, our examples showcase our commitment to helping you excel in your coding journey.