Need some help with part C please: Part A:(Simple Classes) – Let’s start with 1
ID: 3852300 • Letter: N
Question
Need some help with part C please:
Part A:(Simple Classes) – Let’s start with 1 class: The Account class. Let’s build this class first, we will add the other classes as we go. Your class should include set and get methods, constructors, a display method and any other behaviors that you deem necessary. (Note: See Database provided to figure out what Properties you need for this class.)
Part B:(Inheritance) – Let’s now build 2 more classes: The Person class, and the Customer class. Build the Person class first and test it. The Person class should have the properties firstName, lastName, addr and email(all Strings). Then build the Customer class, and make it inherit from the Person class. Your classes should include set and get methods, constructors, a display method and any other behaviors that you deem necessary. (Note: See Database provided to figure out what Properties you need for the Customer class.)
Part C:(Containment)
Next build and Test an AccountList class. Properties: An array(or ArrayList) of Accounts and a Count. Constructors: only a default constructor is needed. Behaviors: addAccount(Account a) and display(). To test the AccountList class, in main(), instantiate an AccountList object and add maybe 2 Accounts to the AccountList, and then display() the AccountList.
Lastly, add an AccountList property to the Customer class. A Customer has a List of Accounts. The Customer’s display() function will also need to be modified to display() the Customer’s AccountList.
=======================================================================================================================================
Here's what I have so far:
public class Account{
//Properties
private int customerId;
private String customerPassword;
private String firstName;
private String lastName;
private String customerAddress;
private String customerEmail;
//Constructors
public Account(){
customerId = 0;
customerPassword = "";
firstName = "";
lastName = "";
customerAddress = "";
customerEmail = "";
}
public Account(int id, String pw, String fn, String ln, String addr, String em){
customerId = id;
customerPassword = pw;
firstName = fn;
lastName = ln;
customerAddress = addr;
customerEmail = em;
}
//Behaviors
public void setId(int id){
customerId = id;
}
public int getId(){
return customerId;
}
public void setPw(String pw){
customerPassword = pw;
}
public String getPw(){
return customerPassword;
}
public void setFn(String fn){
firstName = fn;
}
public String getFn(){
return firstName;
}
public void setLn(String ln){
lastName = ln;
}
public String getLn(){
return lastName;
}
public void setAddr(String addr){
customerAddress = addr;
}
public String getAddr(){
return customerAddress;
}
public void setEm(String em){
customerEmail = em;
}
public String getEm(){
return customerEmail;
}
public void display(){
System.out.println("Customer ID: " + getId());
System.out.println("Customer Password: " + getPw());
System.out.println("Customer First Name: " + getFn());
System.out.println("Customer Last Name: " + getLn());
System.out.println("Customer Address: " + getAddr());
System.out.println("Customer E-mail: " + getEm());
}
public static void main(String[] args){
Account a1;
a1 = new Account();
a1.display();
}
}
=======================================================================================================================================
public class Person{
//Properties
private String firstName;
private String lastName;
private String address;
private String email;
//Constructors
public Person(){
firstName = "";
lastName = "";
address = "";
email = "";
}
public Person(String fn, String ln, String addr, String em){
firstName = fn;
lastName = ln;
address = addr;
email = em;
}
//Behaviors
public void setFn(String fn){
firstName = fn;
}
public String getFn(){
return firstName;
}
public void setLn(String ln){
lastName = ln;
}
public String getLn(){
return lastName;
}
public void setAddr(String addr){
address = addr;
}
public String getAddr(){
return address;
}
public void setEm(String em){
email = em;
}
public String getEm(){
return email;
}
public void display(){
System.out.println("First Name: " + getFn());
System.out.println("Last Name: " + getLn());
System.out.println("Address: " + getAddr());
System.out.println("E-mail: " + getEm());
}
public static void main(String[] args){
Person p1;
p1 = new Person();
p1.display();
}
}
=======================================================================================================================================
public class Customer extends Person{
//Properties
public int customerId;
public String password;
//Constructors
public Customer(){
super();
customerId = 0;
password = "";
}
public Customer(int id, String pw, String fn, String ln, String addr, String em){
super(fn, ln, addr, em);
customerId = id;
password = pw;
}
//Behaviors
public void setId(int id){
customerId = id;
}
public int getId(){
return customerId;
}
public void setPw(String pw){
password = pw;
}
public String getPw(){
return password;
}
public void display(){
System.out.println("Customer ID: " + getId());
System.out.println("Customer Password: " + getPw());
super.display();
}
public static void main(String[] args){
Customer c1;
c1 = new Customer();
c1.display();
}
}
Explanation / Answer
Find the code and answers below.
Account.java
public class Account {
private int customerId;
private String customerPassword;
private String firstName;
private String lastName;
private String customerAddress;
private String customerEmail;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerPassword() {
return customerPassword;
}
public void setCustomerPassword(String customerPassword) {
this.customerPassword = customerPassword;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public String getCustomerEmail() {
return customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public Account() {
customerId = 0;
customerPassword = "";
firstName = "";
lastName = "";
customerAddress = "";
customerEmail = "";
}
public Account(int customerId, String customerPassword, String firstName, String lastName, String customerAddress,
String customerEmail) {
super();
this.customerId = customerId;
this.customerPassword = customerPassword;
this.firstName = firstName;
this.lastName = lastName;
this.customerAddress = customerAddress;
this.customerEmail = customerEmail;
}
@Override
public String toString() {
return "Account [customerId=" + customerId + ", customerPassword=" + customerPassword + ", firstName="
+ firstName + ", lastName=" + lastName + ", customerAddress=" + customerAddress + ", customerEmail="
+ customerEmail + "]";
}
}
Person.java
public class Person {
private String firstName;
private String lastName;
private String address;
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Customer.java
import java.util.List;
public class Customer extends Person {
private int customerId;
private String password;
private List<Account> accountList;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Customer(){
super();
customerId = 0;
password = "";
}
public Customer(int customerId, String password) {
super();
this.customerId = customerId;
this.password = password;
}
public List<Account> getAccountList() {
return accountList;
}
public void setAccountList(List<Account> accountList) {
this.accountList = accountList;
}
@Override
public String toString() {
return "Customer [customerId=" + customerId + ", password=" + password + ", accountList=" + accountList
+ ", getCustomerId()=" + getCustomerId() + ", getPassword()=" + getPassword() + ", getAccountList()="
+ getAccountList() + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName()
+ ", getAddress()=" + getAddress() + ", getEmail()=" + getEmail() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
AccountList.java
import java.util.ArrayList;
import java.util.List;
public class AccountList {
private List<Account> accountList = new ArrayList<Account>();
private int count;
public List<Account> getAccountList() {
return accountList;
}
public void setAccountList(List<Account> accountList) {
this.accountList = accountList;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public AccountList() {
super();
}
public void addAccount(Account a){
accountList.add(a);
}
@Override
public String toString() {
return "AccountList [accountList=" + accountList + ", count=" + count + "]";
}
public static void main(String[] args) {
AccountList obj = new AccountList();
Account a = new Account();
a.setCustomerAddress("chennai");
a.setCustomerId(45);
a.setCustomerEmail("abc@gm.com");
a.setCustomerPassword("abc@123");
a.setFirstName("John");
a.setLastName("Paul");
obj.addAccount(a);
Account b = new Account();
b.setCustomerAddress("bangalore");
b.setCustomerId(44);
b.setCustomerEmail("xyz@gm.com");
b.setCustomerPassword("xyz@123");
b.setFirstName("nivin");
b.setLastName("Paul");
obj.addAccount(b);
System.out.println(obj.toString());
}
}
OUTPUT:
AccountList [accountList=[Account [customerId=45, customerPassword=abc@123, firstName=John, lastName=Paul, customerAddress=chennai, customerEmail=abc@gm.com], Account [customerId=44, customerPassword=xyz@123, firstName=nivin, lastName=Paul, customerAddress=bangalore, customerEmail=xyz@gm.com]], count=0]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.