我有一个Customers的ArrayList,我想将此列表的某些元素添加到另一个名为PayingCustomers
的类中,而不是所有的Customers。如何将ArrayList的特定值添加到类构造函数中?请忽略我有的一些评论行,这些评论行只是我在处理项目的其他元素。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;
}
}
主要:
/*
* 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);
}
}
最佳答案
为什么不在客户类中添加字段payingCustomer
?并将其设置为仅适用于付费客户。或使PayingCustomer扩展客户类,并为付费客户增加额外的功能。