订购系统

时间:2014-04-15 23:23:59

标签: java

我被困在这个项目的最后一部分。至少在我需要它的水平上,我已经完成了所有其他部分的工作。我将它设置为客户可以对其产品进行选择的位置,例如:类型,颜色,材料以及他们希望订购的商品的数量。现在我必须为物品分配价格。我不希望类型或颜色仅影响材料的价格,因此它应该相当简单。一旦他们选择纸张或纸板价格,他们将获得输出消息及其总成本(包括税)。我已经看了一些东西,因为我甚至找不到一个起点来分配所选项目材料的成本。下面是我写的代码,我不确定如何添加我需要的部分。任何帮助都会很棒。

package pcos;

import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;

public class pcos {
    public static void main(String[] args)throws Exception {
        String openingMsg, nameInputMsg, customerName, nameOutputMsg,
            returnInputMsg, customerReturn, orderTypeMsg, customerType, returnOutputMsg, finalTotalAmt,
            orderColorMsg, customerColor, orderCoatMsg, customerCoat, itemAmountMsg, customerItemAmount, itemAmtOutptMsg, frameOutputMsg, greetingOutputMsg, outputMsg;
        //display opening message 
        openingMsg = "*** Welcome to the Diamond Playing Cards Online Ordering System ***\n"; 
        JOptionPane.showMessageDialog(null, openingMsg);
        //get required input using dialog boxes
        nameInputMsg = "Enter your name: "; 
        customerName = getstringInput( nameInputMsg );
        returnInputMsg = "Have you ordered from us before (yes or no)? "; 
        customerReturn = getstringInput( returnInputMsg );
        orderTypeMsg = "What type of deck to you need Standard, Uno, Rook?";
        customerType = getstringInput(orderTypeMsg);
        orderColorMsg = "What color do you want your deck to be Red, Green, Blue, or Black?";
        customerColor = getstringInput(orderColorMsg);
        orderCoatMsg = "Do you prefer a clear laminate or a plastic coating on your cards?" + ".\n" + "Card coating     (Type)           Cost" + ".\n" + "                      Laminated         $2.00" + ".\n" +       "                     Plastic Coating     $2.50" ;
        customerCoat = getstringInput(orderCoatMsg);
        itemAmountMsg = "Please enter the number of your selected item you wish to purchase?" ;
        customerItemAmount =getstringInput(itemAmountMsg);

        //build output strings 
        nameOutputMsg = "Welcome " + customerName + ".\n";
        returnOutputMsg = "Your return customer status is " + customerReturn + ".\n";
        itemAmtOutptMsg= "You have slected to purchase " + customerItemAmount + ' ' + customerColor + ' ' + customerType + "(s)" +  " with " + customerCoat + " coating(s).\n"; ;
        finalTotalAmt= "The total cost of your purchase after taxes will be" + "\n";
        greetingOutputMsg = "     Thank you for visiting Diamond Playing Cards Online!" + "\n";
        //create output string 
        outputMsg = nameOutputMsg + returnOutputMsg + itemAmtOutptMsg + finalTotalAmt +greetingOutputMsg;
        //display output message
        JOptionPane.showMessageDialog( null, outputMsg );
        System.exit(0);
    }

    private static String getstringInput(String prompt){
        int count = 0;
        String input;
        input = JOptionPane.showInputDialog( prompt );
        while ((input != null && input.length() == 0) && (count <2)){
            input = JOptionPane.showInputDialog("No input was detected. \n" + prompt);
            count++;
        }
        if (count==2){
            JOptionPane.showMessageDialog(null, " You did not make an entry please try again later when you are ready to order.  ORDER TERMINATING.");
            System.exit(0);
        }
        return input;
    }
}

随着对代码的更新,它看起来像这样(虽然我决定使用double而不是float来保持更准确。我仍然有使用我分配的价格的问题。我知道我必须创建一些表达式的类型,但我无法弄清楚如何从客户输入* salesTax *中获取的整数,他们选择的类型。我然后我必须让它显示回用户,如在outputMSG,但应该如果我能弄清楚表达方式,那就够容易了。

package pcos;

import java.io.*;
import java.util.*;
import java.lang.*;

import javax.swing.*;

public class pcos {
    public static void main(String[] args)throws Exception {
        String openingMsg, nameInputMsg, customerName, nameOutputMsg,
            returnInputMsg, customerReturn, orderTypeMsg, customerType, returnOutputMsg, finalTotalAmt,
            orderColorMsg, customerColor, orderCoatMsg, customerCoat, itemAmountMsg, customerItemAmount, itemAmtOutptMsg, frameOutputMsg, greetingOutputMsg, outputMsg;
        //display opening message 
        openingMsg = "*** Welcome to the Diamond Playing Cards Online Ordering System ***\n"; 
        JOptionPane.showMessageDialog(null, openingMsg);
        //get required input using dialog boxes
        nameInputMsg = "Enter your name: "; 
        customerName = getstringInput( nameInputMsg );
        returnInputMsg = "Have you ordered from us before (yes or no)? "; 
        customerReturn = getstringInput( returnInputMsg );
        orderTypeMsg = "What type of deck to you need Standard, Uno, Rook?";
        customerType = getstringInput(orderTypeMsg);
        orderColorMsg = "What color do you want your deck to be Red, Green, Blue, or Black?";
        customerColor = getstringInput(orderColorMsg);
        orderCoatMsg = "Do you prefer a clear laminate or a plastic coating on your cards?" + ".\n" + "Card coating     (Type)           Cost" + ".\n" + "                      Laminated         $2.00" + ".\n" +       "                     Plastic Coating     $2.50" ;
        customerCoat = getstringInput(orderCoatMsg);
        itemAmountMsg = "Please enter the number of your selected item you wish to purchase?" ;
        customerItemAmount =getstringInput(itemAmountMsg);
        Integer.parseInt(customerItemAmount);

        //build output strings 
        nameOutputMsg = "Welcome " + customerName + ".\n";
        returnOutputMsg = "Your return customer status is " + customerReturn + ".\n";
        itemAmtOutptMsg= "You have slected to purchase " + customerItemAmount + ' ' + customerColor + ' ' + customerType + "(s)" +  " with " + customerCoat + " coating(s).\n"; ;
        finalTotalAmt= "The total cost of your purchase after taxes will be" + "\n";
        greetingOutputMsg = "     Thank you for visiting Diamond Playing Cards Online!" + "\n";
        //create output string 
        outputMsg = nameOutputMsg + returnOutputMsg + itemAmtOutptMsg + finalTotalAmt +greetingOutputMsg;
        //display output message
        JOptionPane.showMessageDialog( null, outputMsg );
        System.exit(0);
    }

    public static double typeLaminatePrice = 2.00;
    public static double typePlasticPrice = 2.50;
    public static double salesTaxRate = 0.10;
    double price;         
    if (customerFrame.equalsIgnoreCase("Plastic")) {
        price = typePlasticPrice;
    }else if (customerFrame.equalsIgnoreCase("Laminate")) {
        price = typeLaminatePrice;}  
    private static String getstringInput(String prompt){
        int count = 0;
        String input;
        input = JOptionPane.showInputDialog( prompt );
        while ((input != null && input.length() == 0) && (count <2)){
            input = JOptionPane.showInputDialog("No input was detected. \n" + prompt);
            count++;
        }
        if (count==2){
            JOptionPane.showMessageDialog(null, " You did not make an entry please try again later when you are ready to order.  ORDER TERMINATING.");
            System.exit(0);
        }
        return input;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须使用比String更多的数据类型。在处理价格时,最佳基本类型为float,最佳对象为BigDecimal。为方便起见,我们可以使用浮点数:

您需要为这样的价格添加一些值:

private static final float TYPE_LAMINATE_PRICE = 10.0f;
private static final float TYPE_PLASTIC_PRICE = 5.0f;

然后,您需要通过执行以下操作来检测用户输入的内容:

float price;
if (customerCoat.equalsIgnoreCase("laminate") {
    price = TYPE_LAMINATE_PRICE;
} else if (customerCoat.equalsIgnoreCase("plastic") {
    price = TYPE_PLASTIC_PRICE;
}

您还需要检测所需的数量。 Integer.parseInt(String)会为你做这件事。

这应该让你开始,我相信你能够找到如何让你的工作如何在那里工作。