List<User> list=null
public CustomerBiz() {
list=new ArrayList<User>()
}
private void addName(String name) {
User user=new User(name)
list.add(user)
}
private void showNames() {
System.out.println("***************************************")
System.out.println("***************************************")
for (int i = 0 i < list.size() i++) {
System.out.print(list.get(i).getName()+" ")
if(i%4==0 && i>0)
System.out.println()
}
}
public static void main(String[] args) {
CustomerBiz customerBiz=new CustomerBiz()
doInput(customerBiz)
}
public static void doInput(CustomerBiz customerBiz){
String name=""
Scanner in=new Scanner(System.in)
System.out.println("请输入客户姓名:")
name=in.next()
if(name.equals("")) {
System.out.println("请重新输入客户姓名:")
name=in.next()
}
customerBiz.addName(name)
System.out.println("继续输入吗?y/n")
String tag=in.next()
if(tag.equals("y")){
doInput(customerBiz)
}else{
customerBiz.showNames()
}
}
}
class User{
String name
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public User(String name) {
this.name=name
}
}
public class Customer {
private String customerName
private int age
private boolean isValid
public Customer(String customerName, int age, boolean isValid) {
super()
this.customerName = customerName
this.age = age
this.isValid = isValid
}
public String getCustomerName() {
return customerName
}
public void setCustomerName(String customerName) {
this.customerName = customerName
}
public int getAge() {
return age
}
public void setAge(int age) {
this.age = age
}
public boolean isValid() {
return isValid
}
public void setValid(boolean isValid) {
this.isValid = isValid
}
}
import java.util.ArrayList
import java.util.List
public class CustomerBiz {
private List<Customer> customerList = new ArrayList<Customer>()
public void addCustomer(Customer customer){
customerList.add(customer)
}
public void showCustomers(){
Customer customer = null
System.out.println("客户信息:")
for(int i=0i<customerList.size()i++){
customer = customerList.get(i)
System.out.println(customer.getCustomerName()+" "+customer.getAge()+" "+customer.isValid())
}
}
public static void main(String[] args){
CustomerBiz customerBiz = new CustomerBiz()
customerBiz.addCustomer(new Customer("王一",30,false))
customerBiz.addCustomer(new Customer("郝强",19,true))
customerBiz.showCustomers()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)