How to make a looping for input data then sum all value without override the first input



My professor asked me to make a program about cashier using Java. The program is supposed to be inputting the codename of the product and how much you buy it.

Before printing the value, the program will ask you “do you want to count another [Y/N]”. If the user inputting Y then the user will asked to input another value. And then the program will sum every inputs as a total and print it.

I’m almost successful in making the program, but I don’t know how to make loop for inputting another value without override the first value. And then I can sum total value each input. As I know using for loops makes you to set the value of maximum loops, before starting loops.

And this is what I’m trying to do

for(i=0; i<x; i++){
  System.out.println("KODE BUAH (A/J/M):");
  kode = input.nextLine();
  if (kode.equalsIgnoreCase("A"))
  {   harga = harga+47000;
      nama = nama+"Anggur";
  } 
  if (kode.equalsIgnoreCase("J"))
  {   harga = harga+30000;
      nama = nama+"Jeruk";
  } 
  if (kode.equalsIgnoreCase("M"))
  {   harga = harga+20000;
      nama = nama+"Melon";
  }
  while (!(kode.equals("a") || kode.equals("j") || kode.equals("m"))){
      System.out.println("INPUT SALAH");
      System.out.println();
      System.out.print("KODE BUAH   (A/J/M):");
      kode = input.nextLine();
  }
  System.out.print("JUMLAH BELI :");
  jml = input.nextInt();
  
  bayar = harga*jml;
  if (bayar > 300000)
  {
    diskon = bayar/10;
  }
  else diskon = 0;
    
  bayar = harga*jml;
  do {
    System.out.println("masukkan Y/T");
    a = input.nextLine();
  } while (!a.equalsIgnoreCase("Y") && !a.equalsIgnoreCase("T"));

  } while (a.equalsIgnoreCase("Y"));
  {   
      x = x+1;
  } 

Answer

public class Shopping {
private Map<String, Integer> costMap = new HashMap<>();
private Map<String, Integer> buyMap = new HashMap<>();
private Map<String, String> nameMap = new HashMap<>();

private void init() {
    costMap.put("A", 47000);
    costMap.put("J", 30000);
    costMap.put("M", 20000);
    
    nameMap.put("A", "Anggur");
    nameMap.put("J", "Jeruk");
    nameMap.put("M", "Melon");
    
    costMap.keySet()
           .stream()
           .forEach(k -> buyMap.put(k, 0));
}

public Shopping() {
    init();
}

public void shop() throws IOException {
    try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
        while(true) {
            System.out.println("Buy what (A/J/M)?");
            
            String code = in.readLine().toUpperCase();
            if (nameMap.containsKey(code)) {
                System.out.println("How many?");
                int amt = Integer.parseInt(in.readLine());
                buyMap.put(code, amt + buyMap.get(code));
            } else {
                System.out.println("Not a valid code.");
            }
            System.out.println("Do you wish to buy more? (Y/N)");
            String doMore = in.readLine();
            if (doMore.equalsIgnoreCase("n")) {
                break;
            }
        }   
    }
}

public void output() {
    int total = 0;
    for (Entry<String, Integer> entry : buyMap.entrySet()) {
        System.out.println(String.format("%s (%d) at %d each", nameMap.get(entry.getKey()), entry.getValue(), costMap.get(entry.getKey())));
        total += entry.getValue() * costMap.get(entry.getKey());
    }
    System.out.println(String.format("Total: %d", total));
}
public static void main(String [] args) throws IOException {
    Shopping shopping = new Shopping();
    shopping.shop();
    shopping.output();
    
}

}



Source: stackoverflow