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 12 of 12

Thread: Looking for some help on a program

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looking for some help on a program

    I'm attempting to make a basic program for a pizza truck just to keep track of orders.
    Currently, most of the program works. but when i want to print out my transaction data the transaction ID and order ID stay the same and ive honestly hit a hump. just need some fresh eyes.

    attached is a zipped file of my code.

    Thank you in advance.

    PizzaApp.zip

  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: Looking for some help on a program

    when i want to print out my transaction data the transaction ID and order ID stay the same
    Can you post the code that shows the problem?

    Be sure to wrap all posted code in 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.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for some help on a program

    Are you looking for just the segment of code i think has a problem in it, or the whole program?

    --- Update ---

    import java.util.Scanner;
    import java.util.ArrayList;
     
    public class Main {
     
        private int cCount = 0;
     
     
        private static Scanner scnr = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            Main main = new Main();
     
            final char EXIT_CODE = 'E';
            final char CUST_CODE = 'C';
            final char MENU_CODE = 'M';
            final char ORDE_CODE = 'O';
            final char TRAN_CODE = 'T';
            final char CUST_PRNT = 'P';
     
            char userAction;
     
            final String PROMPT_ACTION = "Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: ";
     
            ArrayList<Customer> cList = new ArrayList<>();
            ArrayList<Menu> mList = new ArrayList<>();
            ArrayList<Order> oList = new ArrayList<>();
            ArrayList<Transaction> tList = new ArrayList<>();
     
            Order order1 = new Order(0);
            Transaction trans1 = new Transaction(1, order1, PaymentType.cash);
     
            Menu menu1 = new Menu(1, "Regular Cheese" , 10.0);
            Menu menu2 = new Menu(2, "Meat Lover", 15.0);
            Menu menu3 = new Menu(3, "Margarita", 13.0);
            Menu menu4 = new Menu(4, "Garlic", 13.0);
            Menu menu5 = new Menu(5, "Hawaiian", 13.0);
            Menu menu6 = new Menu(6, "White", 12.0);
     
            mList.add(menu1); //Cheese
            mList.add(menu2); //Meat Lovers
            mList.add(menu3); //Margarita
            mList.add(menu4); //Garlic
            mList.add(menu5); //Hawaiian
            mList.add(menu6); //White
     
            userAction = getAction(PROMPT_ACTION);
     
            while (userAction != EXIT_CODE) {
     
                switch (userAction) {
     
                    case CUST_CODE:
                        cList.add(main.addCustomer(cList));
                        break;
     
                    case CUST_PRNT:
                        Customer.printCustomer(cList);
                        break;
     
                    case MENU_CODE:
                        Menu.listMenu(mList);
                        System.out.println("");
                        break;
     
                    case ORDE_CODE:
     
                        System.out.print("ENTER CUSTOMER #: " );
     
                        int id = scnr.nextInt();
     
                        ArrayList<Menu> cMenu = selectMenu(mList);
                        Order.addOrders(order1, cList.get(id), cMenu);
                        oList.add(order1);
                        trans1 = payment(order1);
                        tList.add(trans1);
                        break;
     
                    case TRAN_CODE:
                        Transaction.listTransactions(tList);
                        break;
                }
     
                userAction = getAction(PROMPT_ACTION);
     
            }
     
        }
     
        private static Transaction payment(Order order1) {
     
            double total = 0;
            double amount;
     
            //Order printout
            for(Menu menu : order1.getMenuItem()){
     
                System.out.print(menu.getQuantity());
                System.out.print("    ");
                System.out.print(menu.getmenuItem());
                System.out.print(" $");
                amount = menu.getQuantity()*menu.getitemPrice();
                total = total + amount;
                System.out.println(amount);
            }
     
            System.out.print("Total: $");
            System.out.println(total);
            System.out.println("");
     
            //Payment Type
            int option;
            Transaction t;
            while(true){
                System.out.println("[1] CASH      [2] CREDIT");
                option = scnr.nextInt();
                if(option==1){
                    t = new Transaction(order1.getorderId(), order1, PaymentType.cash);
                    System.out.println("CASH SELECTED\n");
                    return t;
                }
                else if(option==2){
                    t = new Transaction(order1.getorderId(), order1, PaymentType.credit);
                    System.out.println("CREDIT SELECTED\n");
                    return t;
                }
     
            }
        }
     
        public static ArrayList<Menu> selectMenu(ArrayList<Menu> menus){
            System.out.println("Select order (Press 0 to finish order)");
     
            for (Menu menu : menus)
                System.out.println("'" + menu.getmenuId() + "' for " + menu.getmenuItem());
            int check;
     
            ArrayList<Menu> menus1 = new ArrayList<>();
            while(true) {
                check = scnr.nextInt();
                if(check == 0)
                    break;
                System.out.print("Add quantity: " );
                int quantity = scnr.nextInt();
                Menu item = menus.get(check-1);
                item.setQuantity(quantity);
                menus1.add(item);
            }
            return menus1;
        }
     
        public static char getAction(String prompt) {
            //removed scanner object from here
            String answer = "";
            System.out.println(prompt);
            answer = scnr.nextLine().toUpperCase() + " ";
            char firstChar = answer.charAt(0);
            return firstChar;
        }
     
        public Customer addCustomer(ArrayList<Customer> cList) {
            Customer cust = null;
     
            cust = new Customer(cCount++);
            System.out.println("Please Enter Customer Name: ");
            cust.setCustomerName(scnr.nextLine());
            System.out.println("Please Enter Customer Buzzer #: ");
            cust.setCustomerPhoneNumber(scnr.nextLine());
     
            return cust;
        }
     
    }

    import java.util.*;
    public class Order {
     
        //Class Level Variables - Protect the data
        private int orderId;
        private Customer cust;
        private ArrayList<Menu> menuItem;
        private static ArrayList<Order> orderList = new ArrayList<>();
     
        //Constructor Method
        public Order(int _orderId) {
            this.orderId = _orderId;
        }
     
        //Setters and Getters
        public int getorderId() {
            return orderId;
        }
     
        public void setorderId(int _orderId) {
            this.orderId = _orderId;
        }
     
        public static void addOrders(Order order, Customer customer, ArrayList<Menu> mList){
            order.setorderId(order.getorderId());
            order.setCust(customer);
            order.setMenuItem(mList);
            orderList.add(order);
     
            System.out.println("\nOrder added to customer #: " + order.getorderId());
            System.out.println("\nCustomer Name : " + order.getCust().getCustomerName()
                    + ", Customer ID : " + order.getCust().getCustomerId());
        }
     
        public ArrayList<Menu> getMenuItem() {
            return menuItem;
        }
     
        public void setMenuItem(ArrayList<Menu> menuItem) {
            this.menuItem = menuItem;
        }
     
        public Customer getCust() {
            return cust;
        }
     
        public void setCust(Customer cust) {
            this.cust = cust;
        }
     
    }

  4. #4
    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: Looking for some help on a program

    Sorry, I also need the input so the code can be executed the same way as you do when you get the problem.
    Also post the contents of the console that shows the input and the program's output. Add some comments to the output that describe the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for some help on a program

    https://imgur.com/ZAiIBcD
    The image above is the first set of inputs/outputs of the program.
    https://imgur.com/WksKycQ
    The image above is the second set of inputs/outputs of the program. If you take a look at the second image when transaction is called both orders have the same data. Also it keeps printing the user menu twice after it asks for cash or credit.
    Attached Images Attached Images

  6. #6
    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: Looking for some help on a program

    Please copy the contents of the console and paste it here so its contents can be included in responses as needed.
    Don't post images.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for some help on a program

    "C:\Program Files\Java\jdk1.8.0_212\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\lib\idea_rt.jar=60438:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_212\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_212\jre\lib\rt.jar;C:\Users\Orion Barbounis\Downloads\Lab9Barbounis\out\production\Lab9Barbounis" Main
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    c
    Please Enter Customer Name: 
    Orion
    Please Enter Customer Buzzer #: 
    1
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    p
     
    Customer #: 0
    Customer Name: Orion
    Customer Buzzer #: 1
     
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    o
    ENTER CUSTOMER #: 0
    Select order (Press 0 to finish order)
    '1' for Regular Cheese
    '2' for Meat Lover
    '3' for Margarita
    '4' for Garlic
    '5' for Hawaiian
    '6' for White
    1
    Add quantity: 2
    0
     
    Order added to customer #: 0
     
    Customer Name : Orion, Customer ID : 0
    2    Regular Cheese $20.0
    Total: $20.0
     
    [1] CASH      [2] CREDIT
    1
    CASH SELECTED
     
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    t
    Transaction ID: 0
    Order:0
    Payment Type: cash
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    c
    Please Enter Customer Name: 
    Norm
    Please Enter Customer Buzzer #: 
    2
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    p
     
    Customer #: 0
    Customer Name: Orion
    Customer Buzzer #: 1
     
     
    Customer #: 1
    Customer Name: Norm
    Customer Buzzer #: 2
     
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    o
    ENTER CUSTOMER #: 1
    Select order (Press 0 to finish order)
    '1' for Regular Cheese
    '2' for Meat Lover
    '3' for Margarita
    '4' for Garlic
    '5' for Hawaiian
    '6' for White
    1
    Add quantity: 2
    0
     
    Order added to customer #: 0
     
    Customer Name : Norm, Customer ID : 1
    2    Regular Cheese $20.0
    Total: $20.0
     
    [1] CASH      [2] CREDIT
    1
    CASH SELECTED
     
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: 
    Add 'C'ustomer, 'P'rint Customer, List 'M'enu, Add 'O'rder, List 'T'ransaction or 'E'xit: <<<<<<<<<<< This second menu should not be here.
     
     
    t
    Transaction ID: 0
    Order:0
    Payment Type: cash  
     
     
    Transaction ID: 0   <<<<< This should be 1         
    Order:0                 <<<<< This should be 1
    Payment Type: cash

    I'm sorry do you mean like this?
    Last edited by Orion; June 16th, 2019 at 06:53 PM.

  8. #8
    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: Looking for some help on a program

    Yes, that is better.
    Now where are the comments that point to what is wrong with the output and that show what the correct output should be?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for some help on a program

    I have placed the comments inside the console.

  10. #10
    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: Looking for some help on a program

    How do I find the comments? Do they have marking Strings like >>>>>>> the comment here <<<<<<<<

    For example:

    value1: 22 <<<<<<<< This should be 33

    value3: 1 <<<<<<<<< this should be 23
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2019
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looking for some help on a program

    I changed it to be like your examples
    Last edited by Orion; June 16th, 2019 at 06:54 PM.

  12. #12
    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: Looking for some help on a program

    So the output values should be something other than 0. Is that the problem? Is 0 the default values for the variables?
    Where are the variables that are being shown given values? Is the code that gives them values being executed?

    Listed above is two printouts of the user menu. This should only be one print out
    Add a default case in the switch statement that prints out the value of userAction when it doesn't match any of the case statements.
    Note: It is always good to have a default that prints a message when the expected case statements don't catch a value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. switch program problem check my program and help how to fix
    By robel in forum Other Programming Languages
    Replies: 2
    Last Post: August 18th, 2014, 04:08 PM
  2. Replies: 8
    Last Post: August 17th, 2014, 12:31 PM
  3. Replies: 1
    Last Post: February 6th, 2014, 01:11 PM
  4. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  5. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM