7-11在线商店计划。需要修改?

时间:2015-06-07 15:17:27

标签: java constructor accessor modifier

我是一名非常业余的学生程序员,他制作了销售用户项目的代码,然后通过询问用户所处的状态来计算该项目的税。鉴于我缺乏编程经验,这是一个非常简单的代码。不幸的是,我错误地编写了这段代码而没有考虑过。

这个程序实际上是我班上的最后一个项目。因此,我必须满足要求,我目前遇到了困难。我已经完成了几乎所有的要求,但是注意到有些要求尚未完成。我缺少的一些要求是......:

  • 1个专业的建设者(我从来没有理解如何有效地做到这一点,并且怀疑这是否仍然可能,鉴于我的代码)
  • 1个访问器和2个修饰符(与构造函数相同,如何使用给定的代码正确执行此操作?)
  • 使用printf进行某种格式化输出(也许可以用来计算总价格?太糟糕了我还不完全理解它)

这是我的[效率极低]代码本身:

import java.util.Scanner;

public class Tax_Calculator {

public static void main(String[]args)
{
    Scanner in = new Scanner(System.in);

    //variables
    double taxPrice = 0;
    double totalPrice = 0;
    int choice = 0;
    double rawCost = 0;

    System.out.println("Hello! Welcome to the new 7/11 online ordering device.");
    System.out.println("We're low on supplies, so only pick one.");
    System.out.println("");
    //menu
    String [][] menu = {
            {"(1) Pizza - $4.99","          (6) Slurpee - $0.79"},
            {"(2) Cigarettes - $7.99","          (7) Hotdog - $1.99"},
            {"(3) Coffee - $2.99", "          (8) Doritos - $2.99"},
            {"(4) Mountain Dew - $1.49", "  (9) Water - $1.29"},
            {"(5) Ice cream - $2.49", "          (10) Muffin - $0.99"},
    };

    //prints menu
    for (int e = 0; e < 5; e++)
    {
        System.out.println(menu[e][0] + "\t"+ menu[e][1]);
        System.out.print("");
    }

    System.out.println("");
    System.out.println("Enter the number of the item you want: ");

    //user chooses item off of menu
    int userInputMenu = in.nextInt();
    if (userInputMenu == 1)
    {
        choice = 1;
        System.out.println("You chose the pizza for $4.99.");
        rawCost = 4.99;
    }       
    if (userInputMenu == 2)
    {
        choice = 2;
        System.out.println("You chose the cigarettes for $7.99.");
        rawCost = 7.99;
    }       
    if (userInputMenu == 3)
    {
        choice = 3;
        System.out.println("You chose the coffee for $2.99.");
        rawCost = 2.99;
    }

**Continues all the way to userInputMenu == 10**


    System.out.println("Now to calculate the tax, please enter the state you are currently in: ");
    String userInputState = in.next();

    //what state is the user in?
    if (userInputState.equals("Alaska") || userInputState.equals("Delaware") || userInputState.equals("Montana") || userInputState.equals("New Hampshire") || userInputState.equals("Oregon"))
    {
        taxPrice = 0.0;
        System.out.println("Luckily, the sales tax in " + userInputState + " is 0, so your final price is " + rawCost);
    }
    if (userInputState.equals("Colorado"))
    {
        taxPrice = 0.029;
        totalPrice = ((taxPrice * rawCost) + rawCost);
        System.out.println("The sales tax in " + userInputState + " is only " + taxPrice);
        System.out.println("That means that the total price of your item is "+ totalPrice);
    }

**Also continues for all 50 states**


    //thank you
    System.out.println("");
    System.out.println("Thank you for shopping at the new online 7/11.");
    System.out.println("Thank you come again.");


}

只是要求我就如何满足我缺少的要求进行彻底的解释。

另外,如何在用户输入状态时将其读取为“MARYLAND”或“maryland”?

2 个答案:

答案 0 :(得分:0)

您是否阅读过面向对象编程的任何内容?这是一个很好的使用方式,因为你有一个超级的项目&#39;,这些项目的子类&#39;咖啡&#39; &#39;思乐冰&#39;此外,客户是否能够一次购买多个项目,然后找到含税的总额?

答案 1 :(得分:0)

我试图对此进行编码。您可以根据其他人对SO或您的需求的输入进一步了解。这对你来说可能是个好的开始。添加try / catch来处理异常:

用于保存菜单对象的类:

public class Menu {

    private int id;
    private String item;
    private double Price;

    public Menu(int id, String item, double price) {
        super();
        this.id = id;
        this.item = item;
        Price = price;
    }

    public int getId() {
        return id;
    }

    public String getItem() {
        return item;
    }

    public double getPrice() {
        return Price;
    }

    @Override
    public String toString() {
        return "Menu [id=" + id + ", item=" + item + ", Price=$" + Price + "]";
    }

    public boolean validateMenu(int id) {
        if (id == this.id) {
            System.out.println("You chose the " + item + " for " + Price);
            return true;
        }
        return false;
    }
}

保存州对象的类:

public class State {

    private String message;
    private String state;
    private double taxPrice;

    public State(String state, double taxPrice, String message) {
        this.state = state;
        this.taxPrice = taxPrice;
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public String getState() {
        return state;
    }

    public double getTaxPrice() {
        return taxPrice;
    }

    @Override
    public String toString() {
        return "State [taxPrice=" + taxPrice + ", state=" + state
                + ", message=" + message + "]";
    }

    public boolean validateState(String state) {
        if (state.equalsIgnoreCase(this.state)) {
            System.out.println(this.message.replace("userInputState", this.state) + this.taxPrice);
            return true;
        }
        return false;
    }
}

<强> Tax_Calculator

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Tax_Calculator {

    public static void main(String[] args) throws Exception {

        Scanner in = null;
        Tax_Calculator tc;
        Menu menu = null;
        State state = null;
        double taxPrice;
        double totalPrice;

        System.out
                .println("Hello! Welcome to the new 7/11 online ordering device.");
        System.out.println("We're low on supplies, so only pick one.");
        System.out.println("");

        tc = new Tax_Calculator();

        boolean continueFlag = true;

        while (continueFlag) {
            tc.printMenu();

            System.out.println("");
            System.out.println("Enter the number of the item you want: ");

            in = new Scanner(System.in);
            int userInputMenu = in.nextInt();

            for (Menu menu2 : tc.menuList)
                if (menu2.validateMenu(userInputMenu)) {
                    menu = menu2;
                    break;
                }

            if (menu == null)
                System.out.println("Please choose a valid number! \n");
            else
                continueFlag = false;
        }

        System.out.println("");
        System.out
                .println("Now to calculate the tax, please enter the state you are currently in: ");

        in.nextLine();
        String userInputState = in.nextLine();

        for (State state2 : tc.stateList)
            if (state2.validateState(userInputState)) {
                state = state2;
                break;
            }

        taxPrice = state.getTaxPrice();
        totalPrice = (1 + taxPrice) * menu.getPrice();
        System.out.print("That means that the total price of your item is ");
        System.out.printf("%.2f", totalPrice);

        System.out.println("");
        System.out.println("Thank you for shopping at the new online 7/11.");
        System.out.println("Thank you come again.");

        in.close();
        tc.destroy();
    }

    private List<Menu> menuList = null;
    private List<State> stateList = null;

    Tax_Calculator() {
        initMenu();
        initState();
    }

    private void destroy() {
        menuList = null;
        stateList = null;
    }

    private void initMenu() {
        menuList = new ArrayList<Menu>();
        menuList.add(new Menu(1, "Pizza", 4.99));
        menuList.add(new Menu(2, "Cigarettes", 7.99));
        menuList.add(new Menu(3, "Coffee", 2.99));
    }

    private void initState() {
        stateList = new ArrayList<State>();
        stateList.add(new State("North Dakota", 0.05,
                "The sales tax in userInputState is "));
        stateList.add(new State("Arizona", 0.05,
                "The sales tax in userInputState is "));
        stateList.add(new State("Maine", 0.05,
                "The sales tax in userInputState is "));
    }

    private void printMenu() {
        for (Menu menu : menuList)
            System.out.println(menu);
    }
}