/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.mycompany.mavenproject1; /** * * @author thatv */ import java.util.ArrayList; import java.util.List; public class Product { private int id; private String title; private double price; private int quantity; private double total; private double discountPercentage; private double discountedPrice; public Product(int id, String title, double price, int quantity, double total, double discountPercentage, double discountedPrice) { this.id = id; this.title = title; this.price = price; this.quantity = quantity; this.total = total; this.discountPercentage = discountPercentage; this.discountedPrice = discountedPrice; } // getters and setters omitted for brevity } public class Main { public static void main(String[] args) { String text = "id:59,title:Spring and summershoes,price:20,quantity:3,total:60,discountPercentage:8.71,discountedPrice:55\n" + "id:88,title:TC Reusable Silicone Magic Washing Gloves,price:29,quantity:2,total:58,discountPercentage:3.19,discountedPrice:56"; List<Product> products = new ArrayList<>(); String[] lines = text.split("\\n"); for (String line : lines) { String[] attributes = line.split(","); int id = Integer.parseInt(attributes[0].split(":")[1]); String title = attributes[1].split(":")[1]; double price = Double.parseDouble(attributes[2].split(":")[1]); int quantity = Integer.parseInt(attributes[3].split(":")[1]); double total = Double.parseDouble(attributes[4].split(":")[1]); double discountPercentage = Double.parseDouble(attributes[5].split(":")[1]); double discountedPrice = Double.parseDouble(attributes[6].split(":")[1]); Product product = new Product(id, title, price, quantity, total, discountPercentage, discountedPrice); products.add(product); } // Do something with the list of products... } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter