Как получить итоговое значение, когда пользователь просит повторить программу? - PullRequest
0 голосов
/ 24 марта 2019

Я абсолютный новичок, и я пишу программу для интернет-магазина, у меня есть 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 голосов
/ 24 марта 2019

Способ расчета цены будет работать только один раз.

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

}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...