Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!

    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");

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    code my classmate created
    You should ask the author of the program to add comments to the code explaining what it does.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. NEEDING HELP UNDERSTANDING A CODE FROM THREE CLASSES!!!
    By BlackShadow in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 10:11 PM
  2. Help with understanding simple GUI Code
    By jordan123 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 21st, 2012, 03:19 PM
  3. Replies: 1
    Last Post: February 10th, 2012, 08:49 AM
  4. Problem understanding Java code modification
    By eagle09 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 26th, 2011, 04:13 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM