当用户要求重复执行程序时如何获得总价值?

时间:2019-03-24 11:39:50

标签: java oop

我是一个绝对的初学者,我正在为网上商店编写程序,我有3个部门,而且我是从女性部门开始的。我问用户是否要添加另一种产品,如果可以,那么我想做的就是重复执行该程序并添加每次重复的值,仅打印总数,但我的代码无法按我需要的方式工作它可以正常工作。

public class WomenDep extends Shop{
    int a,c,d,NoOfSizes=3,Counter;
    double b,Total;
    Prices P1 = new Prices();
    double []arr = new double[NoOfSizes];
    @Override
     public void GetDet(){
        System.out.println("Pls Select Product No., \n(1)--> Tshirt\n(2)--> Short\n(3)--> Jeans\n"
                + "(4)--> Dress\n(5)--> Skirts");
        a = input.nextInt();
        System.out.println("Pls Select required Size No., \n(1)-->S\n(2)-->M\n(3)-->L");
        b = input.nextDouble();
        System.out.println("Enter Number of Pieces");
        c = input.nextInt();
        this.Pieces=c;
         System.out.println("Do you want to add another product to your cart?\n(1)-->Yes\n(2)-->No");
        d = input.nextInt();
    }

    @Override
     public double [] SearchArr(){
         if(this.a==1){
            this.ProductName = "Tshirt";
            for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WTshirt[i];
            }
         if(this.a==2){
            this.ProductName = "Short";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WShort[i];
            }
         if(this.a==3){
            this.ProductName = "Jeans";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WJeans[i];
            }
         if(this.a==4){
            this.ProductName = "Dress";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WDress[i];
            }
         if(this.a==5){
            this.ProductName = "Skirts";
         for (int i = 0; i<NoOfSizes; ++i)
                arr[i]=P1.WSkirts[i];
         }
          return arr;
     }

    @Override
     public double CalPrice(){
         if(b==1){
                Price = arr [0];}
         if(b==2){
                Price = arr [1];}
         else if(b==3){
                Price = arr [2];}
         return Price;
     }

    @Override
     public void TotalPrice(){
         TPrice =Price * Pieces;
         System.out.println("this is total Price "+TPrice);
     }

    @Override
     public void Recal(){ 
            do{
            this.GetDet();
            this.SearchArr();
            this.CalPrice();
            this.TotalPrice();
            }while(d==1);
        }
     }
public class Prices extends Shop{
    public double [] WTshirt = {60.00,100.5,120};//S,M,L
    public double [] WShort = {50,110,130.5};//S,M,L
    public double [] WJeans = {150.99,180,200};//S,M,L
    public double [] WDress = {350,400,450.99};//S,M,L
    public double [] WSkirts = {350.5,499.99,450.5};//S,M,L
}
  public static void main(String[] args) {
     WomenDep a = new WomenDep();
     a.Recal();

    }

我希望它每次都将计算出的价格的总价值相加,但它只会计算一个。

1 个答案:

答案 0 :(得分:0)

计算价格的方法仅会起作用一次。

  1. 将调用更改为this.CalPrice()以存储该函数的返回值。
  2. 创建价格作为全局变量,以存储更新的价格

    public void Recal(){ 
    do{
        this.GetDet();
        this.SearchArr();
        double Price = this.CalPrice();
        this.TotalPrice();
    }while(d==1);
    }
    
  3. 更改TotalPrice函数以更新价格

  4. 全局创建TPrice变量,以获取最新的更新价格

    public void TotalPrice(){
     TPrice = TPrice + this.Price * Pieces;
    System.out.println("this is total Price "+TPrice);
    }
    

完整代码,我已经删除了扩展类部分,请查看并检查问题是否已解决。

import java.util.Scanner;

public class WomenDep{
int a,c,d,NoOfSizes=3,Counter;
double b,Total;
Prices P1 = new Prices();
double []arr = new double[NoOfSizes];
private int Pieces;
double Price;
double TPrice;
Scanner input = new Scanner(System.in);
private String ProductName;

public void GetDet(){
    System.out.println("Pls Select Product No., \n(1)--> Tshirt\n(2)--> Short\n(3)--> Jeans\n"
            + "(4)--> Dress\n(5)--> Skirts");
    a = input.nextInt();
    System.out.println("Pls Select required Size No., \n(1)-->S\n(2)-->M\n(3)-->L");
    b = input.nextDouble();
    System.out.println("Enter Number of Pieces");
    c = input.nextInt();
    this.Pieces=c;
    System.out.println("Do you want to add another product to your cart?\n(1)-->Yes\n(2)-->No");
    d = input.nextInt();
}


public double [] SearchArr(){
    if(this.a==1){
        this.ProductName = "Tshirt";
        for (int i = 0; i<NoOfSizes; ++i)
            arr[i]=P1.WTshirt[i];
    }
    if(this.a==2){
        this.ProductName = "Short";
        for (int i = 0; i<NoOfSizes; ++i)
            arr[i]=P1.WShort[i];
    }
    if(this.a==3){
        this.ProductName = "Jeans";
        for (int i = 0; i<NoOfSizes; ++i)
            arr[i]=P1.WJeans[i];
    }
    if(this.a==4){
        this.ProductName = "Dress";
        for (int i = 0; i<NoOfSizes; ++i)
            arr[i]=P1.WDress[i];
    }
    if(this.a==5){
        this.ProductName = "Skirts";
        for (int i = 0; i<NoOfSizes; ++i)
            arr[i]=P1.WSkirts[i];
    }
    return arr;
}


public double CalPrice(){

    if(b==1){
        Price = arr [0];}
    if(b==2){
        Price = arr [1];}
    else if(b==3){
        Price = arr [2];}
    return Price;
}


public void TotalPrice(){
     TPrice = TPrice + this.Price * Pieces;
    System.out.println("this is total Price "+TPrice);
}


public void Recal(){ 
    do{
        this.GetDet();
        this.SearchArr();
        double Price = this.CalPrice();
        this.TotalPrice();
    }while(d==1);
}
 class Prices{
    public double [] WTshirt = {60.00,100.5,120};//S,M,L
    public double [] WShort = {50,110,130.5};//S,M,L
    public double [] WJeans = {150.99,180,200};//S,M,L
    public double [] WDress = {350,400,450.99};//S,M,L
    public double [] WSkirts = {350.5,499.99,450.5};//S,M,L
}


public static void main(String[] args) {
WomenDep a = new WomenDep();
a.Recal();

}
}