如何让用户在我的代码中设置自己的订单号?

时间:2015-11-02 01:26:03

标签: java class

我是初学程序员,我不明白为什么我不能在我的驱动程序中设置orderNum。我希望用户能够自己设置orderNum,因为没有orderNum是易于设置的。

public class Cafe 
{
    private int orderNum;
    private String cafeName, cafeID;

    public Cafe()
    {
        int orderNum;
        String cafeName = null, cafeID;
    }
    public String setName(String cafeID)
    {
        if(cafeID.equals("101"))
            cafeName = "Essex Cafe";
        else if(cafeID.equals("252"))
            cafeName = "White Marsh Cafe";
        else
            cafeName = "Towson Cafe";
        return cafeName;
    }
    public String toString()
    {

        return "\t" + cafeName + "\nOrder Number = " + orderNum;
    }
}

import java.util.Scanner;
public class CafeProject 
{
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    Cafe Emp = new Cafe();

    System.out.print("Enter Cafe Identifier:    ");
    Emp.setName(scan.next());




    System.out.print(Emp);
    }
}

1 个答案:

答案 0 :(得分:1)

像这样更改您的程序,您可以手动设置订单

import java.util.Scanner;
class Cafe
{
    private int orderNum;
    private String cafeName, cafeID;

    public Cafe()
    {        int orderNum;
        String cafeName = null, cafeID;
    }
    public String setName(String cafeID)
    {
        if(cafeID.equals("101"))
            cafeName = "Essex Cafe";
        else if(cafeID.equals("252"))
            cafeName = "White Marsh Cafe";
        else
            cafeName = "Towson Cafe";
        return cafeName;
    }
    public void setOrderNo(int orderNo){
        orderNum=orderNo;
    }
    public String toString()
    {

        return "\t" + cafeName + "\nOrder Number = " + orderNum;
    }
}

public class CafeProject
{
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    Cafe Emp = new Cafe();

    System.out.print("Enter Cafe Identifier:    ");
    Emp.setName(scan.next());
    System.out.print("Enter Order No:    ");
    Emp.setOrderNo(scan.nextInt());



    System.out.print(Emp);
    }
}

您只需要一个功能来设置顺序