简体   繁体   English

将更改的值从一种方法转移到另一种方法

[英]Transferring a changed value from one method to another

so what I am trying to do it take the Value that I am allowing my BargainCustomer to spend, and get him/her to buy the first car they see in the list under that price. 因此,我要努力做到的是,我要让BargainCustomer消费这个价值,并让他/她以该价格购买他们在清单中看到的第一辆车。 Then take the remaining money and buy whatever features they can. 然后拿走剩下的钱,购买他们能提供的任何功能。 However, I do not remember how to call my value for the remaining money from my other method. 但是,我不记得如何用其他方法来调用剩余价值的价值。 Here is my code: 这是我的代码:

import java.util.ArrayList;

public class BargainCustomer implements Customer
{
   public Car purchase(ArrayList<Car> listOfCars)
   {
       int moneyToSpend = 35000;
       int moneyLeft = 0;
       int indexLocation = 0;
       for (int i = 0; i < listOfCars.size(); i++)
       {
           if (listOfCars.get(i).getBasePrice()<moneyToSpend)
           {
               moneyLeft=moneyToSpend-listOfCars.get(i).getBasePrice();
               indexLocation = i;
           }
       }
       return listOfCars.get(indexLocation);
   } 

   public void upgrade(Car g)
   {
       int featuresMoney = moneyLeft;
       for (int i = 0; i < g.getFeatures().size(); i++)
       {
           moneyLeft-=g.getFeatures().get(i).getCost();
           if (moneyLeft>=0)
           {
               g.getFeatures().get(i).purchase();
           }
       }
   }
}

Ideally the buyer should only buy what they can afford with the remaining money. 理想情况下,买方应只用剩余的钱买得起他们所能承受的。

depending on your logic, it would be best to just chain the calls: 根据您的逻辑,最好只是将调用链接起来:

import java.util.ArrayList;

public class BargainCustomer implements Customer
{
   public Car purchase(ArrayList<Car> listOfCars)
   {
       int moneyToSpend = 35000;
       int moneyLeft = 0;
       int indexLocation = 0;
       for (int i = 0; i < listOfCars.size(); i++)
       {
           if (listOfCars.get(i).getBasePrice()<moneyToSpend)
           {
               moneyLeft=moneyToSpend-listOfCars.get(i).getBasePrice();
               indexLocation = i;
           }
       }
       return upgrade(listOfCars.get(indexLocation), moneyLeft);
   } 

   public void upgrade(Car g, int moneyLeft)
   {
       int featuresMoney = moneyLeft;
       for (int i = 0; i < g.getFeatures().size(); i++)
       {
           moneyLeft-=g.getFeatures().get(i).getCost();
           if (moneyLeft>=0)
           {
               g.getFeatures().get(i).purchase();
           }
       }
   }
}

This will just give you the car with all the features right away, if you need steps in between, you can just use a class variable, or calculate it again. 这将立即为您提供具有所有功能的汽车,如果您需要在两者之间进行操作,则可以使用类变量或重新计算。 Since you return the car, you can just substract the car price from the moneyToSpend 既然您还车,就可以从moneyToSpend中减去车价

PS are you sure, int is the right choose for money in your case ? PS,您确定,int是您的情况下正确的选择吗? it will not handle comma values. 它不会处理逗号值。 Depending on currency, prices are rarly ever without fractions. 根据货币的不同,价格几乎没有零头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM