简体   繁体   中英

incrementing arrays and length

I am setting up a shop program. The code below is a method. The code includes setting up a shop and actually using the shop. Within this method, I am trying to figure out the discount a user gets based upon how many items a user buys.

When a user sets up a shop, they are able to set x amount of items to qualify for the discount.

For example, if a user buys 20 items, and x amount of items to qualify for the discount is set to 6 packs. The user would get (2 * the price of items) discount subtracted from the original price.

Every 6 packs the user gets 1 pack for free to a max of 20.

6 packs + 1 pack free + another 6 packs + one pack free + 6 pack = 20 packs.

I have tried to create loop such as subtracting the number of items a user wants to buy bought while counting value when modulo is 0 and subtracting from the amount that the user bought but I am getting nowhere close to it.

Arrays were brought from other methods:

price[i] is the price of the item buyItems[i] is the number of items a user buys packs[i] is the user choice of discount, for example, a user can set the discount to apply at 2 pack, 3 pack or even none.

public static void checkOuts(String [] names, double [] price,double [] packs,double addDiscount[], double[] buyItems, double[]addDiscountrate, int k ){

    double orgSub=0;
    double newSub=0;
    double addPercent=0;
    double specDis =0;
    double freePack=0;
    double disCheck =0;
    double count=0;

    for (int i =0; i < k; i++ ) {
        orgSub+=price[i]*buyItems[i];
    }

    System.out.println("Original Subtotal:             $" + orgSub);

for (int i =0; i < k; i++ ) {
    orgSub+=price[i]*buyItems[i];
}

System.out.println("Original Subtotal:             $" + orgSub);

for (int j = 0; j < buyItems.length; j++) {
    disCheck = buyItems[j];

    for(int d =0; d < buyItems.length; d++) {
        freePack = packs[d];

        for (int s =1; s < disCheck; s++)
            if (s % freePack==0) {
                count++;
                disCheck = disCheck -1;
                System.out.println(disCheck);
            }

    }
    specDis+= count*price[j];
    // the final discount that will be subtracted from original
}

Why are all of these arrays? Aren't they just an amount? (I may be misunderstanding)

If you do change these to integers, you can get the amount for free with

count = Math.floor(buyItems/packs);

My version of your code (I removed things that were never explained):

public static void checkOuts(String[] names, double[] price, int[] packs, int[] buyItems) {

        System.out.println("Name\tnumFree\ttotalNum\tTotalPrice");
        for (int i = 0; i < names.length; i++) {
            int numFree = buyItems[i] / (packs[i] + 1);
            double totalPrice = (buyItems[i] - numFree) * price[i];
            System.out.println(names[i] + "\t" + numFree + "\t" + buyItems[i] + "\t\t" + totalPrice);
        }

    }

When called with the following code:

String[] names = {"Coke", "Pepsi"};
double[] price = {1, 1};
int[] packs = {3, 4};
int[] buyItems = {10, 20};

checkOuts(names, price, packs, buyItems);

You get the following output: (Which is in line with your comments)

Name    numFree totalNum TotalPrice
Coke    2       10       8.0
Pepsi   4       20       16.0

You may also want to look into making your code object oriented. Rather than passing around parallel arrays, it would be easier to pass around a single array of items, or orders, which would have fields for name, price, discount, and anything else you needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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