我正在尝试创建一个日志程序来记录当天的Boats,我试图让用户在键入" log"时查看当前日志,但是一旦他们查看日志程序重新开始,日志变为空,直到他们进入新船。是否有任何方式使它,即使他们检查日志以前记录的船只显示和一旦他们在检查日志后添加新船...它将该船添加到以前的日志?
日志类
public class Log {
//creates an array list for boats to be stored
private ArrayList<Boat> log;
public Log() {
log = new ArrayList<Boat>();
}
//method to add boats to the list
public void addBoat(Boat a) {
log.add(a);
}
//method to view what's on the array list or boat log
public void viewLog() {
// print headings
System.out.printf("%8s%10s %8s%10s %8s%n", "Boat", "Position","Gas","Ice","Price");
System.out.printf("%8s%10s %8s%10s %8s%n", "========","========","========","========","========");
// for each boat in this log
for (int i = 0; i < log.size(); i++) {
Boat current = log.get(i); // get next boat
// print boat info and where it is.
System.out.printf("%8s%10s%10s%10s $%6s%n%n", current.getName().toUpperCase(),current.getPosition().toUpperCase(),current.getGasAmount().toUpperCase(),current.getIceAmount().toUpperCase(),current.getPrice());
}
}
}
主要课程
import java.io.IOException;
import javax.swing.JOptionPane;
public class LogTest {
public static void main(String[] args) throws IOException {
//boolean variable in order to pass control
//back to the top of the program
//instance variables to pass into constructor
boolean run = true;
String boatName;
String position;
String gas;
String ice;
while (run) {
//log object to store when a boat is entered
Log dailyLog = new Log();
//options to add a boat to the array list
//or to view the current array list
//or to remove a boat from the array list (this method is not made yet)
String boatLog = JOptionPane.showInputDialog("To add a boat type 'Add'\n"
+ "To view todays log type 'Log'\n"
+ "To remove a boat type 'Remove'\n" + "Hit 'Cancel' to quit");
//if user enters nothing then show the Error message
//and re-display menu screen
while (boatLog.isEmpty()) {
JOptionPane.showMessageDialog(null, "You have not entered anything. "
+ "Try again", "ERROR!", JOptionPane.INFORMATION_MESSAGE);
boatLog = JOptionPane.showInputDialog("To add a boat type 'Add'\n"
+ "To view todays log type 'Log'\n"
+ "To remove a boat type 'Remove'\n" + "Hit 'Cancel' to quit");
}
//if use enters "add" then ask for the boats name
//where they want it to go (water or wash)
//if it needs gas and ice
while (boatLog.equalsIgnoreCase("Add")) {
//variables for boat object constructer
boatName = JOptionPane.showInputDialog("Enter Boat Id");
position = JOptionPane.showInputDialog("If this boat is "
+ "going to the water enter 'Water'\n" + "If this boat is"
+ "going to the wash rack enter 'Wash'");
gas = JOptionPane.showInputDialog("Is this boat getting gas?");
ice = JOptionPane.showInputDialog("Is this boat getting ice?");
//boat object constructer
Boat addBoat = new Boat(boatName, position, gas, ice);
//methods to confirm position and how much gas & ice the boat wants
addBoat.position();
addBoat.gasAmount();
addBoat.iceAmount();
//displays the options they choose for their boat
JOptionPane.showMessageDialog(null,
String.format("Boat: %s\nGoing to: %s\nAmount of Gas (Gallons): %s\nAmount of Ice: %s\n", addBoat.getName().toUpperCase(), addBoat.getPosition().toUpperCase(), addBoat.getGasAmount().toUpperCase(), addBoat.getIceAmount().toUpperCase()),
"New Boat", JOptionPane.INFORMATION_MESSAGE);
//adds boat to array list
dailyLog.addBoat(addBoat);
//re-displays menu screen
boatLog = JOptionPane.showInputDialog("To add a boat type 'Add'\n"
+ "To view todays log type 'Log'\n"
+ "To remove a boat type 'Remove'\n" + "Hit 'Cancel' to quit");
}
//remove method (coming soon <3)
while (boatLog.equalsIgnoreCase("Remove")) {
}
//if user choose to view log then display items of the array
if (boatLog.equalsIgnoreCase("Log")) {
dailyLog.viewLog();
}
//if at all user hits cancel then whole loop stops and system exits
if (boatLog == null) {
run = false;
}
}
}
}
答案 0 :(得分:1)
您在while
循环的每次迭代中重新初始化日志。将此行移至循环之前。
Log dailyLog = new Log();