Hello everyone! So I am rather new to Java Programming, still trying to get the hang of using concepts like arrays, objects, constructors, and creating new methods. I'm needing someone to help me understand what is going on in the following code my classmate created for a group project I am in. We have to present it in class and I want to be able to understand and explain the code if called upon. Any assistance in giving me some insight would be MUCH appreciated! P.S. This is an item that we have to sell, so we have a Tshirt class, a shopping list class, and Tshirt shore class.
//first class
import java.text.NumberFormat;
public class tShirt
{
private String[] styles = {"Tanktop","T-Shirt","Button Up Shirt",""};
private String[] genders = {"Men’s Department","Unisex Department","Women's Department",""} ;
private double prices;
private String[] colors = {"White","Red","Blue","Green","Yellow",""};
private String[] sizes = {"Small","Medium","Large",""};
private int quantities;
private int choice;
public tShirt()
{
this.styles[3]="No Choice Selected";
prices = 6;
this.colors[5] = "No Choice Selected";
this.sizes[3] = "No Choice Selected";
quantities = 1;
choice = 1;
}
public tShirt(String[] style, String[] color, String[] size, int quantity,double price)
{
styles = style;
prices = price;
colors = color;
sizes = size;
quantities = quantity;
}
public void setQuantities(int quantities)
{
this.quantities=quantities;
}
public void setStyles(int choice)
{
if(choice<4)
{
styles[3]=styles[choice-1];
prices *= (choice+1.0)/2.0);
}
else
{
prices = 0;
styles[3]="Invalid Entry";
}
}
public void setColors(int choice)
{
if(choice<6)
{
colors[5]=colors[choice-1];
}
else
{
prices=0;
colors[5]="Invalid Entry";
}
}
public void setSizes(int choice)
{
if(choice<4)
{
sizes[3]=sizes[choice-1];
}
else
{
sizes[3]="Invalid Entry";
prices=0;
}
}
public void setGenders(int choice)
{
if(choice<4)
{
genders[3]=genders[choice-1];
prices+=choice;
}
else
{
genders[3]="Invalid Entry";
prices=0;
}
}
public int getQuantities()
{
return quantities;
}
public String getStyles()
{
return styles[choice];
}
public double getPrices()
{
return prices;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(prices) + "\t\t" + quantities + "\t\t";
description += sizes[3] + "\t" + colors[5] + "\t" + styles[3];
return description;
}
}
//second class
import java.text.NumberFormat;
public class shoppingList
{
public tShirt[] list;
private int totalCount;
private double totalCost;
private int count;
public shoppingList()
{
list = new tShirt[100];
totalCount=0;
totalCost=0;
count=0;
}
public void addtShirt()
{
if(totalCount==list.length)
{
increaseListSize();
}
list[totalCount] = new tShirt ();
totalCount++;
}
public void addtShirt(String[] styles, String[] colors, String[] sizes, int quantities,double prices)
{
if(totalCount==list.length)
{
increaseListSize();
}
list[totalCount] = new tShirt (styles,colors,sizes,quantities,prices);
totalCost+=prices;
totalCount++;
count+=quantities;
}
public int getTotalCount()
{
return totalCount;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "````````````````````````````````````````\n";
report += "Your Shopping List\n\n";
report += "Your total comes to " + fmt.format(totalCost) + ".\n";
report += "You have " + count + " items in your shopping cart. \n";
report += "Shopping Cart Contents:\n\n";
report += "Price per item\tQuantity\tSize\tColors\tStyles\n";
for(int tshirts = 0;tshirts < totalCount; tshirts++)
{
report+=list[tshirts].toString() + "\n";
}
return report;
}
public void updateTotalCost()
{
count=0;
totalCost=0;
for(int tshirtsb=0;tshirtsb<totalCount;tshirtsb++)
{
totalCost+= list[tshirtsb].getPrices()*list[tshirtsb].getQuantities();
count+=list[tshirtsb].getQuantities();
}
}
private void increaseListSize()
{
tShirt[] temp = new tShirt[list.length * 2];
for(int tshirts=0;tshirts< list.length;tshirts++)
{
temp[tshirts] = list[tshirts];
}
list = temp;
}
}
//last class
import java.util.Scanner;
public class tShirtStore
{
public static void main (String[] args)
{
int item = 1;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to our Store. \n");
shoppingList clothing = new shoppingList();
while(item==1)
{
clothing.addtShirt();
System.out.println("For Men’s Department, type 1 \n"
+ "For Unisex Department, type 2 \n"
+ "For Women's Department, type 3 \n");
clothing.list[clothing.getTotalCount()-1].setGenders(scan.nextInt());
System.out.println(clothing.list[clothing.getTotalCount()-1].getPrices());
System.out.println("For TankTops, type 1 \n"
+"For T-Shirts, type 2 \n"
+"For Button Up Shirts, type 3 \n");
clothing.list[clothing.getTotalCount()-1].setStyles(scan.nextInt());
System.out.println(clothing.list[clothing.getTotalCount()-1].getPrices());
System.out.println("For White " + ", type 1 \n"
+"For Red " + ", type 2 \n"
+"For Blue " + ", type 3 \n"
+"For Green " + ", type 4 \n"
+"For Yellow "+ ", type 5 \n");
clothing.list[clothing.getTotalCount()-1].setColors(scan.nextInt());
System.out.println(clothing.list[clothing.getTotalCount()-1].getPrices());
System.out.println("For Small " + ", type 1 \n"
+"For Medium " + ", type 2 \n"
+"For Large " + ", type 3 \n");
clothing.list[clothing.getTotalCount()-1].setSizes(scan.nextInt());
System.out.println(clothing.list[clothing.getTotalCount()-1].getPrices());
System.out.println("How many of this item do you want?");
clothing.list[clothing.getTotalCount()-1].setQuantities(scan.nextInt());
System.out.println("Do you want to buy more clothes? 1 for Yes. 2 for No.");
item=scan.nextInt();
}
clothing.updateTotalCost();
System.out.println(clothing);
System.out.println("Thank you for coming to our store. Have a nice day");
}
}