Kytcs.Blogspot.com: Java Arrays - Hands on 2

Java Arrays - Hands on 2

Java Arrays - Hands on 2
Problem 1: 

import java.util.Scanner;
public class Solution {
public static void main(String args[] ) throws Exception {

Shirt[] shirts = new Shirt[5];
Scanner sc = new Scanner(System.in);
for(int i = 0;i<5;i++)
{
int tag = sc.nextInt();
sc.nextLine();
String brand = sc.nextLine();
double price = sc.nextDouble();
sc.nextLine();
char g = sc.nextLine().charAt(0);
shirts[i] = new Shirt(tag,brand,price,g);
}
double price = sc.nextDouble();

for(Shirt s: shirts)
{
System.out.println(getDiscountPrice(s));
}
Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);
for(Shirt s: result)
{
if(s.getTag()!=0)
System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
}

}
public static double getDiscountPrice(Shirt s)
{
double discount;
if(s.gender=='m')
discount=10;
else if(s.gender=='f')
discount=20;
else if(s.gender=='u')
discount=30;
else
discount=0;
return s.price-(discount*s.price)/100;
}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt [] shirts, double price)
{
Shirt[] res = new Shirt[5];
for(int i=0;i<res.length;i++)
res[i]=new Shirt(0,"",0,'c');
int j=0;
for(int i=0;i<shirts.length;i++)
{
if(shirts[i].price>price)
{
res[j++]= new Shirt(shirts[i].tag,shirts[i].brand,shirts[i].price,shirts[i].gender);
}
}
return res;
}
}
class Shirt
{
int tag;
String brand;

double price;
char gender;
Shirt(int tag, String brand, double price, char gender)
{
this.tag=tag;
this.brand=brand;
this.price=price;
this.gender=gender;
}
public int getTag()
{
return this.tag;
}
public void setTag(int tag)
{
this.tag=tag;
}
public String getBrand()
{
return this.brand;
}
public void setBrand(String brand)
{
this.brand=brand;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
public char getGender()
{
return this.gender;
}
public void setGender(char gender)
{

this.gender=gender;
}
}

No comments:

Post a Comment

Followers

Ad Space