我正在为一个假装的体育馆建立预订系统,我为客户开设了一堂课,为体育课开设了一堂课,还有一个课程经理将课程汇总到HashMap
中。
每个会话都有一个哈希图来收集预订的客户。我有一个带有GUI的大师班,可以执行各种功能。我希望能够打开一个包含JPanel
的新JFrame
/ Jlist
,这样,当选择特定会话时,它将显示已预订的客户,但是我一直找不到相关的方法。
以下相关代码:
客户分类
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;
}
}
会话课程
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);
}
}
SessionManager类
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 ++;
}
}
我确定这确实是简单的东西,但是目前看来我还不了解,任何提示或指示都将不胜感激,谢谢!
答案 0 :(得分:0)
您可以从JList
创建一个HashMap
,如下面的示例所示。
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;
}
}