首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >7-11网上商店计划。需要修改吗?

7-11网上商店计划。需要修改吗?
EN

Stack Overflow用户
提问于 2015-06-07 15:17:27
回答 2查看 120关注 0票数 0

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

这个项目实际上是我在班上的最后一个项目。正因为如此,我必须满足一些要求,而我目前在这方面遇到了困难。我已经完成了几乎所有的要求,但注意到有些还没有完成。我缺少的一些要求是:

  • 一个专门的构造器(我从来不知道如何高效地完成这个任务,而且我怀疑,考虑到我的代码,这是否仍然可行)
  • 1访问器和2个修饰符(与构造函数相同,如何使用给定的代码正确地完成此操作?)
  • 某种形式的输出使用printf (也许可以用来计算总价?)可惜我没有完全理解)

下面是我的效率极低的代码本身:

代码语言:javascript
复制
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.");


}

只是想详细解释一下我怎样才能满足我缺失的要求。

另外,当用户输入状态时,程序可以将其读为“马里兰”或“马里兰”。

EN

回答 2

Stack Overflow用户

发布于 2015-06-07 15:51:57

你读过任何关于面向对象编程的文章吗?这是一个很好的情况使用它,因为你有一个超类的‘物品’,这些项目的子类‘咖啡’浆糊‘等等。同样,客户可以购买多个项目在同一时间,然后找到总额与税收?

票数 0
EN

Stack Overflow用户

发布于 2015-06-07 17:54:03

我试图对此进行编码。你可以在其他人对你的需求的投入的基础上更进一步。这对你来说可能是个好开始。添加try/catch以处理异常:

保存菜单对象的类:

代码语言:javascript
复制
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;
    }
}

保存状态对象的类:

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30695178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档