[text] jshirt

Viewer

  1. import java.util.Scanner;
  2.  
  3. public class Solution {
  4.     public static void main(String args[] ) throws Exception {
  5.         /* Do not alter code in main method */
  6.         Shirt[] shirts = new Shirt[5];
  7.         
  8.         Scanner sc = new Scanner(System.in);
  9.         
  10.         for(int i = 0;i<5;i++)
  11.         {
  12.             int tag = sc.nextInt();sc.nextLine();
  13.             String brand = sc.nextLine();
  14.             double price = sc.nextDouble();sc.nextLine();
  15.             char g = sc.nextLine().charAt(0);
  16.             shirts[i] = new Shirt(tag,brand,price,g);
  17.         }
  18.         
  19.         
  20.         char g = sc.nextLine().charAt(0);
  21.         String brand = sc.nextLine();
  22.         
  23.         for(Shirt s: shirts)
  24.         {
  25.             System.out.println(getDiscountPrice(s));            
  26.         }
  27.         
  28.         Shirt[] result = getShirtForBrand(shirts,g,brand);
  29.         
  30.         for(Shirt s: result)
  31.         {
  32.             System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
  33.         }
  34.     }
  35.  
  36.     private static Shirt[] getShirtForBrand(Shirt[] shirts, char g, String brand) {
  37.         int c=0;
  38.         int count=0;
  39.         for(int i=0;i<shirts.length;i++) {
  40.             if(shirts[i].getG() == g && shirts[i].getBrand().equalsIgnoreCase(brand) ) {
  41.                 count++;
  42.             }
  43.             
  44.         }
  45.         
  46.         Shirt[] shrt=new Shirt[count];
  47.         for(int i=0;i<shirts.length;i++) {
  48.             if(shirts[i].getG() == g && shirts[i].getBrand().equalsIgnoreCase(brand) ) {
  49.                 shrt[c]=shirts[i];
  50.                 c++;
  51.             }
  52.             
  53.         }
  54.         
  55.         for(int i=0;i<shrt.length;i++) {
  56.             for(int j=i+1;j<shrt.length;j++) {
  57.                 if(shrt[i].getPrice()>shrt[j].getPrice()) {
  58.                     Shirt temp =shrt[i];
  59.                     shrt[i]=shrt[j];
  60.                     shrt[j]=temp;
  61.                 }
  62.                 
  63.             }    
  64.         }
  65.         return shrt;
  66.     }
  67.  
  68.     private static double getDiscountPrice(Shirt s) {
  69.         double res =0;
  70.         if(s.getG() == 'm') {
  71.             res=s.getPrice()-s.getPrice()*0.10;
  72.         }
  73.         
  74.         if(s.getG() == 'f') {
  75.             res=s.getPrice()-s.getPrice()*0.20;
  76.         }
  77.         
  78.         if(s.getG() == 'u') {
  79.             res=s.getPrice()-s.getPrice()*0.30;
  80.         }
  81.         return res;
  82.     }
  83.         
  84.     
  85.     /* implement your methods here*/
  86. }
  87.  
  88.  
  89. class Shirt {
  90.     private int tag ;
  91.     private String brand ;
  92.     private double price ;
  93.     private char g ;
  94.     public int getTag() {
  95.         return tag;
  96.     }
  97.     public void setTag(int tag) {
  98.         this.tag = tag;
  99.     }
  100.     public String getBrand() {
  101.         return brand;
  102.     }
  103.     public void setBrand(String brand) {
  104.         this.brand = brand;
  105.     }
  106.     public double getPrice() {
  107.         return price;
  108.     }
  109.     public void setPrice(double price) {
  110.         this.price = price;
  111.     }
  112.     public char getG() {
  113.         return g;
  114.     }
  115.     public void setG(char g) {
  116.         this.g = g;
  117.     }
  118.     public Shirt(int tag, String brand, double price, char g) {
  119.         super();
  120.         this.tag = tag;
  121.         this.brand = brand;
  122.         this.price = price;
  123.         this.g = g;
  124.     }

Editor

You can edit this paste and save as new:


File Description
  • jshirt
  • Paste Code
  • 29 Jan-2020
  • 3.22 Kb
You can Share it: