In: Statistics and Probability
1. You are to write a simple program with two classes. One controller class and a class to hold your object definition. (Similar to what we used in class) 2. Use a package in your project, the package name should be xxxprojectname. xxx is your initials taken from the first three characters of your Cal Poly email address. 3. Read in the following from the JOptionPane input window: a. Customer First Name b. Customer Last Name c. Customer Phone Number (example: 9093873744) d. Number of Vehicles to be manufactured (example: 5) e. Number of fuel tanks to be mounted on the vehicle. 4. Make sure the following is entered correctly, otherwise display an error message and close the program: California State Polytechnic University, Pomona Computer Information Systems Department Introduction to Object-Oriented Programming with Java a. The first name of the customer is not blank b. The last name of the customer is not blank c. The phone number of the customer is not blank and is 10 characters d. The number of vehicles is between 1 and 10 (includes 1 and 10) e. The number of fuel tanks can only be (2,4,8,10, 15,20) 5. Compute the cost for manufacturing vehicles using the price of $500.19 per vehicle 6. Compute the cost for fuel tanks using the price of $2.15 per fuel cell. 7. For this project each vehicle tank holds 12 Fuel Cells and vehicles can have more than one tank. 8. Use the following formulas to perform the calculations: Cost for manufacturing = number of vehicles * manufacturing price Cost for fuel tanks = number of vehicles * number of tanks * number of fuel cells per tank * price of fuel cell 9. Then you are to test the program using the data listed below. In the case of the first customer enter the data that has errors as shown in the screenshots below. The program should prompt you to reenter the data until the data is valid. First Name Last Name Phone Number of Vehicles Number of Tanks Homer Simpson 9093429871 5 8 Bart Simpson 9093429872 2 2 Lisa Simpson 9093429873 4 15 Marge Simpson 9093429874 3 4 Make sure that you check that the data is valid when the user enters it, Note in the example below how some data entered is incorrect the program then asks you to enter it again.
USE these as start up code
package project2startup;
public class VehicleApp {
public static void main(String[] args) {
}
}
package project2startup;
public class VehicleFactory {
//variables to hold our object properties
private String name;
private String phone;
private int nbrVehicles =0;
private int nbrTanks =0;
private double manufactureCost =0;
private double fuelTanksCost =0;
private double subtotal =0;
private double tax =0;
private double total = 0;
//constants to hold our pricing properties
private final double VEHICLE_PRICE = 500.19;
private final double FUELCELL_PRICE = 2.15;
//you have to create a constant and store the tax rate in it
public HayloFactory(String aName, String aPhone, int aNbrVehicles, int aNbrTanks){
//add necessary code in here
}
//Add methods to perform your calculations here
//for example you have to calculate the manufacturing cost and store it in manufactureCost
public void calcManufacturingCost(){
}
//for example you have to calculate the fuel tanks cost and store it in fuelTankCost
public void calcFuelTankCost(){
}
//add method to calculate the subtotal and store it in subtotal
public void calcSubtotal(){
}
//add method to calculate the tax and store it in tax
public void calcTax(){
}
//add method to calculate the total and store it in total
public void calcTotal(){
}
//Add a method to return a summary for this object here
public String getSummary(){
//you have to build your summary and store it in the summary variable
return null;
}
}
Short Summary:
Source Code:
CustomerVehicle.java File:
package xxxcustomervehicle;
import java.text.NumberFormat;
import java.util.Locale;
public class CustomerVehicle {
//Private members goee here...
private String firstName;
private String lastName;
private String phoneNumber;
private int numVehicle;
private int numFuelTank;
//Decalre all constants here..
private final static int[] VALID_FUEL_TANKS = {2, 4, 8, 10, 15, 20};
private final static double MANUFACTURE_BASE_COST = 500.19;
private final static double FUEL_CELL_COST = 2.15;
private final static int NO_FUEL_CELL_PER_TANK = 12;
//Default contructor, does nothing
public CustomerVehicle() {
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public int getNumVehicle() {
return numVehicle;
}
public int getNumFuelTank() {
return numFuelTank;
}
//Sets first name if it is not blank and returns true; otherwise, false
public boolean setFirstName(String firstName) {
if(firstName != null && firstName.length() != 0){
this.firstName = firstName;
return true;
}
return false;
}
//Sets last name if it is not blank and returns true; otherwise, false
public boolean setLastName(String lastName) {
if(lastName != null && lastName.length() != 0){
this.lastName = lastName;
return true;
}
return false;
}
//Sets phonenumber if it is not blank and 10 characters lenght and returns true;
//otherwise, false
public boolean setPhoneNumber(String phoneNumber) {
if(phoneNumber != null && phoneNumber.length() == 10){
this.phoneNumber = phoneNumber;
return true;
}
return false;
}
//Sets numVechicle if the parameter value is between 1 and 10 and return true;
//Otherwise false;
public boolean setNumVehicle(int numVehicle) {
if(numVehicle >= 1 && numVehicle <= 10){
this.numVehicle = numVehicle;
return true;
}
return false;
}
private boolean arrayContains(int[] array, int key){
for(int value: array){
if(value == key){
return true;
}
}
return false;
}
//Sets numVechicle if the parameter value is 2/4/8/10/15/20 and return true;
//Otherwise false;
public boolean setNumFuelTank(int numFuelTank) {
if (arrayContains(VALID_FUEL_TANKS, numFuelTank)){
this.numFuelTank = numFuelTank;
return true;
}
return false;
}
//Calculates and returns vehicle manufacturing cost
public double getManufacturingCost(){
double manuCostFuelTank = numVehicle * numFuelTank *
NO_FUEL_CELL_PER_TANK * FUEL_CELL_COST;
double manuVehicleCost = (MANUFACTURE_BASE_COST * numVehicle) + manuCostFuelTank;
return manuVehicleCost;
}
@Override
public String toString() {
return "Customer First Name = " + firstName
+ "\nCustomer Last Name = " + lastName
+ "\nPhone Number = " + phoneNumber
+ "\nNumber of Vehicle = " + numVehicle
+ "\nNumber of Fuel Tanks = " + numFuelTank
+ "\nTotal Manufacturing Cost = "
+ NumberFormat.getCurrencyInstance(new Locale("en", "US")).format(getManufacturingCost());
}
}
Controller.java File:
package xxxcustomervehicle;
import javax.swing.JOptionPane;
public class Controller {
//Displays input dialog with the given caption and returns the string entered by user
static String getUserInput(String label){
return JOptionPane.showInputDialog(null, label, "Input", JOptionPane.QUESTION_MESSAGE);
}
//Display the error message using JOptionPane.showMessageDialog
static void showErrorMessage(String errmsg){
JOptionPane.showMessageDialog(null, errmsg, "Error", JOptionPane.ERROR_MESSAGE);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CustomerVehicle newVehicle;
//Create new vehicle
newVehicle = new CustomerVehicle();
//Get First name until it is valid
do{
//Get first name from user
String firstName = getUserInput("Enter Customer First Name:");
if(newVehicle.setFirstName(firstName)){
break;
}
showErrorMessage("First name cannot be blank!");
}while(true);
//Get Last name until it is valid
do{
String lastName = getUserInput("Enter Customer Last Name:");
if(newVehicle.setLastName(lastName)){
break;
}
showErrorMessage("Last name cannot be blank!");
}while(true);
//Get phone number until it is valid
do{
String phoneNumber = getUserInput("Enter Customer Phone Number:");
if(newVehicle.setPhoneNumber(phoneNumber)){
break;
}
showErrorMessage("Invalid Phone number, please re-enter!");
}while(true);
//Get number of vehicle until it is valid
do{
String strVehicle = getUserInput("Number vehicles to be manufactured:");
//Check if user enters valid integer
int numVehicle = 0;
try{
numVehicle = Integer.parseInt(strVehicle);
} catch(Exception ex){
//do nothing
}
if(newVehicle.setNumVehicle(numVehicle)){
break;
}
showErrorMessage("Number vehicle should be between 1 and 10!");
}while(true);
//Get number of vehicle until it is valid
do{
String strFuelTank = getUserInput("Number of fuel tanks to be mounted on the vehicle:");
//Check if user enters valid integer
int numFuelTank = 0;
try{
numFuelTank = Integer.parseInt(strFuelTank);
} catch(Exception ex){
//do nothing
}
if(newVehicle.setNumFuelTank(numFuelTank)){
break;
}
showErrorMessage("Number of fuel tank can be 2, 4, 8, 10, 15 or 20!");
}while(true);
//Show the final output with Manufaturing cost
JOptionPane.showMessageDialog(null, newVehicle, "Output", JOptionPane.INFORMATION_MESSAGE);
}
}
Sample Run: