java-不知道如何创建一个菜单,该菜单使用其他类的方法

到目前为止,我已经创建了两个类,下面将显示

我已经测试了两个类,它们似乎都可以工作.

我现在需要创建一个菜单,该菜单将使用我创建的两个类,并允许用户输入信息并在其帐户中找到以下信息:

>添加客户详细信息
>存入公司账户
>将抄表记录到企业帐户
>显示商家帐户的当前余额
>显示完整的帐户详细信息
>更改企业帐户的折扣值
>更改所有企业帐户的单位成本
>如何使用菜单系统

虽然我已尽力寻找如何使用在线资源来做到这一点,但我目前迷失了.到目前为止,每次尝试创建菜单系统时,我都无法正常工作.如果有人可以在如何做的基础上帮助我,我将不胜感激.

谢谢 :)

public class GasAccount
{
    private int intAccRefNo;
    private String strName;
    private String strAddress;
    public double dblBalance;
    private double dblUnits;
    public static double dblUnitsCosts = 0.02;

    public GasAccount (int intNewAccRefNo , String strNewName , String strNewAddress)
    {
    }

    public GasAccount (int intNewAccRefNo , String strNewName , String strNewAddress , double dblNewUnits)
    {
        intAccRefNo = intNewAccRefNo;
        strName = strNewName;
        strAddress = strNewAddress;
        dblUnits = dblNewUnits;
        dblBalance = dblUnits * dblUnitsCosts;
    }

    public int getAccRefNo()
    {
        return intAccRefNo;
    }

    public String getName()
    {
        return strName;
    }

    public String getAddress()
    {
        return strAddress;
    }

    public void deposit(double dblDepositAmount)
    {
        dblBalance = dblBalance - dblDepositAmount;
    }

    public double getBalance()
    {
        return dblBalance;
    }

    public double getUnitCost()
    {
        return dblUnitsCosts;
    }

    public void recordUnits (double dblUnitsUsed)
    {
        dblBalance = dblBalance + dblUnitsUsed * dblUnitsCosts;
        dblUnits = dblUnitsUsed + dblUnits;
    }

    public double getUnits()
    {
        return dblUnits;
    }

    public void updateUnitsCosts(double dblNewUnitsCosts)
    {
        this.dblUnitsCosts = dblNewUnitsCosts;
    }
}

另一个扩展了它-

public class BusinessAccount extends GasAccount
{

    private double dblDiscount;

    public BusinessAccount (int intNewAccRefNo, String strNewName, String strNewAddress, double dblNewUnits, double dblNewDiscount)
    {
        super (intNewAccRefNo , strNewName , strNewAddress, dblNewUnits);
        dblDiscount = dblNewDiscount;
    }

    public void setNewDiscount(double dblNewDiscount)
    {
        dblDiscount = dblNewDiscount;
    }

    public double getDiscount()
    {
        return dblDiscount;
    }

    @Override
    public void recordUnits (double dblUnitsUsed)
    {
        double dblNewBalance;
        dblBalance = dblBalance + dblUnitsUsed * dblUnitsCosts;
        dblNewBalance = dblUnitsUsed * dblUnitsCosts * dblDiscount / 100;
        dblBalance = dblBalance - dblNewBalance;
    }

这是我尝试的菜单,直到第五个选项为止.我从其他类中调用方法时发生了严重错误,因为BuisnessAccount.getMethod始终显示为错误.我也很确定再次声明变量是完全错误的,因为它们与我的其他类没有链接.

如果有人可以帮助我解决这个问题,将不胜感激

import java.util.Scanner;

public class Menu
{
    public static void main(String [] args)
    {

        Scanner input = new Scanner(System.in);

        int Choice;

        {
            System.out.println("------------------------------");
            System.out.println ( "1. Add the customers details" ) ;
            System.out.println ( "2. Make a deposit to the business account" );
            System.out.println ( "3. Record a meter reading to the business account" ) ;
            System.out.println ( "4. Display current balance of the business account" ) ;
            System.out.println ( "5. Display full account details" ) ;
            System.out.println ( "6. Change the discount value for the business account" ) ;
            System.out.println ( "7. Change the cost per unit for all business accounts ");
            System.out.println ( "8. How to use the menu system ");
            System.out.println ( "Any other number will exit the program");
            System.out.println("------------------------------");
            System.out.println ( "\n\nEnter a number from 1 to 8" );
            Choice = input.nextInt();

            switch (Choice)
            {
             case 1 :
              int intNewAccRefNo;
              String strNewName;
              String strNewAddress;
              Double dblNewUnits;
              Double dblNewDiscount;

              System.out.println("Please enter the account number?");
              intNewAccRefNo  = input.nextInt();

              System.out.println("Please enter the account name?");
              input.nextLine();
              strNewName = input.nextLine();

              System.out.println("Please enter the account address?");
              strNewAddress = input.nextLine();

              System.out.println("Please enter the number of initial number of units used?");
              dblNewUnits = input.nextDouble();

              System.out.println("Please enter the discount?");
              dblNewDiscount = input.nextDouble();

             case 2:

              double dblDeposit;

              System.out.println("Please enter the amount you want to deposit?");
              dblDeposit = input.nextDouble();

              System.out.println ( "The current balance: " + BusinessAccount.getBalance() ) ;

             case 3:

              double dblUnits;

              System.out.println("Enter the number of Units Used");
              dblUnits = input.nextDouble();
              BusinessAccount.recordUnits(dblUnits);

             case 4:

              System.out.println("\n Current Balance: £"+ BusinessAccount.getBalance());

             case 5:

               System.out.println("Account Reference Number: " + BusinessAccount.getAccRefNo());
               System.out.println("Address: " + BusinessAccount.getAddress());
               System.out.println("Name: " + BusinessAccount.getName());
               System.out.println("Balance: " + BusinessAccount.getBalance());
               System.out.println("Discount: " + BusinessAccount.getDiscount());
               System.out.println("Units: " + BusinessAccount.getUnits());
case 1 :  

String strAddress;

System.out.println("Please enter the account address?"); 
strAddress = input.nextLine(); 
System.out.println("Address:" + firstAccount.getAddress());

用户输入的内容和我正在调用的方法之间没有任何联系,不确定如何解决.

解决方法:

通过使用BusinessAccount.someMethod(),您试图在静态上下文中调用所述方法,但是您的方法不是静态的.要调用您的方法,您需要使它们静态化,或者需要创建一个对象然后可以对其进行调用,即:

BusinessAccount ba= new BusinessAccount (4, "name", "address", 3.4, 43.4);
ba.someMethodFromClass();

在您的情况下,您不希望它们是静态的.阅读有关静态方法/变量的更多信息(请参见下文).

一些文档可能会有所帮助:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
http://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html

要回答第二个问题,您需要有一个方法(在对象的类中,而不是在main中),该方法可以为变量分配值.即:

public void setAddress (String address)
{
    strAddress=address;
}

然后,将将从main读取的地址传递给函数.通过这样做,您正在将用户的值初始化/分配给您类中存储的值,即:

System.out.println("Please enter the account address?"); 
String temp = input.nextLine(); 
ba.setAddress(temp);
System.out.println("Address:" + ba.getAddress());

甚至更好

System.out.println("Please enter the account address?"); 
ba.setAddress(input.nextLine());
System.out.println("Address:" + ba.getAddress());
上一篇:python-如何继承numpy.`ma.core.masked_array`?


下一篇:Python-如何更改模块实现?