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の前にこの行「sandwich = new Sandwich()」を追加すると機能しますが、新しいサンドイッチを作成していると思われるため、空の文字列が返されます. すでに作ったサンドイッチをどのように呼び出すことができますか?

4

1 に答える 1

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);
       }
    }
于 2012-12-04T05:03:47.847 に答える