2012-12-04 118 views
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,但它最終會給我空串,因爲我想我正在創建一個新的三明治。我怎麼叫我已經做的三明治?

+3

百勝餐飲,三明治。你應該在'makeSandwich()'方法中將'sandwich'實例作爲參數傳遞給'writeOrderLine()',而不是三明治的各個組件。 – Vulcan

+0

你能澄清嗎?我以爲我已經發送我的三明治實例作爲參數OrderLine.writeOrderLine(name,sandwich.getBread(),sandwich.getMeat(),sandwich.getVegetables(),sandwich.getPrice()); –

+0

再次,這些是三明治的各種組成部分,而不是「三明治」本身。唯一的參數應該是「夾心三明治」。 – Vulcan

回答

0

在writeOrderLine功能還沒有初始化的三明治,但是經過夾層的所有屬性,要麼你能做到這一點通過以下方式

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(bread + "\t"); 
       out.print(meat + "\t"); 
       out.print(veg + "\t"); 
       out.println(price+ "\t"); 
       out.close(); 
      } 
      catch (IOException e) 
      { 
       System.out.println(e); 
      } 
     } 

,或者你可以這樣來做

調用這樣

OrderLine.writeOrderLine(name, sandwich); 

和改變功能

public static void writeOrderLine(String name, Sandwich sandwich) 
    { 
     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); 
     } 
    }