0% found this document useful (0 votes)
7 views6 pages

PRA2

The document contains Java code for managing orders and products, with classes for Orders and Products. It includes methods to find the product with the maximum quantity in an order and to list orders by product name. The code also handles input and output for order details and product information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

PRA2

The document contains Java code for managing orders and products, with classes for Orders and Products. It includes methods to find the product with the maximum quantity in an order and to list orders by product name. The code also handles input and output for order details and product information.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.io.

*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

class Orders{
int oId;

public int getoId() {


return oId;
}
String oName;

public String getoName() {


return oName;
}

int noOfProduct;

public int getNoOfProduct() {


return noOfProduct;
}

List<String>products;

public List<String> getProducts() {


return products;
}

List<Integer>quantities;

public List<Integer> getQuantities() {


return quantities;
}

public Orders(int oId, String oName, int


noOfProduct,List<String>products,List<Integer>quantities){
this.oId=oId;
this.oName=oName;
this.noOfProduct=noOfProduct;
this.products=products;
this.quantities=quantities;
}
}
public class OrdersTester {

static void productWithMaxiQuantity(Orders[] o,int orderId){


int max=0;
int maxi=-1;
int count=0;
for(Orders od:o){
if(od.getoId()==orderId){
for(int i=0;i<od.quantities.size();i++){
if(od.quantities.get(i)>max){
max=od.quantities.get(i);
maxi=i;
count++;
}
}
System.out.println(od.products.get(maxi));
}

}
if(count==0){
System.out.println("Order Id Not Found");
}
}
static void orderByProduct(Orders[] o,String pName){
int count=0;
for(Orders od:o){
for(int i=0;i<od.products.size();i++){
if(od.products.get(i).equalsIgnoreCase(pName)){
System.out.println(od.getoId());
System.out.println(od.getoName());
count++;
}
}
}
if(count==0){
System.out.println("Product Not Found");
}
}
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */

Scanner sc=new Scanner (System.in);


int n=sc.nextInt();
Orders[] o= new Orders[n];
for(int i=0;i<n;i++){
int oId=sc.nextInt();sc.nextLine();
String oName=sc.nextLine();
int noOfProduct=sc.nextInt();sc.nextLine();
List<String>pt=new ArrayList<>();
List<Integer>qt=new ArrayList<>();
for(int j=0;j<noOfProduct;j++){
String products=sc.nextLine();
int quantities=sc.nextInt();sc.nextLine();
pt.add(products);
qt.add(quantities);
}
o[i]= new Orders(oId, oName, noOfProduct, pt, qt);
}

int orderId=sc.nextInt();sc.nextLine();
String pName=sc.nextLine();
productWithMaxiQuantity(o,orderId);
orderByProduct(o,pName);
}
}

----------------------------------------------------------------

package PRA;
import java.util.*;

class Orders{
int oId;

public int getoId() {


return oId;
}
String oName;

public String getoName() {


return oName;
}

int noOfProduct;

public int getNoOfProduct() {


return noOfProduct;
}

List<String>bTitle;

public List<String> getBTitle() {


return bTitle;
}

List<Double>price;

public List<Double> getPrice() {


return price;
}

public Orders(int oId, String oName, int


noOfProduct,List<String>bTitle,List<Double>price){
this.oId=oId;
this.oName=oName;
this.noOfProduct=noOfProduct;
this.bTitle=bTitle;
this.price=price;
}
}
public class Main {

static void mostExpBook(Orders[] o,int orderId){


double max=0;
int maxi=-1;
int count=0;
for(Orders od : o){
if(od.getoId()==orderId){
for(int i=0;i<od.price.size();i++){
if(od.price.get(i)>max){
max=od.price.get(i);
maxi=i;
count++;
}
}
System.out.println(od.bTitle.get(maxi));
System.out.println(od.price.get(maxi));
}
}
if(count==0){
System.out.println("no book found");
}

static void avgPriceOfBook(Orders[] o,int orderId){


double sum=0;
int count=0;
for(Orders od : o){
if(od.getoId()==orderId){
for(int i=0;i<od.price.size();i++){
sum=sum+od.price.get(i);
count++;
}
}
}
double avg=(sum/count);
if(count==0){
System.out.println("no order found");
}else{
System.out.print(avg);
}

public static void main(String args[] ) throws Exception {


/* Enter your code here. Read input from STDIN. Print output to STDOUT */

Scanner sc=new Scanner (System.in);


int n=sc.nextInt();
Orders[] o= new Orders[n];
for(int i=0;i<n;i++){
int oId=sc.nextInt();sc.nextLine();
String oName=sc.nextLine();
int noOfProduct=sc.nextInt();sc.nextLine();
List<String>bt=new ArrayList<>();
List<Double>p=new ArrayList<>();
for(int j=0;j<noOfProduct;j++){
String bTitle=sc.nextLine();
Double price=sc.nextDouble();sc.nextLine();
bt.add(bTitle);
p.add(price);

}
o[i]= new Orders(oId, oName, noOfProduct, bt, p);
}

int orderId=sc.nextInt();
// String pName=sc.nextLine();
mostExpBook(o,orderId);
avgPriceOfBook(o,orderId);
}
}
-----------------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class OrdersTester {


public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner in = new Scanner(System.in);
int n=in.nextInt();
ArrayList<Order> ord = new ArrayList<>();
for(int i=0; i<n; i++){
int Oid=in.nextInt(); in.nextLine();
String Oname = in.nextLine();

int noOfProducts = in.nextInt();


in.nextLine();
ArrayList<Product> prod = new ArrayList<>();
for(int j=0; j<noOfProducts; j++){
String products= in.nextLine();
int quantity = in.nextInt();
in.nextLine();
prod.add(new Product(products, quantity));
}
ord.add(new Order(Oid, Oname, noOfProducts, prod));
}
//
int oid= in.nextInt(); in.nextLine();
String pname = in.nextLine();
MaxQuantityProds(ord, oid);
OrderByProdName(ord, pname);
}
public static void MaxQuantityProds(ArrayList<Order> ord, int oid){
boolean flag = false;
int max =0;
String str="";
for(Order o: ord){
if(o.getOid() == oid){
flag = true;
for(Product p: o.getProdList()){
if(p.getQuantity() > max){
max = p.getQuantity();
str = p.getProducts();
}
}
}
}
if(!flag){
System.out.println("Order Id Not Found");
}
else{
System.out.println(str);
}
}
public static void OrderByProdName(ArrayList<Order> ord, String pname){
boolean flag = false;
for(Order o: ord){
for(Product p: o.getProdList()){
if(p.getProducts().equalsIgnoreCase(pname)){
flag = true;
System.out.println(o.getOid());
System.out.println(o.getOname());
}
}
}
if(!flag){
System.out.println("Product Not Found");
}
}
}
class Order{
int Oid;
String Oname;
int noOfProducts;
List<Product> prodList;
public int getOid() {
return Oid;
}
public String getOname() {
return Oname;
}
public int getNoOfProducts() {
return noOfProducts;
}
public List<Product> getProdList() {
return prodList;
}
public Order(int Oid, String Oname,int noOfProducts,List<Product> prodList){
this.Oid=Oid;
this.Oname= Oname;
this.noOfProducts=noOfProducts;
this.prodList=prodList;
}
}
class Product{
String products;
int quantity;
public String getProducts() {
return products;
}
public int getQuantity() {
return quantity;
}
public Product(String products, int quantity){
this.products=products;
this.quantity=quantity;
}
}

You might also like