Results 1 to 2 of 2
  1. #1
    jayfaxva is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Need help with Java program

    I'm having problems with the last part of my assignment. I know how to do arrays in scanner, but my teacher hasn't really talked too much about how to do it in JOptionPane.The last part of the directions are below. Any tips on exactly what I need to do and where I need to implement it would be wonderful.

    Prompt for the full name of the customer in the format below:
    FirstName,LastName
    Example: Ben,King
    When you display the quote you need to display the below format:
    First Name: Ben
    Last Name: King
    You need to re-prompt the name if the user doesn't have a comma in the name he/she provides.

    Java Code:
    import java.util.Scanner;
    
    public class String {
    
    public static void main(String[] args) {
    
    Scanner scan = new Scanner(System.in);
    String input;
    String firstName;
    String lastName;
    String names[];
    while (true) {
    System.out.print("<FirstName> , <LastName> : ");
    input = scan.nextLine();
    names = input.split(",");
    if (names.length == 2) {
    break;
    }
    System.out.println("seperate first name and last name with a comma(,) ");
    }
    System.out.println("FirstName: " + names[0]);
    System.out.println("LastName: " + names[1]);
    }
    }

    Java Code:
    /**
     * @author 
     */
    import javax.swing.JOptionPane;
    
    public class TakeHomeAssignment4 //public class name and file name are the same
    {
    	public static void main(String[] args)
    	{
    	//Set the global variables below
    		String firstName = "";
    		String lastName = "";
    		String licenseNum = "";
    		String insuranceType = "";
    		String yourAge = "";
    		String otherInsurance = "";
    		//Set the total, final quote, and number of tickets to 0
    		double total = 0;
    		double finalQuote = 0;
    		int numOfTickets = 0;
    		int custNum = 0;//Set the customer number to 0 so it starts at 0
    		
    		do{
    			//These two prompt the user for his/her name and license number
    			firstName = JOptionPane.showInputDialog(null, "What is your first name?");
    			lastName = JOptionPane.showInputDialog(null, "what is your last name?");
    			licenseNum = JOptionPane.showInputDialog(null, "What is your license number?");
    			//The user chooses what type of insurance they have (basic and premium)
    			String input = JOptionPane.showInputDialog(null, "What type of insurance do you have?" + " 1 for Basic($55.10), 2 for Premium($75.50)");
    			int insuranceChoice = Integer.parseInt(input);
    			
    			
    			//If they chose 1 (basic), it stores the amount the basic package costs in "total"
    			if (insuranceChoice == 1)
    			{
    				total = total + 55.10;
    				insuranceType = "Basic";
    			}
    			//If they chose 2 (premium, it stores the amount the basic package costs in "total"
    			else if (insuranceChoice == 2)
    			{
    				total = total + 75.50;
    				insuranceType = "Premium";
    			}
    			else
    			{
    			//If the user doesn't choose either of the packages, it will re-prompt them to choose again until they choose the correct one
    				JOptionPane.showMessageDialog(null, "Please select 1(Basic) or 2(Premium)");
    				JOptionPane.showInputDialog(null, "What type of insurance do you have?" + " 1 for Basic($55.10), 2 for Premium($75.50)");
    			}
    			//Prompts the user if they are under age 25
    			if(JOptionPane.showConfirmDialog(null,"Are you under 25 years of age?")== JOptionPane.YES_OPTION)
    			{
    			//If they are, add $10 to their package
    				total = total + 10.00;
    				yourAge = "Yes";
    			}
    			else
    			{
    			//If they aren't under 25, continue with the program
    				yourAge = "No";
    			}
    			//Prompt the user to see if they have any speeding tickets
    			if(JOptionPane.showConfirmDialog(null,"Do you have any speeding tickets?")== JOptionPane.YES_OPTION)
    			{
    			//If they have speeding tickets, prompt them for how many they have, then take the number of tickets and multiple it by 5 and add it to the total
    				input = JOptionPane.showInputDialog(null, "How many tickets do you have?");
    				numOfTickets = Integer.parseInt(input);
    				total = total + (numOfTickets *5);
    			}
    			else
    			{
    			//If they have no tickets, set tickets to 0
    				numOfTickets = 0;
    			}
    			//Prompt the user to see if they have other car insurance
    			if(JOptionPane.showConfirmDialog(null,"Do you currently hold any other car insurance?")== JOptionPane.YES_OPTION)
    			{
    			//If they do, subtract $10 from their total
    				total = total - 10.00;
    				otherInsurance = "Yes";
    			}
    			else 
    			{
    			//If they have no other insurances, add $20 to their total
    				total = total + 20.00;
    				otherInsurance = "No";
    			}
    			//Multiple the total with 6(for the 6 month period) to get their final quote price
    			finalQuote = total * 6;
    			custNum++;//This will add 1 everytime a customer finishes
    			
    			//The following command below will display a summary all of the information they have entered previously and their final quote price
    			JOptionPane.showMessageDialog(null, "First Name: " + firstName + "\nLast Name: " + lastName + "\nLicense number: " + licenseNum + "\nInsurance type: " + insuranceType +
    			"\nUnder 25 years old?: " + yourAge + "\nNumber of speeding tickets: " + numOfTickets + "\nOther Insurance: " +  otherInsurance +
    			"\nTotal: $" + total + "\n6 month quote: $" + finalQuote);
    		}while (custNum <= 10);//Allows up to 10 customers to use the program
    	}
    }
    Last edited by pbrockway2; 11-17-2011 at 07:28 AM. Reason: code tags corrected

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Need help with Java program

    When you post code use the "code" tags rather than "quote". You can either type [code] at the start of the code and [/code] at the end, or use the # button which is to the right of the speech bubble button (on the "advanced" message composition pane only)

Similar Threads

  1. Call one Java Program from another Java Program
    By rajpalparyani in forum New To Java
    Replies: 3
    Last Post: 02-14-2011, 05:13 AM
  2. Replies: 4
    Last Post: 07-30-2010, 01:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 06:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •