(Java) Попытка увеличить переменную на 1 каждый раз, когда еда оплачивается - PullRequest
0 голосов
/ 23 октября 2019

Итак, у меня есть класс для оплаты еды, и когда он успешно выполняется, я пытаюсь заставить его увеличить переменную на 1 и сохранить ее в этой переменной, чтобы я мог отобразить количество транзакций, связанных с объектом, вотформатированный строковый метод с именем getStatistics (), я относительно новичок в Java, (это для назначения, так что, возможно, просто установите меня на правильном пути или выделите, что это за ошибка).

вот класс -метод называется payForMeal ()

public class CanteenAcc 
{

        private String customerId; 
        private String name;
        private double balance;
        private static double minTopup = 2.00;
        private String status;
        private static double creditLimit = 5.00; 
        private static int transCount;


       public CanteenAcc(String newId, String newName, double newBalance)
        { 
            this.customerId = newId;
            this.name = newName; 
            this.balance = newBalance;

        }
        public CanteenAcc(String newId, String newName)
        {
            this.customerId = newId;
            this.name = newName; 
        }

        public void topUp(double depositAmount)
        {
            this.balance += depositAmount;
            if(depositAmount > 0)
            {
                this.status = "Valid";
            }else
            {
                this.status = "Invalid";
            }
          }  

         public void payForMeal(double amount)
         {
             if((balance < 0 || balance - amount < 0) && amount < creditLimit)
             {
                double newBalance = balance - amount;
                this.status = "Using Credit";
                throw new RuntimeException("You must topUp your balance");

             }
             else if(amount > creditLimit)
             {
                  throw new RuntimeException("Cost exceeds the credit limit.");
             }
             else
             {   

                 double newBalance = balance - amount;
                 transCount++;    
                 System.out.println(transCount);
             }  

        }

       public String displayAccountDetails()  
       {
             StringBuilder ad = new StringBuilder(); 
             ad.append("------------------------\n");
             ad.append("****Account Details****\n");
             ad.append("------------------------\n");
             ad.append("\n");
             ad.append("****Customer ID****: \n" + customerId + "\n");
             ad.append("\n");
             ad.append("****Name****: \n" + name + "\n"); 
             ad.append("------------------------\n");
             ad.append("\n");




             return ad.toString();
       }

       public double getBalance()
       {
          return balance;
       }

       public double getCreditLimt()
       {
           return creditLimit; 
       }

       public double getMinTopup() 
       {
           return minTopup;
       }

       public String getStatus()
       {
           return status; 
       }

       public void updateCreditLimit(double newLimit)
       {
           this.creditLimit = newLimit; 
       }

       public void updateMinTopup(double newTopup)
       {
            this.minTopup = newTopup; 
       }

       public String getStatistics()
       {
           StringBuilder as = new StringBuilder();

           as.append("------------------------\n");
           as.append("    CANTEEN ACCOUNT    \n");
           as.append("------------------------\n");
           as.append("\n");
           as.append("****Transaction Count****\n");
           as.append(transCount + "\n");
           as.append("\n");
           as.append("****Account Balance****\n");
           as.append(balance + "\n");
           as.append("\n");
           as.append("***Account Status****\n");
           as.append(status + "\n");
           as.append("------------------------\n");
           return as.toString();}



       }

, и вот основной метод, который я использую метод payFor food

public class Test {

    public static void main(String[] args) {

        CanteenAcc liam = new CanteenAcc("ABD92", "Liam", 2.00);

        System.out.println(liam.displayAccountDetails());
        System.out.println(liam.getStatistics());
        liam.payForMeal(2.00);

    }

}
...