从 HashMap 创建 JList

Creating JList from HashMap

我正在为假装的健身房建立一个预订系统,我有一个 class 用于客户,一个用于健身房课程,还有一个课程管理器将课程收集到一起 HashMap.

每个会话都有一个哈希映射来收集预订的客户。我有一个大师 class,带有 GUI 来执行各种功能。我希望能够打开一个包含 Jlist 的新 JPanel/JFrame,以便在选择特定会话时显示已预订的客户,但是我一直没找到相关的方法。

相关代码如下:

客户Class

public class Customer {

private final String name;
private final String payMethod;
public final UUID uniqueId;

/*
* Constructor for me.davehargest.weekendfitness.customer.struct.Customer
*
* @param String name    The customers name
* @param int id         The sequential ID Reference of the customer
*/
public Customer(String name, UUID uniqueId, String payMethod) {
    this.name = name;
            this.uniqueId = uniqueId;
    this.payMethod = payMethod;
}
}

会话 Class

public class Session {

private int sessionId;
private String sessionName;
    private double price; // Cost of the session per Person
    private double totalEarnings; // Total Earnings of the Session (Number of People * @price
    private Date sessionDate; //Date that the session takes place
    private int classNumber = 1;
/*
* Generates a HashMap of the Customers that will be booked onto a class    
*/   
public Map <Integer, Customer> customers = new HashMap <>();

 /**
 * Session Constructor
 * Creates a new instance of a session
 * 
 * @param sessionId - An identification ID for the Exercise Session
 * @param sessionName - A description of the actual exercise i.e. "Yoga"
 * @param price - The cost to attend the session per person
 * @param sessionDate
 */

public Session(int sessionId, String sessionName, double price, Date sessionDate) 
{
    this.sessionId = sessionId;
this.sessionName = sessionName;
    this.price = price;
    this.sessionDate = sessionDate;
    totalEarnings = 0;
}

/*
* Method addCustomer
* 
* @param Customer - Adds a new Customer to the Exercise Session 
*/

public void addCustomer (Customer customer)
{
    if (customers.size() >= 20){
        System.out.println("Class is full");
    } else {
            customers.put(classNumber, customer); // Adds in a new Customer to the session ArrayList
            totalEarnings += price; // Adds the per person cost to total fees
            classNumber++;
            System.out.println("Added: " + classNumber + customer);
    }
}

会话管理器Class

public class SessionManager {

    /*
    * A HashMap of all of the different Sessions that WeekEnd Fitness Offers
    */
    public Map<Integer, Session> sessions = new HashMap<>();

    private int sId = 1;

    public SessionManager() {}

    public void addSession(String sessionName, double price, String seshDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat ("dd-MM-yyyy HH:mm");
        sessionDate = format.parse(seshDate);
        this.sessions.put(this.sId, new Session(sId, sessionName, price, sessionDate));
        sId ++;
    }
 }

我敢肯定这真的很简单,但目前看来我还无法理解,如果有任何提示或指示,我们将不胜感激,谢谢!

您可以从 HashMap 创建一个 JList,就像我在下面的示例中展示的那样。

import javax.swing.JFrame;
import javax.swing.JList;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;

public class CustomerList {

  public static void main(String[] args) {
    // Sample data
    Map<Integer, Customer> customers = new HashMap<>();
    customers.put(1, new Customer("Kevin", UUID.randomUUID(), "Cash"));
    customers.put(2, new Customer("Sally", UUID.randomUUID(), "Credit card"));
    customers.put(3, new Customer("Kate", UUID.randomUUID(), "Cash"));

    Vector<ListItem> items = new Vector<>();
    for (Map.Entry<Integer, Customer> entry : customers.entrySet()) {
      items.add(new ListItem(entry.getKey(), entry.getValue()));
    }

    JList list = new JList(items);

    JFrame f = new JFrame("Customer List");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(list, BorderLayout.CENTER);
    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}

class ListItem {
  private int classNumber;
  private Customer customer;

  ListItem(int classNumber, Customer customer) {
    this.classNumber = classNumber;
    this.customer = customer;
  }

  @Override
  public String toString() {
    // You can change this to suite the presentation of a list item
    return classNumber + " - " + customer.name + " (" + customer.payMethod + ")";
  }
}

class Customer {

  final String name;
  final String payMethod;
  public final UUID uniqueId;

  public Customer(String name, UUID uniqueId, String payMethod) {
    this.name = name;
    this.uniqueId = uniqueId;
    this.payMethod = payMethod;
  }
}