224k views
3 votes
Write a program called DeliveryCharges for the package delivery service. The program should use an array that holds the 10 zip codes of areas to which the company makes deliveries. (Note that this array has been created for your and does not need to be changed.) A Parallel array has also been creating contating 10 delivery charges that differ for each zip code. Prompt a user to enter a zip code, and then display either a message indicating the price of delivery to that zip code or a message indicating that the company does not deliver to the requested zip code.

User Navin Leon
by
5.4k points

1 Answer

4 votes

Answer:

import java.util.Scanner;

public class DeliveryCharges

{

public static void main(String[] args) {

String[] zips = {"01234", "11234", "21234", "31234", "41234", "51234", "61234", "71234", "81234", "91234"};

double[] prices = {2.2, 1.0, 3.6, 6, 9, 7.1, 0.8, 4.7, 3.3, 5.2};

int index = -1;

Scanner ob = new Scanner(System.in);

System.out.print("Enter the zip code for your delivery: ");

String zip = ob.next();

for (int i=0; i<10; i++) {

if (zip.equals(zips[i])) {

index = i;

}

}

if (index!= -1)

System.out.println("Delivery charge to " + zips[index] + " is: " + prices[index]);

else

System.out.println("No delivery to " + zip);

}

}

Step-by-step explanation:

Initialize the zips and prices

Initialize index that represents the index of the zip code. If the entered zip code is in the array

Ask the user to enter the zip code

Create a for loop that iterates through the zips. If the entered zip is in the zips, set its index to index

When the loop is done, check if the index. If the index is not -1, that means zip code was found in the zips array, print the corresponding price from the price array. Otherwise, print no delivery for the entered zip code

User Antoine Viscardi
by
6.0k points