java - 不确定如何创建使用其他类方法的菜单

标签 java oop methods menu

<分区>

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

我已经测试了这两个类,它们似乎都有效。

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

  1. 添加客户详细信息
  2. 向企业账户存入资金
  3. 将抄表记录到企业帐户
  4. 显示企业账户的当前余额
  5. 显示完整的帐户详细信息
  6. 更改企业帐户的折扣值
  7. 更改所有企业帐户的单位成本
  8. 如何使用菜单系统

虽然我已尽最大努力使用在线资源找出如何做到这一点,但我目前迷路了。到目前为止,每次我尝试创建菜单系统时,我都无法让它工作。如果有人可以帮助我了解如何去做的基础知识,我将不胜感激。

谢谢:)


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());

关于java - 不确定如何创建使用其他类方法的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16631418/

相关文章:

python - 仅对具体类为 True 的静态属性,在 Python 中对其子类为 False

c# - 如何生成嵌套的 IENumerable?

java - 在 JScrollPane 中按比例绘制图像

java - keycloak SPI 用于客户端策略?

java - 颜色(Alpha)通过鼠标悬停而改变,直到出现工具提示

java - OpenImaj 教程和 EigenFaces

java - 在类中使用共享服务

oop - Magento Tier 销售价格?

c# - 有人可以(很好地)解释为什么 OOP 方法重载是一件好事吗?

c++ - enqueue() 方法向队列 : how to implement in C++? 添加一个元素