This program will use parallel arrays. Create an integer array named customerNum
ID: 3641747 • Letter: T
Question
This program will use parallel arrays. Create an integer array named customerNum containing the following numbers:1001, 1002, 1003, 1004, 1005, 1006, 1007,
1008, 1009, 1010, 1011, 1012, 1013, 1014,
1015, 1016, 1017, 1018, 1019, 1020
Create a String array named customerInfo with the following information:
"MORALES, BONITA P.O. BOX 651 EASTPOINT, FL 32328",
"THOMPSON, RYAN P.O. BOX 9835 SANTA MONICA, CA 90404",
"SMITH, LEILA P.O. BOX 66 TALLAHASSEE, FL 32306",
"PIERSON, THOMAS 69821 SOUTH AVENUE BOISE, ID 83707",
"GIRARD, CINDY P.O. BOX 851 SEATTLE, WA 98115",
"CRUZ, MESHIA 82 DIRT ROAD ALBANY, NY 12211",
"GIANA, TAMMY 9153 MAIN STREET AUSTIN, TX 78710",
"JONES, KENNETH P.O. BOX 137 CHEYENNE, WY 82003",
"PEREZ, JORGE P.O. BOX 8564 BURBANK, CA 91510",
"LUCAS, JAKE 114 EAST SAVANNAH ATLANTA, GA 30314",
"MCGOVERN, REESE P.O. BOX 18 CHICAGO, IL 60606",
"MCKENZIE, WILLIAM P.O. BOX 971 BOSTON, MA 02110",
"NGUYEN, NICHOLAS 357 WHITE EAGLE AVE. CLERMONT, FL 34711",
"LEE, JASMINE P.O. BOX 2947 CODY, WY 82414",
"SCHELL, STEVE P.O. BOX 677 MIAMI, FL 33111",
"DAUM, MICHELL 9851231 LONG ROAD BURBANK, CA 91508",
"NELSON, BECCA P.O. BOX 563 KALMAZOO, MI 49006",
"MONTIASA, GREG 1008 GRAND AVENUE MACON, GA 31206",
"SMITH, JENNIFER P.O. BOX 1151 MORRISTOWN, NJ 07962",
"FALAH, KENNETH P.O. BOX 335 TRENTON, NJ 08607"};
Create an integer array named bookNum containing the following numbers:
101, 102, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114
Create a double array named bookPrice with the following information:
18.75, 14.20, 37.80, 31.40, 12.50, 47.25, 21.80,
37.90, 48.00, 19.00, 5.32, 17.85, 15.40, 21.85
These arrays can be created at the program level, declared as static and created before the main method. Now they are available to all methods in this program and do not have to be passed to the methods.
Write a program that prompts a user for a customer number. Pass the customer number to a method with the signature: boolean validateCustNum(int custNum). The validateCustNum method will search the cutomerNum array for the customer’s number and if found will return true. If the customer number is not valid continue to prompt until a valid customer number is entered.
Now prompt the user for the book number and quantity. Pass the book number to a method, the module’s signature is: num getPrice(num bookNumber). The getPrice() module accepts a book number and returns the price of the item, if found, otherwise, it returns 0. If the book number is not found continue to prompt until a valid book number is entered.
Multiply the price by the quantity ordered, giving the total due. Pass the customer number and the calculated price to a method whose signature is: printBill(num custNum, num price). This method determines the customer’s name and address by using the customer ID number, and calculates and prints the final bill, including tax, using the price.
Explanation / Answer
import java.text.DecimalFormat;
import java.util.Scanner;
public class BookArray {
private static int customerNum[] = {1001, 1002, 1003, 1004, 1005, 1006, 1007,
1008, 1009, 1010, 1011, 1012, 1013, 1014,
1015, 1016, 1017, 1018, 1019, 1020};
private static String customerInfo[] = {"MORALES, BONITA P.O. BOX 651 EASTPOINT, FL 32328",
"THOMPSON, RYAN P.O. BOX 9835 SANTA MONICA, CA 90404",
"SMITH, LEILA P.O. BOX 66 TALLAHASSEE, FL 32306",
"PIERSON, THOMAS 69821 SOUTH AVENUE BOISE, ID 83707",
"GIRARD, CINDY P.O. BOX 851 SEATTLE, WA 98115",
"CRUZ, MESHIA 82 DIRT ROAD ALBANY, NY 12211",
"GIANA, TAMMY 9153 MAIN STREET AUSTIN, TX 78710",
"JONES, KENNETH P.O. BOX 137 CHEYENNE, WY 82003",
"PEREZ, JORGE P.O. BOX 8564 BURBANK, CA 91510",
"LUCAS, JAKE 114 EAST SAVANNAH ATLANTA, GA 30314",
"MCGOVERN, REESE P.O. BOX 18 CHICAGO, IL 60606",
"MCKENZIE, WILLIAM P.O. BOX 971 BOSTON, MA 02110",
"NGUYEN, NICHOLAS 357 WHITE EAGLE AVE. CLERMONT, FL 34711",
"LEE, JASMINE P.O. BOX 2947 CODY, WY 82414",
"SCHELL, STEVE P.O. BOX 677 MIAMI, FL 33111",
"DAUM, MICHELL 9851231 LONG ROAD BURBANK, CA 91508",
"NELSON, BECCA P.O. BOX 563 KALMAZOO, MI 49006",
"MONTIASA, GREG 1008 GRAND AVENUE MACON, GA 31206",
"SMITH, JENNIFER P.O. BOX 1151 MORRISTOWN, NJ 07962",
"FALAH, KENNETH P.O. BOX 335 TRENTON, NJ 08607"};
private static int bookNum[] = {101, 102, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114};
private static double bookPrice[] = {18.75, 14.20, 37.80, 31.40, 12.50, 47.25, 21.80,
37.90, 48.00, 19.00, 5.32, 17.85, 15.40, 21.85};
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int custNum = 0;
System.out.print("Enter a customer number: ");
custNum = in.nextInt();
while(!validateCustNum(custNum)) {
System.out.println("Not a valid customer number.");
System.out.print("Enter a customer number: ");
custNum = in.nextInt();
}
int bookNumber = 0;
int quantity = -1;
System.out.print("Enter a book number: ");
bookNumber = in.nextInt();
double price = getPrice(bookNumber);
while(price <= 0) {
System.out.println("Not a valid book number.");
System.out.print("Enter a book number: ");
bookNumber = in.nextInt();
price = getPrice(bookNumber);
}
while(quantity < 0) {
System.out.print("Enter a quantity: ");
quantity = in.nextInt();
}
double total = price * quantity;
printBill(custNum, total);
}
private static void printBill(int custNum, double subtotal) {
int index = custNum - 1001;
String customer = customerInfo[index];
double total = subtotal * 1.08875;
System.out.println("Bill for order");
System.out.println(customer + ": ");
DecimalFormat dFormat = new DecimalFormat("0.00");
System.out.println("Total: $" + dFormat.format(total));
}
private static double getPrice(int bookNumber) {
int book = -1;
double ret = 0;
for(int i = 0; i < bookNum.length; ++i) {
if(bookNumber == bookNum[i]) {
book = i;
}
}
if(book >= 0) {
ret = bookPrice[book];
}
return ret;
}
private static boolean validateCustNum(int custNum) {
boolean ret = false;
for(int i = 0; i < customerNum.length; ++i) {
if(customerNum[i] == custNum) {
ret = true;
}
}
return ret;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.