Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project 16-1: Customer Viewer The following code has already been given: CustomerApp.java package murach.ui; import murach.business.Customer; import murach.db.CustomerNotFoundException; import murach.db.CustomerDB; public class CustomerApp { public

image text in transcribed

Project 16-1: Customer Viewer

The following code has already been given:

CustomerApp.java

package murach.ui;

import murach.business.Customer;

import murach.db.CustomerNotFoundException;

import murach.db.CustomerDB;

public class CustomerApp {

public static void main(String args[]) {

// display a welcome message

System.out.println("Welcome to the Customer Viewer");

System.out.println();

// continue until choice isn't equal to "y" or "Y"

String choice = "y";

while (choice.equalsIgnoreCase("y")) {

int number = Console.getInt("Enter a customer number: ");

System.out.println();

try {

Customer c = CustomerDB.getCustomer(number);

System.out.println(c.getNameAndAddress());

} (CustomerNotFoundException e) {

System.out.println(e.getMessage());

}

System.out.println();

choice = Console.getString(

"Display another customer? (y): ");

System.out.println();

}

System.out.println("Bye!");

}

}

Console.java

package murach.ui;

import java.util.Scanner;

public class Console {

private static Scanner sc = new Scanner(System.in);

public static String getString(String prompt) {

String s = "";

boolean isValid = false;

while (!isValid) {

System.out.print(prompt);

s = sc.nextLine();

if (s.isEmpty()) {

System.out.println("Error! Required entry. Try again.");

} else {

isValid = true;

}

}

return s;

}

public static int getInt(String prompt) {

int i = 0;

boolean isValid = false;

while (!isValid) {

System.out.print(prompt);

if (sc.hasNextInt()) {

i = sc.nextInt();

isValid = true;

} else {

System.out.println("Error! Invalid integer. Try again.");

}

sc.nextLine(); // discard any other data entered on the line

}

return i;

}

public static int getInt(String prompt, int min, int max) {

int i = 0;

boolean isValid = false;

while (!isValid) {

i = getInt(prompt);

if (i

System.out.println(

"Error! Number must be greater than " + min + ".");

} else if (i >= max) {

System.out.println(

"Error! Number must be less than " + max + ".");

} else {

isValid = true;

}

}

return i;

}

public static double getDouble(String prompt) {

double d = 0;

boolean isValid = false;

while (!isValid) {

System.out.print(prompt);

if (sc.hasNextDouble()) {

d = sc.nextDouble();

isValid = true;

} else {

System.out.println("Error! Invalid number. Try again.");

}

sc.nextLine(); // discard any other data entered on the line

}

return d;

}

public static double getDouble(String prompt, double min, double max) {

double d = 0;

boolean isValid = false;

while (!isValid) {

d = getDouble(prompt);

if (d

System.out.println(

"Error! Number must be greater than " + min + ".");

} else if (d >= max) {

System.out.println(

"Error! Number must be less than " + max + ".");

} else {

isValid = true;

}

}

return d;

}

}

CustomerNotFoundException.java

package murach.db;

public class CustomerNotFoundException {

public CustomerNotFoundException() {

}

public CustomerNotFoundException(String message) {

super(message);

}

}

CustomerDB.java

package murach.db;

import murach.business.Customer;

public class CustomerDB {

public static Customer getCustomer(int number)

throws CustomerNotFoundException {

Customer c = new Customer();

switch (number) {

case 1000:

c.setName("Andrew Antosca");

c.setAddress("485 Duane Terrace");

c.setCity("Ann Arbor");

c.setState("MI");

c.setPostalCode("48108");

return c;

case 1001:

c.setName("Barbara White");

c.setAddress("3400 Richmond Parkway #3423");

c.setCity("Bristol");

c.setState("CT");

c.setPostalCode("06010");

return c;

case 1002:

c.setName("Karl Vang");

c.setAddress("327 Franklin Street");

c.setCity("Edina");

c.setState("MN");

c.setPostalCode("55435");

return c;

case 1003:

c.setName("Ronda Chavan");

c.setAddress("518 Comanche Dr.");

c.setCity("Greensboro");

c.setState("NC");

c.setPostalCode("27410");

return c;

case 1004:

c.setName("Sam Carol");

c.setAddress("9379 N. Street");

c.setCity("Long Beach");

c.setState("CA");

c.setPostalCode("90806");

return c;

default:

CustomerNotFoundException(

"No customer found for number " + number + ".");

}

}

}

Project 16-1: Customer Viewer Use a custom exception in an application that displays customer information. Console Custoer Viewer Enter a customer number 1003 Ronda Chavan 518 Commanche Dr Greensboro, NC 27410 Display another customer? (y): y Enter a customer number: 2439 No customer found for number 2439. Display another customer? (y): n Bye! Specifications . Your instructor should provide you with the Customer and CustomerDB classes that are used to get and display the information for a customer. Create a NoSuchCustomerException class that can store a message. If the getCustomer0 method of the CustomerDB class can't find a customer with the specified number, it should throw a CustomerNotFoundException with a message that says "No customer found for number XXXX .Modify the code for the application so it catches the CustomerNotFoundException

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Examine the diffuculty of adding a prposed swap rs , rt

Answered: 1 week ago