You are on page 1of 8

CSC 201

Computer Science – I
Homework – 3
Out: Sunday, Nov 1st 2009
Due: Sunday, Nov 15th 2009

Instructions

1) All the Homeworks are individual assignments. No group work allowed.


2) This electronic version of this assignment is due Nov 15th 10:00AM(Sunday).
3) Please include proper comments in your programs.
4) If it is a programming assignment, you would need to submit both electronic
copy (to pgundamraju@nvcc.edu) and a hard copy. Same timings apply for
both the submissions.
5) Non-submission of homework electronically OR manually will result in a ‘0’
in that homework.
6) Please read and understand the Cheating Policy mentioned in the course
outline document.
7) Any questions/concerns should be directed to the Professor.
8) I recommend students to start working on the assignment as early as
possible.

Total: 50 Points
Question: Algorithm: 10 points Code/Program: 40 points

Programming Assignment – 3 simulates ‘Java Classes’. For the below assignment we


learn to create/design our own data type.

Write an algorithm and a Java Program for the below scenario:

We simulate a Car Reservation System where in users of this system can check the
availability of a car and reserve it. The end-user will be presented with 3 options:
1) View cars 2) Reserve car 3) Exit the system.

There are two types of customers: Preferred (P) and Normal Customers (N).
Scenario 1: View cars

When either user (Preferred or Normal customers) enters option ‘1’, he can view a list of
all the cars and their availability. Your output should look something like below.

Car Name Availability MemberCode


Car1 True P
Car2 True P
Car3 True N
Car4 True N

The carName variable is of datatype String.


The availability variable is of data type Boolean.
The MemberCode variable is of datatype String.

Scenario 2: Reserve Car

When either user enters option ‘2’, user has the ability to reserve a car. But a normal
customer with member code = N cannot reserve a car whose member code = ‘P’.
A preferred customer can reserve cars with either of the member code i.e ‘P’ or ‘N’.

Scenario 3: Exit

When user enters option ‘3’, just break out from the system and your program should end
running.

_______________________________________________________________________
_

Below is the skeleton I have designed. Students are free to design their own program.

import scanner class;

class car
{
bavailability;
String member_code;
String car_name;

car(String name, String code)


{

}
boolean getAvailability(String name)
{

public String getCarName()


{

public String getMemberCode()


{

void displayAllCars()
{

void reserveCars(String name)


{

}
}

Class carrental
{
public static void main(String[] args)
{
System.out.println(“Hello, Welcome to ABC car Rental company”);
Scanner s = new Scanner(System.in);

car[] carobj = new car[3];

// Students are encouraged to take a sample of more than 10 cars


//Initializing the car objects in below code
//The availability for all cars is true initially.
//You can initialize P or N according to your wish.

carobj[0] = new car("car1","P");


carobj[1] = new car("car2","P");
carobj[2] = new car("car3","N");

System.out.println("Enter your code P:For Preferred Customers or N: For


normal customers.");
String code = s.next();
if(code.equalsIgnoreCase("N"))
{
System.out.println("Welcome customer.");
while(true)
{
System.out.println("Here are your options:1.View
Cars\n2.Reserve a car\n3.Exit");

// Please fill in the code when a normal customer enters above


//options

}
}

else
if(code.equalsIgnoreCase("P"))
{
System.out.println("Welcome preferred customer:");
while(true)
{
System.out.println("Here are your options:1.View
Cars\n2.Reserve a car\n3.Exit");
// Please fill in the code when a preferred customer enters above
//options

}
}

}
}

Sample Run:
run: When a preferred customer wants to operate this system.

Hello welcome to ABC car company


Please enter num of cars to operate on
3
Enter your code P:For Preferred Customers or N: For normal customers.
P
Welcome preferred customer:
Here are your options:
1.View Cars
2.Reserve a car
3.Exit

1
CarName Avail Member-Code
car1 true P
car2 true P
car3 true N

Here are your options:


1.View Cars
2.Reserve a car
3.Exit
2
Choose car name

car1
Your car is reserved

Here are your options:

1.View Cars
2.Reserve a car
3.Exit

Choose car name


car3

Your car is reserved


Here are your options:
1.View Cars
2.Reserve a car
3.Exit

1
CarName Avail Member-Code

car1 false P
car2 true P
car3 false N

Here are your options:

1.View Cars
2.Reserve a car
3.Exit
3
You have exited the system!

BUILD SUCCESSFUL (total time: 38 seconds)

Sample Run: For a Normal Customer

run:
Hello welcome to ABC car company
Please enter num of cars to operate on
3
Enter your code P:For Preferred Customers or N: For normal customers.
N
Welcome customer.
Here are your options:

1.View Cars
2.Reserve a car
3.Exit
1

CarName Avail Member-Code

car1 true P
car2 true P
car3 true N

Here are your options:

1.View Cars
2.Reserve a car
3.Exit

Enter a car name to reserve:

car1

Preferred Car: Sorry, You cannot reserve a preferred customer car!!

Here are your options:

1.View Cars
2.Reserve a car
3.Exit

Enter a car name to reserve:


car3
Your car is reserved
Here are your options:

1.View Cars
2.Reserve a car
3.Exit

CarName Avail Member-Code

car1 true P
car2 true P
car3 false N

Here are your options:

1.View Cars
2.Reserve a car
3.Exit
3
You have exited the system!

BUILD SUCCESSFUL (total time: 28 seconds)

_______________________________________________________________________

You might also like