2012-12-04 73 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); 
     } 
    }