In: Computer Science
Design a class named Person with fields for holding a person's
name, address, and telephone number(all as Strings). Write
a constructor that initializes all of these values, and mutator and
accessor methods for every field.
Next, design a class named Customer, which inherits from the Person
class. The Customer class should have a String field for the
customer number and a boolean field indicating whether the customer
wishes to be on a mailing list. Write a constructor that
initializes these values and the appropriate mutator and accessor
methods for the class's fields.
Demonstrate the Customer class in a program that prompts the user
to enter values for the customer's name, address, phone number,
and customer number, and then asks the user whether or not the
customer wants to receive mail. Use this information to create a
customer object and then print its information.
Put all of your classes in the same file. To do this, do not
declare them public.
Instead, simply write:
class Person { ... }
class Customer { ... }
Sample Run
java Driver
Enter·name·of·customer:Julia·Stevens↵
Enter·address·of·customer:77·Massachusetts·Ave·Cambridge,·MA·02139↵
Enter·phone·number·of·customer:617-777-7777↵
Enter·customer·number:928734502↵
Enter·yes/no·--·does·the·customer·want·to·recieve·mail?:no↵
↵
Customer:·↵
Name:·Julia·Stevens↵
Address:·77·Massachusetts·Ave·Cambridge,·MA·02139↵
Phone·Number:·617-777-7777↵
Customer·Number:·928734502↵
Recieve·Mail?:·false↵
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package customerdemo;
import java.util.*;
/**
*
* @author Addy
*/
class Person {
private String Name;
private String phoneNumber;
private String address;
//default constructor
public Person()
{
}
//parameterize constructor
public Person (String n, String pn, String ad)
{
Name = n;
phoneNumber = pn;
address = ad;
}
//mutators
public void setName(String n)
{
Name = n;
}
public void setNumber(String pn)
{
phoneNumber = pn;
}
public void setAddress(String ad)
{
address = ad;
}
//accessors
public String getName()
{
return Name;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public String getAddress()
{
return address;
}
}
class Customer extends Person { //person class inherited
private String customerID;
private boolean eMail;
//default constructor
public Customer()
{
}
// parameterize constructor
public Customer (String cn, boolean em)
{
customerID = cn;
eMail = em;
}
//mutators
public void setCustomerNumber(String customerNumber)
{
this.customerID =
customerNumber;
}
public void seteMail(boolean eMail) {
this.eMail = eMail;
}
//accessors
public String getCustomerNumber() {
return customerID;
}
public boolean geteMail() {
return eMail;
}
}
public class CustomerDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Customer customer; //object of customer
Scanner sc= new Scanner(System.in); //scanner for input
String name,id,number,address;
boolean email=false;
String temp;
//input
System.out.print("Enter Customer Name: ");
name=sc.nextLine();
System.out.print("Enter Customer ID: ");
id=sc.nextLine();
System.out.print("Enter Customer Number: ");
number=sc.nextLine();
System.out.print("Enter Customer Address: ");
address=sc.nextLine();
System.out.print("Enter Yes/No if you want to recieve emails or
not: ");
temp=sc.nextLine();
if((temp.equals("Yes")) ||(temp.equals("YES")) ){ //if yes email is
ture
email=true;
}
else if((temp.equals("No")) ||(temp.equals("no"))){ //if no email
is false
email =false;
}
else{
System.out.println("Invalid choice email set to NO by
default");
}
//setting data
customer = new Customer(id,email);
customer.setName(name);
customer.setNumber(number);
customer.setAddress(address);
//output
System.out.println("Name: "+customer.getName());
System.out.println("Address: "+customer.getAddress());
System.out.println("Phone Number:
"+customer.getPhoneNumber());
System.out.println("Customer Number:
"+customer.getCustomerNumber());
System.out.println("Recieve Mail: "+customer.geteMail());
System.out.println();
}
}
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP