Define a class Garments with the following description :
- Members are : GCode of type String, GType of type String, GSize of type integer, GFabric of type string, GPrice of type float.
- Member functions :
- A function Assign() which calculates and assigns the value of GPrice as follows:
- For the value of GFabric “COTTON”,
| GType | GPrice |
| Trouser | 1300 |
| Shirt | 1100 |
- For GFabric other than “COTTON” the above mentioned GPrice gets reduced by 10%.
- A constructor to assign initial values of GCode, GType and GFabric with the word “Not Allowed” and GSize and GPrice with 0.
- A function Input() to input the values of the data members GCode, Gtype, GSize and GFabric and invoke the Assign() function.
- A function Display() which displays the content of all the data members for a Garment.
Solution
import java.util.*;
class Garments
{
String GType;
String GCode;
int GSize;
String GFabric;
float GPrice;
//define a constructor
Garments()
{
GCode="Not Allowed";
GType="Not Allowed";
GFabric="Not Allowed";
GSize=0;
GPrice=0.0f;
}
public void Assign(){
if(GFabric.equals("Cotton"))
{
if(GType.equals("Trouser"))
GPrice=1300;
if(GType.equals("Shirt"))
GPrice=1100;
}
else
{
if(GType.equals("Trouser"))
GPrice=1170;
if(GType.equals("Shirt"))
GPrice=990;
}
}
public void Input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Garment Code : ");
GCode=sc.next();
System.out.println("Enter Garment type : ");
GType=sc.next();
System.out.println("Enter Garment Fabric : ");
GFabric=sc.next();
System.out.println("Enter Garment Size : ");
GSize=sc.nextInt();
Assign();
}
public void Display(){
System.out.println("Garment Code : "+GCode);
System.out.println("Garment Type : "+GType);
System.out.println("Garment Fabric : "+GFabric);
System.out.println("Garment Size : "+GSize);
System.out.println("Garment Price : "+GPrice);
}
}
class GarmentsData
{
public static void main(String args[])
{
Garments gar=new Garments();
gar.Input();
gar.Display();
}
}
Save the above Program with “GarmentsData.Java” then compile it with the following commands:
javac GarmentsData.java // to compile the code java GarmentsData // to run the code
Output is :
Enter Garment Code : 101 Enter Garment type : Shirt Enter Garment Fabric : Cotton Enter Garment Size : 32 Garment Code : 101 Garment Type : Shirt Garment Fabric : Cotton Garment Size : 32 Garment Price : 1100.0