gpt4 book ai didi

java - 将数组对象存储到 CSV 文件中,并使用 GUI 的特定参数读取它们

转载 作者:行者123 更新时间:2023-12-01 09:10:26 25 4
gpt4 key购买 nike

作为作业的一部分,我必须将数组的对象存储在平面文件中,并在满足某些条件时检索它们。我可以很好地保存对象,但在检索它们时,我遇到了获取多个值的问题,我明白出了什么问题,但我正在努力寻找解决方案。这是正在发生的事情的概念。

按钮 10,A(代码中的 R1S10)是我的测试按钮,当我单击它时,它会创建一个事件,我将在下面显示。

This is my system, its a ticket booking system按钮 10A 的点击事件 -

private void R1S10ActionPerformed(java.awt.event.ActionEvent evt) {                                      

seats.add(seat1);


if (R1S10.getBackground().equals(Color.red) &&(IsSeatBooked().equals("true"))){
Component frame = null;
JOptionPane.showMessageDialog(frame, "Seat UnBooked");
seat1.setBooked("false");
seat1.setName("");
R1S10.setBackground(Color.yellow);
try {
reader();
writer();

//String booked = "true";
//Pass String booked into csv file
} catch (IOException ex) {
Logger.getLogger(SeatingPlan.class.getName()).log(Level.SEVERE, null, ex);
}

}
else{
Component frame = null;
String name = JOptionPane.showInputDialog(frame, "Please enter name of Customer booking");
if (name.isEmpty()) {
JOptionPane.showMessageDialog(frame, "No value entered");

} else if (name != null) {
seat1.setName(name);
seat1.setBooked("true");
R1S10.setBackground(Color.red);
JOptionPane.showMessageDialog(frame, "Your Booking has been placed");
try {
writer();
reader();
//String booked = "true";
//Pass String booked into csv file
} catch (IOException ex) {
Logger.getLogger(SeatingPlan.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

}

接下来是下面的屏幕 - When 10A is first pressed
结果 -

When booking is complete

当再次按下按钮时 -

UnBooked

我在此 SeatingPlan.java 中使用三种方法 - writer()、reader() 和 IsSeatBooked()。

座位安排 -

public class SeatingPlan extends javax.swing.JFrame {

/**
* Creates new form SeatingPlan
*/
String seatNo, name, bookedSeat;
FileWriter fileWriter = null;
List<Seat> seats = new ArrayList<Seat>();


//Seat Object Declaration

Seat seat1 = new Seat("R1S10","","false");
Seat seat2 = new Seat("R1S9", "", "false");
String fileName = "seat.csv";

作者-

 public void writer() throws IOException {
//Delimiter used in CSV file
final String NEW_LINE_SEPARATOR = "\n", COMMA_DELIMITER = ",";

//CSV file header
final String FILE_HEADER = "seatID,name,booked";

//fileName = System.getProperty("user.home") + "/seat.csv";
try {
fileWriter = new FileWriter(fileName);

//Write the CSV file header
fileWriter.append(FILE_HEADER.toString());

//Add a new line separator after the header
fileWriter.append(NEW_LINE_SEPARATOR);

//Write a new student object list to the CSV file
for (Seat seat : seats) {
fileWriter.append(String.valueOf(seat.getSeatID()));
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(seat.getName());
fileWriter.append(COMMA_DELIMITER);
fileWriter.append(seat.isBooked());
fileWriter.append(NEW_LINE_SEPARATOR);
}

System.out.println("CSV file was created successfully !!!");

} catch (Exception e) {
System.out.println("Error in CsvFileWriter !!!");
e.printStackTrace();
} finally {

fileWriter.flush();
fileWriter.close();

}
}

读者 -

public void reader() {
//Delimiter used in CSV file
final String COMMA_DELIMITER = ",";
//Student attributes index
final int SEAT_ID_IDX = 0;
final int SEAT_NAME_IDX = 1;
final int SEAT_BOOKED = 2;
//private static final int STUDENT_LNAME_IDX = 2;
BufferedReader fileReader = null;

try {

//Create a new list of student to be filled by CSV file data
List<Seat> seats = new ArrayList<>();

String line = "";

//Create the file reader
fileReader = new BufferedReader(new FileReader(fileName));

//Read the CSV file header to skip it
fileReader.readLine();

//Read the file line by line starting from the second line
while ((line = fileReader.readLine()) != null) {
//Get all tokens available in line
String[] tokens = line.split(COMMA_DELIMITER);
if (tokens.length > 0) {
//Create a new seat object and fill his data
Seat seat = new Seat(tokens[SEAT_ID_IDX],
tokens[SEAT_NAME_IDX], tokens[SEAT_BOOKED]);
seats.add(seat);
seatNo = tokens[SEAT_ID_IDX];
//System.out.println("Seat Number: " + seatNo);
bookedSeat = tokens[SEAT_BOOKED];
}
}

//Print the new student list
for (Seat seat : seats) {
System.out.println(seat.toString());
}
} catch (Exception e) {
System.out.println("Error in CsvFileReader !!!");
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("Error while closing fileReader !!!");
e.printStackTrace();
}
}

}//end reader

SeatingPlan - 如果我尝试让参数控制结果,但在选择多个座位时 IsBooked 会发生冲突。

 public SeatingPlan() throws IOException {
setVisible(true);
initComponents();
//reader();
ColourSectionGold();
ColourSectionBronze();
reader();

if(R1S10.getBackground().equals(Color.yellow) && (IsSeatBooked().equals("true"))){ R1S10.setBackground(Color.red);}
//if(R1S9.getBackground().equals(Color.yellow) && (IsSeatBooked().equals("true2"))){ R1S9.setBackground(Color.red);}
}

已预订座位 -

 public String IsSeatBooked(){
return bookedSeat;
}//end IsSeatBooked

我使用上面的方法作为参数来查看座位是否已预订,但是当单击新座位时,它会设置“bookedSeat”的整个值 - 这使得系统无法正常工作。我知道代码效率不是很高,但是如果我解释正确的话,是否有针对此问题的临时解决方案。

此外,我还将包括我的座位类(class) -

public class Seat {
private String seatID;
private String booked;
private String name;
private int price;

public Seat(String seatID,String name,String booked){
this.seatID = seatID;
this.booked = "";
this.name = name;
this.price = price;
}

public String getSeatID() {
return seatID;
}

public void setSeatID(String seatID) {
this.seatID = seatID;
}

public String isBooked() {
return booked;
}

public void setBooked(String booked) {
this.booked = booked;
}

public String getStatus(){
return booked;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setPrice() {
this.price = price;
}}//end class Seat

查看创建的 CSV 文件 -

CSV Outcome

我希望能够单击多个按钮并保存其状态,按钮 10 目前工作正常,但由于 IsBooked 一次只有一个值,因此会发生冲突。

如果您花时间查看此内容,我将不胜感激。任何建设性的批评都是有帮助的,任何想法都会很棒!

谢谢,稻田。

最佳答案

代码太多,无法准确了解您在做什么。

您可以创建一个 Properties 文件,而不是使用 csv 文件。 Properties文件将以以下形式存储数据:

key:data

因此,在您的情况下,键将是 id:A1、A2...,数据将是预订座位的人的姓名。

所以文件一开始是空的。创建 GUI 时,您将创建一个循环来检查每个 id 以查看是否在“属性”字段中找到条目。如果找到,则显示已占用的座位,否则该座位为空。

然后,每当您想预订座位时,只需使用 setProperty(...) 方法即可。

Properties 类具有 load(...)store(...) 方法。

因此,Properties 类允许您以最少的努力轻松管理平面文件数据库。

请注意,您永远不会有像 R1S10 这样的变量名称。这将需要 100 个带有 if/else 语句的不同变量。相反,您可以扩展 JButton 并将行和座位作为参数传递给按钮。然后,在按钮的 ActionListener 中,您可以访问行/座位信息来构建用作属性文件键的 ID。

编辑:

Couldn't quite make the loop that checks if the ID is in the properties file.

如果属性为 null,则海底为空。

import java.util.*;

public class Main
{
public static void main(String[] args)
{
Properties properties = new Properties();
properties.setProperty("A2", "Smith");
properties.setProperty("C3", "Jones");

String[] rows = { "A", "B", "C", "D" };
int seats = 4;

for (int row = 0; row < rows.length; row++)
{
for (int seat = 1; seat <= seats; seat++)
{
String key = rows[row] + seat;
String property = properties.getProperty( key );

System.out.println(key + " : " + property);
}
}


}
}

关于java - 将数组对象存储到 CSV 文件中,并使用 GUI 的特定参数读取它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40949880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com