Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Shopping cart help with code

  1. #1
    Junior Member
    Join Date
    Sep 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Shopping cart help with code

    Hello, i need to get this shopping cart to work and i need help at creating a place to save the data after the "Enter product move".
    This is the code so far. I need this step to move on with delete, update, search
    Thanks in advance!

    import java.util.Scanner;

    public class Shoppingcart {

    private static Scanner scanner = new Scanner(System.in);
    private static String[] menuItems
    = { "Exit", "Insert Item", "Update Item", "Search Item", "Delete Item"
    };


    private static Products productList;
    private static String item;

    static void printMenu() {
    int i = 0;
    for (String item : menuItems) {
    System.out.printf("%d -> %s\n", i, item);
    i++;
    }
    }


    static int requestAction() {
    int choice = 0;
    printMenu();
    do {
    System.out.print("Choose a number from the menu: ");
    if (scanner.hasNextInt())
    choice = scanner.nextInt();
    scanner.nextLine(); //
    } while (choice < 0 || choice >= menuItems.length);
    return choice;
    }

    public static void main (String[] args){
    int test;
    do {
    test = requestAction();
    commander(test);
    } while (test != 0);
    System.out.println("Exit from the app"); //
    }

    private static void commander(int n) {
    switch (n) {
    case 1: insertItem();
    break;
    // case 2: updateitem();
    // break;
    // case 3: searchitem();
    // break;
    // case 4: deleteitem();

    }
    }

    static void insertItem() {
    System.out.print("Enter product: ");
    String item = scanner.nextLine();
    System.out.println(item);
    }

    public class Products {
    private String[] [] items = new String[50][1];
    private int nextAvail = 0;

    public void insertItem(String[] item) {
    items[nextAvail] = item;
    nextAvail++;
    }

    public boolean isFull() {
    return (nextAvail >= items.length);
    }
    }

    public int findProduct(String item) {
    Object[] products = null;
    int nextAvail = 0;
    for (int i = 0; i < nextAvail; i++)
    if (products[i].equals(item) )
    return i;
    return -1;
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Shopping cart help with code

    need help at creating a place to save the data
    Would a simple text file work to save the data between program executions?

    The code needs some comments describing what it is doing and how it is going to do it.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. simple shopping cart
    By dodda911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2013, 08:25 AM
  2. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2012, 01:40 PM
  3. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2012, 12:08 PM
  4. Need help with simple Shopping cart math
    By I hate Java! in forum Loops & Control Statements
    Replies: 3
    Last Post: November 6th, 2011, 10:46 PM
  5. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 03:37 PM