7 Replies - 1325 Views - Last Post: 20 March 2013 - 07:03 PM Rate Topic: -----

#1 rsj101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 13

Stuck with Car Rental program

Posted 20 March 2013 - 12:38 AM

Hello all I am really new to Java. What I have done is what I have studied from my class and from the book. But at this point nothing seems to be working.The object of this program is to choose between vehicles(Car,Truck,SUV) cost per day for car 30 truck 40 SUV 55. Each vehicle has a larger size which increases the price by 10%. If you rent the vehicle more then 7 days you have to subtract 25%. Also it asks if you need insurance. If so add 5 dollars per day.
import java.util.Scanner; 
public class project4rsj {
    
    public static void main(String[] args) {
    
    Scanner keyboard = new Scanner(System.in);	
    

    int days;
    int ins;
    int vehical;
    int large,small;
    String Model;
    String Make;
    String Ins;
    String cars;
    String trucks;
    String SUV;
    char inputChar;
    
    //instantiate the class
    
    cars carPick = new cars();
    trucks truckObject = new trucks();
    SUV SUVobject = new SUV();
    //enter days for rental
    
   System.out.print("Enter the Number of Days: ");
   days = keyboard.nextInt(); 
   	
   	//enter for Ins.
   System.out.println("Do you wish to have insurance?");
   System.out.println("Enter 1 for Yes");
   System.out.println("Enter 2 for No");
   ins = keyboard.nextInt();
   //type of vehical
   System.out.print("What vehical do you want?\n");
   System.out.println("Enter 1 for Car");
   System.out.println("Enter 2 for Truck");
   System.out.println("Enter 3 for SUV");
   vehical = keyboard.nextInt();
   
   //want large or small vehical
   System.out.println("What model do you want Large or Small?");
   System.out.println("Enter 1 for Small");
   System.out.println("Enter 2 for Large");
   inputChar = keyboard.next().charAt(0);
   
   
   
   
   
   
    }
}
this is what I have for now I am stuck on how to either start a switch statement or how to connect them so will do the mathematics. Any help or even a push in the right direction would be greatly appreciated..

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck with Car Rental program

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Stuck with Car Rental program

Posted 20 March 2013 - 04:07 AM

Before I can help I need to know what your desired output is.
Was This Post Helpful? 0
  • +
  • -

#3 rsj101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 13

Re: Stuck with Car Rental program

Posted 20 March 2013 - 04:59 AM

View PostFlukeshot, on 20 March 2013 - 04:07 AM, said:

Before I can help I need to know what your desired output is.

I'm not sure what you were really looking for but I think this is what you were asking for. Thanks for any help you can offer.

I would like the out put to be
if you pick car: your cost will be: the cost of the car with or without the ins, if the the input wants the larger car then that adds 10% to the total cost of the car per day. if the input wants or needs insurance then that only adds 5 dollars per day to the total costs. if the input wants the car for longer then 7 days then the total will be 25% off.

Like if it were on a receipt or something like that. if the input wants a car for 4 days with insurance
Car is "x with ins $30 *4 days + $5 per day for insurance

or if the input wanted 8 days without insurance and larger car but because they want larger car that adds 10% to the car
Car is "x = without ins 30* 8-25% + 10%
Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Stuck with Car Rental program

Posted 20 March 2013 - 05:53 AM

Then you simply need to write that in a println() statement:

if(days > 6) {
   System.out.println("The cost of this " + carObject.getName() 
                       + " is $" + (carObject.getPrice()*days)*0.75 + ".");
} else {
//same as above without *0.75
if(ins == 1) {
   System.out.println("Your total insurance cost is: $" + (days * 5));
}


That obviously creates some need for improvement in your original code. I can't see your vehicle objects so I can't make any comment for those.

The way you have designed the program leaves it extremely volatile: Using nextInt(), if the user enters "s" your program is going to crash. You will want to add a try/catch for InputMismatchException and use if statements to make sure the int is a 1, a 2 or a 3.

I'm going to have to see all of the code to understand what your strategy is and help you further.
Was This Post Helpful? 0
  • +
  • -

#5 rsj101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 13

Re: Stuck with Car Rental program

Posted 20 March 2013 - 06:08 PM

View Postrsj101, on 20 March 2013 - 04:59 AM, said:

View PostFlukeshot, on 20 March 2013 - 04:07 AM, said:

Before I can help I need to know what your desired output is.

I'm not sure what you were really looking for but I think this is what you were asking for. Thanks for any help you can offer.

I would like the out put to be
if you pick car: your cost will be: the cost of the car with or without the ins, if the the input wants the larger car then that adds 10% to the total cost of the car per day. if the input wants or needs insurance then that only adds 5 dollars per day to the total costs. if the input wants the car for longer then 7 days then the total will be 25% off.

Like if it were on a receipt or something like that. if the input wants a car for 4 days with insurance
Car is "x with ins $30 *4 days + $5 per day for insurance

or if the input wanted 8 days without insurance and larger car but because they want larger car that adds 10% to the car
Car is "x = without ins 30* 8-25% + 10%



Basically at this point I only have the other classes I am basically lost on where to go with this...After reading java book along with a not so good text book im basically lost...I understood my military SOP's better then java
Was This Post Helpful? 0
  • +
  • -

#6 rsj101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 13

Re: Stuck with Car Rental program

Posted 20 March 2013 - 06:15 PM

View PostFlukeshot, on 20 March 2013 - 05:53 AM, said:

Then you simply need to write that in a println() statement:

if(days > 6) {
   System.out.println("The cost of this " + carObject.getName() 
                       + " is $" + (carObject.getPrice()*days)*0.75 + ".");
} else {
//same as above without *0.75
if(ins == 1) {
   System.out.println("Your total insurance cost is: $" + (days * 5));
}


That obviously creates some need for improvement in your original code. I can't see your vehicle objects so I can't make any comment for those.

The way you have designed the program leaves it extremely volatile: Using nextInt(), if the user enters "s" your program is going to crash. You will want to add a try/catch for InputMismatchException and use if statements to make sure the int is a 1, a 2 or a 3.

I'm going to have to see all of the code to understand what your strategy is and help you further.

Ive also tried to instantiate a class from the other pages but gives me (;)expected error.
Was This Post Helpful? 0
  • +
  • -

#7 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 417
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Stuck with Car Rental program

Posted 20 March 2013 - 06:56 PM

As I'm sure you're aware, your program will start at the first line of main(). Try and keep track of the way the compiler will read your code and design it in an easy to follow structure.

In my own experience you can keep simple programs very clean by using main() only for instantiating the main objects, and then letting logic do the rest.

You require a lot of user input, so you're right to ask the user for that first. But as an example, how about this?

import java.util.*;

public class Test {
	
	int days;
	int carType;
	boolean insurance = false;

	public static void main(String[] args) {
		new Test();
	}
	
	Test() {//The test constructor will call these 
		//methods as soon as you make the new test object.
		getUserInput();
		heresTheLogic();
	}

	void getUserInput() {
		//Find out how many days
		Scanner in = new Scanner(System.in);
		System.out.println("For how many days do you wish to rent the vehicle?");
		try {
			days = in.nextInt();
		} catch (InputMismatchException e) {
			e.printStackTrace();
		}
		//Find out car type
		System.out.println("Which type of vehicle do you require?\n" +
				"1. Car\n" +
				"2. Van\n" +
				"3. Truck");
		try {
			carType = in.nextInt();
		} catch (InputMismatchException e) {
			e.printStackTrace();
		}
		//further code for this task is your job :)/>
	}
	
	void heresTheLogic() {
		switch(carType) {
		case 1:
			Car rental = new Car();
			if(days > 6) {
			   System.out.println("The cost of this " + rental.getName() 
	                       + " is $" + (rental.getPrice()*days)*0.75 + ".");
			} else {
			   System.out.println("The cost of this " + rental.getName() 
	                       + " is $" + (rental.getPrice()*days)*0.75 + ".");
			}
			if(insurance) {
			   System.out.println("Your total insurance cost is: $" + (days * 5));
			}
		case 2:
			//.....
		}
	}
}

This post has been edited by Flukeshot: 20 March 2013 - 06:58 PM

Was This Post Helpful? 1
  • +
  • -

#8 rsj101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 13

Re: Stuck with Car Rental program

Posted 20 March 2013 - 07:03 PM

Flukeshot thanks so much for your help. You have explained this way better then my teacher could....thoughts of being a teacher? I will post my finished code so you can see.. Thanks again I do appreciate your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1