0
public static void makeSandwich()
{
System.out.println("Enter First Name: ");
String name = Scanner.next();
double price = sandwich.getBreadPrice() + sandwich.getMeatPrice() + sandwich.getVegPrice();
sandwich.setPrice(price);
NumberFormat currency = NumberFormat.getCurrencyInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date) + " " + name + " " + sandwich.getBread() + " " + sandwich.getMeat() + " " + sandwich.getVegetables() + " " + currency.format(sandwich.getPrice()));
OrderLine.writeOrderLine(name, sandwich.getBread(), sandwich.getMeat(), sandwich.getVegetables(), sandwich.getPrice());
}
和我的繼承人爲orderline.app代碼寫入文本文件問題
public class OrderLine{
private static Sandwich sandwich = null;
public static void writeOrderLine(String name, String bread, String meat, String veg, double price)
{
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try
{
File productsFile = new File("orderline.txt");
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(productsFile, true)));
out.print(dateFormat.format(date) + "\t");
out.print(name + "\t");
out.print(sandwich.getBread() + "\t");
out.print(sandwich.getMeat() + "\t");
out.print(sandwich.getVegetables() + "\t");
out.println(sandwich.getPrice() + "\t");
out.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
它不會停止打印,但是當我加入這行「三明治=新的三明治()」前它的工作原理是orderline.java中的dateformat,但它最終會給我空串,因爲我想我正在創建一個新的三明治。我怎麼叫我已經做的三明治?
百勝餐飲,三明治。你應該在'makeSandwich()'方法中將'sandwich'實例作爲參數傳遞給'writeOrderLine()',而不是三明治的各個組件。 – Vulcan
你能澄清嗎?我以爲我已經發送我的三明治實例作爲參數OrderLine.writeOrderLine(name,sandwich.getBread(),sandwich.getMeat(),sandwich.getVegetables(),sandwich.getPrice()); –
再次,這些是三明治的各種組成部分,而不是「三明治」本身。唯一的參數應該是「夾心三明治」。 – Vulcan