Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    18
    ReputationRep:
    I'm not very good.. So no laughing!! :sad:

    I'm trying to design a program that calculates insurance for a bike.. I can do it.. However my method is relatively long.. I'm looking to do it efficiently.

    So I currently have standard insurance rate of £30.

    Now if the person is aged 25 or above then the insurance needs to have an additional £5 added.

    Would it be easiest to have the insurance as a variable/string or an integer?

    Secondly.. How would I put this in code form? Thus far I have

    Code:
    System.out.print ("How old are you?");
    Age = input.next();
    
    if (Age >= 25)
    {
    -=INSERT CODE=-
    
    System.out.print
                ("What bike do you currently own? (Mountain Bike or Touring Bike");
    Bike = input.next();
    Online

    21
    ReputationRep:
    Any variable that deals with currencies should be a double. Also, the NumberFormat library can convert your double into a currency string.
    http://docs.oracle.com/javase/7/docs...berFormat.html
    Offline

    0
    ReputationRep:
    import java.util.Scanner;


    public class test {
    static double insurance;
    public static void main(String []args){



    System.out.println("How old are you?");

    Scanner scan = new Scanner(System.in);
    int age = scan.nextInt();
    System.out.println(age);

    if (age <= 25)
    {
    insurance = 25;
    }

    else
    {
    insurance = 30;
    }

    System.out.println("What type of bike do you own? mountain bike or touring bike");

    Scanner scan2 = new Scanner(System.in);
    String ans = scan2.nextLine();

    if (ans.equals("mountain bike"))
    {
    insurance = insurance * 1.2;
    System.out.println("Your insurance is equal to £" + insurance);
    }
    else if(ans.equals("touring bike"))
    {
    insurance = insurance * 0.8;
    System.out.println("Your insurance is equal to £" + insurance);
    }
    }
    }

    I am also new to this so I'm guessing you want something like this, I made my insurance variable a double as I used decimal in my formulas at the end, however you could also use Int if you are not using decimal. If you have insurance as a string then you cannot use it in mathematical formulas such as insurance * 0.8.
    Offline

    15
    ReputationRep:
    (Original post by HTTP 404)
    import java.util.Scanner;

    I am also new to this so I'm guessing you want something like this, I made my insurance variable a double as I used decimal in my formulas at the end, however you could also use Int if you are not using decimal. If you have insurance as a string then you cannot use it in mathematical formulas such as insurance * 0.8.
    Just so your aware you can do this

    string string1 = "10";
    string string2 = "20";

    int number1 = Integer.parseInt(string1);
    int number2 = Integer.parseInt(string2);

    number 1 + number2 = 20.
 
 
 
Poll
Do you agree with the PM's proposal to cut tuition fees for some courses?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.