3
\$\begingroup\$

I wrote this program to implement a shopping cart. It supports adding/removing items to the cart, viewing the current order, and completing the checkout process.

import java.util.Scanner;

public class Keychains1 {

    public static Scanner keyboard = new Scanner(System.in);
    public static boolean stay = true;


    public static void add_keychains() {
        System.out.println("ADD KEYCHAINS");
    }

    public static void remove_keychains() {
        System.out.println("REMOVE KEYCHAINS");
    }

    public static void view_order() {
        System.out.println("VIEW ORDER");
    }

    public static void checkout() {
        System.out.println("CHECKOUT");
        stay = false;
    }


    public static void main(String[] args) {

        int choice;     

        while (stay) {

            System.out.println("Ye Olde Keychain Shoppe");
            System.out.println();

            System.out.println("1. Add Keychains to Order");
            System.out.println("2. Remove Keychains from Order");
            System.out.println("3. View Current Order");
            System.out.println("4. Checkout");
            System.out.println();
            System.out.print("Please enter your choice: ");

            choice = keyboard.nextInt();

            System.out.println();

            if (choice == 1) {
                add_keychains();
            }
            else if (choice == 2) {
                remove_keychains();
            }
            else if (choice == 3) {
                view_order();
            } 
            else if (choice == 4) {
                checkout();
            }
            else {
                System.out.println("Error. Please choose again.");
            }
            System.out.println();
        }

    }
}
\$\endgroup\$
1
\$\begingroup\$

Naming

I can't begin with another topic.

You use 2 different approaches to name your variables:

  1. someName -> it's a camelCase
  2. some_name -> it's a snake_case

Read this article to understand the difference

Java has own code style and you should use only camelCase for your variable name, class name, method name

I'll provide more examples about naming in Java. Let's start with simple variable (not final, because final variable has own name convention)

  1. someName - for variable (not final variable) name
  2. someMethod - for method name
  3. SomeClass - for class name

You may have final variable, it's commonly use in case when you need the property that will be general for all objects of the class and this property will not change.

private static int SOME_IMMUTABLE_VARIABLE = 10;

You need to fix names for your variables and methods:

remove_keychains -> removeKeyChains() // in the same manner for others
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.