thedafferg
thedafferg

Reputation: 99

Passing some values of an ArrayList of objects into another class java

I have an ArrayList of Customers, and I want to add some elements of this list but not all of the Customers into another class called PayingCustomers . How would I be able to add specific values of an ArrayList into my class constructor? Kindly ignore some of the commented lines I have which is just me working on other elements of my project.

Customer.java:

//
    //
    //  Generated by StarUML(tm) Java Add-In
    //
    //  @ Project : Untitled
    //  @ File Name : Customer.java
    //  @ Date : 21/04/2020
    //  @ Author : 
    //
    //

    package javaapplication1;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;


    public class Customer {

        private String name;
        private String email;
            private List <Customer> list;
        private Magazine magazines;


            public void SetName(String name){

                this.name = name;

            }
        public void SetEmail(String email){

                this.email = email;

            }
        public String GetName(){

                return name;

            }
        public String GetEmail(){

                return email;

            }

            public List<Customer> getList() {

                return list;

            }

            public void setList(List<Customer> list) {

                this.list = list;

            }

            public Customer(String name, String email, Magazine magazines){

                this.name = name;
                this.email = email;
                this.magazines = magazines;


            }

            public String toString() {

            return "\nName: " + name + " Email: " + email + "\nMagazine Details: " + magazines + "\n";


            }

    }

PayingCustomer.java:

//
    //
    //  Generated by StarUML(tm) Java Add-In
    //
    //  @ Project : Untitled
    //  @ File Name : PayingCustomer.java
    //  @ Date : 21/04/2020
    //  @ Author : 
    //
    //

    package javaapplication1;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;


    public class PayingCustomer{
        private String paymentmethod;
        private List <Customer> list;
            //private List <Associate> list2;

        public void SetPaymentMethod(String paymentmethod){

                this.paymentmethod = paymentmethod;

            };
        public String GetPaymentMethod(){

                return paymentmethod;

            };


            public PayingCustomer(List<Customer> list, String paymentmethod){

                this.list = list;
                this.paymentmethod = paymentmethod;
                //this.list2 = list2;

            }

            public String toString() {

                return "List of Paying Customers: " + list;

            }
    }

Main:

/*
 * 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 javaapplication1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JavaApplication1 {
    public static void main(String[] args) {


        List<Supplement> supplements = new ArrayList<Supplement>();

        supplements.add(new Supplement("Sports Illustrated Special", 4));
        supplements.add(new Supplement("Health and Nutrition", 2));
        supplements.add(new Supplement("Lifestyled", 5));
        supplements.add(new Supplement("Gamer's Update", 3));

        /*supplements2[0] = new Supplement("Horse Racing", 4);
        supplements2[1] = new Supplement("7 Seven", 2);
        supplements2[2] = new Supplement("Guru of the North", 5);
        supplements2[3] = new Supplement("Electrify", 3);*/


        Magazine magazineobj3 = new Magazine("The Wheels Special", 35, supplements);



        Magazine magazineobj = new Magazine("The Wheels Special", 35, supplements);
        Magazine magazineobj2 = new Magazine("Free your think", 15, supplements);

        List <Customer> customers = new ArrayList<Customer>();
        customers.add(new Customer("Morgan Freeman","[email protected]", magazineobj3));
        customers.add(new Customer("George Bush","[email protected]", magazineobj3));
        customers.add(new Customer("Jamie Carragher","[email protected]", magazineobj3));
        customers.add(new Customer("Sarah Williams","[email protected]", magazineobj3));
        customers.add(new Customer("Nathan Bledsoe","[email protected]", magazineobj3));
        customers.add(new Customer("Phillip Franklin","[email protected]", magazineobj3));

        List <PayingCustomer> payingCustomers = new ArrayList<PayingCustomer>();



        //System.out.println(magazineobj3);
        //System.out.println(magazineobj2);
        System.out.println(customers);

    }

}

Upvotes: 0

Views: 95

Answers (1)

2dor
2dor

Reputation: 1001

Why not add a field payingCustomer in the Customer class? And set that to true only for paying customers. Or make PayingCustomer to extend Customer class and add for paying customer extra functionalities.

Upvotes: 2

Related Questions