basic java read string to list and other functionality

Flenters :

I am trying to learn java so total newbie here. Lets say for example we have a string

String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";

What I need to do is create a console based application to list all these items then add multiple items and display the price.

This is the code I have tried. I can add the numbers, I just don't know how to take the input from the user to a variable.

For example when I run the console application I want the user to input as many burgers as he wants. Then he is done he selects number 3.

Please be gentle

import java.util.Scanner;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";
        //split
        String [] elements = file.split(";");
        //convert string to list of string
        List<String> fixedLengthList = Arrays.asList(elements);
        //copy fixed to arraylist
        ArrayList<String> listOfString = new ArrayList<String>(fixedLengthList);

       int  p = 0;
        while (p != 5) {
            Scanner sc = new Scanner(System.in);
            for (String line : listOfString
            ) {
                System.out.println("1. Cheese burger 50");
                System.out.println("2. Pizza 80");
                System.out.println("3. View your bill");
                sc.nextInt();
                String[] items = line.split(",");
                String itemPriceString = items[2];
                double itemPrice = Double.parseDouble(itemPriceString);
                double burger = itemPrice;
                System.out.println(burger+burger);

            }
        }

   }
}
J4BEZ :

Well, I'm really sorry that I don't understand the problem exactly, but I'd like to recommend some tips to manage similar variables in a bundle.

Like the data structure in C++, such as the pair and tupe, java has a method of declaring 'Class'

 class Menu {
int order;       //for food order number
 String name;    // for food name,
int price;       //for food price

Menu(String name, int price){
  //following the 'Constructor' We can put data
  this.name = name;
  this.price = price;
  }

}

Then, how can we use it?

By using List in this way,

       String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";

    //split
    String [] elements = file.split(";");

    List<Menu> foodMenu = new ArrayList<Menu>();        //init

    for(int i = 0; i< 4; i++) { //4 foods
        String [] line = elements[i].split(",");

        foodMenu.add(new Menu(Integer.parseInt(line[0]), //food order number
                        line[1],                         //food name
                        Integer.parseInt(line[2]))       //food price
                );
    }
   /*foodMenu[0] = 1, Cheese Burger, 50
    foodMenu[1] = 2, Pizza, 70 
    foodMenu[2] = 3, Coke, 20
    foodMenu[3] = 4, Beer, 20

    if you want to access a data you can use like this
    foodMenu.get(index).order / name / price ; */

Now, let's try through this new storage system let customers choose what to buy and pay for

import java.util.Scanner;
import java.util.*;

class Menu { 
    int order;      //for food order number
    String name;    // for food name
    int price;      //for food price

    Menu(int order, String name, int price){
        //following the 'Constructor' We can put data
        this.order = order;
        this.name = name;
        this.price = price;
    }

}


public class StackOver {
    public static void main(String[] args) {
        String file = "1,Cheese Burger,50;2,Pizza,70;3,Coke,20;4,Beer,20";

        //split
        String [] elements = file.split(";");

        List<Menu> foodMenu = new ArrayList<Menu>();        //init

        for(int i = 0; i< 4; i++) { //4 foods
            String [] line = elements[i].split(",");

            foodMenu.add(new Menu(Integer.parseInt(line[0]), //food order number
                            line[1],                         //food name
                            Integer.parseInt(line[2]))       //food price
                    );
        }
       //foodMenu[0] = 1, Cheese Burger, 50
      //foodMenu[1] = 2, Pizza, 70 
      //foodMenu[2] = 3, Coke, 20
      //foodMenu[3] = 4, Beer, 20

      //if you want to access a data you can use like this
      // foodMenu.get(index).order / name / price ;

   Scanner sc = new Scanner(System.in);


    int price = 0; //for purchase price
    int  p = 0; //Great Init for avoiding NullPointerException
    while (p != 5) {
            System.out.println("Please Input number 1 to 5 to Order food or purchase");
            System.out.println("1. Cheese burger 50$");
            System.out.println("2. Pizza 80$");
            System.out.println("3. Coke 20$");
            System.out.println("4. Beer 20$");
            System.out.println("5. Beer 20$");

            p = sc.nextInt();   //customer will choose number

            if(p!=5) {  
                price += foodMenu.get(p-1).price;
                //add price of selected food!
            }

        }
    System.out.println("Ok, The price is "+price+"$ in total");
    System.out.println("Have a nice day!");
    }
}

enter image description here ^ExampleResult

I hope this answer will help you a little bit and if it's hard to understand, please leave a comment! Have a peaceful day!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216726&siteId=1