Home > Enterprise >  I have a problem with do while loop in my project
I have a problem with do while loop in my project

Time:05-13

This is the program, I designed it to be a phone store, and the problem I am facing is that after you choose to go back, the program will go back normally, but after you choose to buy the next time it restarts the program. What I want is to make it stop at buy directly.

import java.util.Scanner;
public class MyStore {
    
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        Phones phOne = new Phones("iPhone 13.");
        phOne.phs(256);
        phOne.php(904);
        Phones phTwo = new Phones("Galaxy s22.");
        phTwo.phs(512);
        phTwo.php(1199);
        Phones phThree = new Phones("Huawei p50 pocket.");
        phThree.phs(512);
        phThree.php(1699);
        
        System.out.println("Welcom To My Store!\n-------*****-------");
        
        int i = 1;
        
        do {
        System.out.println("Choose a phone please:");
        System.out.println("1-iPhone 13.\n2-Galaxy s22.\n3-Huawei p50 pocket.");
        int a = scan.nextInt();
        System.out.println("-------*****-------");
        
        switch(a) {
            case 1:
                phOne.print();
                System.out.println("1-buy.\n2-go back.");
                int bg1 = scan.nextInt();
                if(bg1 == 1) {
                    System.out.println("Congratulations! we will deliver it to you.");
                    --i;
                }
                else if(bg1 == 2){
                      i;
                }
                else {
                    System.out.println("Please choose 1 or 2.");
                }
                break;
            case 2:
                phTwo.print();
                System.out.println("1-buy.\n2-go back.");
                int bg2 = scan.nextInt();
                if(bg2 == 1) {
                    System.out.println("Congratulations! we will deliver it to you.");
                    --i;
                }
                else if(bg2 == 2){
                      i;
                }
                else {
                    System.out.println("Please choose 1 or 2.");
                }
                break;
            case 3:
                phThree.print();
                System.out.println("1-buy.\n2-go back.");
                int bg3 = scan.nextInt();
                if(bg3 == 1) {
                    System.out.println("Congratulations! we will deliver it to you.");
                   i-=2;
                }
                else if(bg3 == 2){
                      i;
                }
                else {
                    System.out.println("Please choose 1 or 2.");
                }
                break;
            default:
                System.out.println("Please choose 1, 2, or 3.");
        }
        }while(i >= 1);
        
    }
    
}

And this is the rest of the program if you need it.

import java.text.NumberFormat;
public class Phones {
    
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    
    String name;
    int storage;
    double price;
    
    public Phones(String name) {
        this.name = name;
    }
    
    public void phs(int phs) {
        storage = phs;
    }
    
    public void php(double php) {
        price = php;
    }
    
    public void print() {
        System.out.println("Model: " name);
        System.out.println("Storage: " storage "G");
        System.out.println("Price: " nf.format(price));
    }
}

CodePudding user response:

If you want the program to stop after buying a phone you'd want to implement the do-while loop with a boolean value, then after the user buys a phone set the boolean value to false.

boolean loop = true;
    
    do {
    System.out.println("Choose a phone please:");
    System.out.println("1-iPhone 13.\n2-Galaxy s22.\n3-Huawei p50 pocket.");
    int a = scan.nextInt();
    System.out.println("-------*****-------");
    
    switch(a) {
        case 1:
            phOne.print();
            System.out.println("1-buy.\n2-go back.");
            int bg1 = scan.nextInt();
            if(bg1 == 1) {
                System.out.println("Congratulations! we will deliver it to you.");
                loop = false;
            }
......
while(loop);

I'd recommend taking better care of your variable names. Make them obvious and meaningful, makes it easier to debug in the end!

  • Related