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

    Exclamation 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
    Junior Member lunix's Avatar
    Join Date
    Apr 2012
    Location
    England
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    use code tags...also is there anything specific you need to know? you named most the concepts being used in your post, do you need these concepts quoting from your code and explaining and if so which? im not a great help as im learning myself but more detail usually helps get a quicker answer. Also i can help with most the basic concepts....
    Feels good man

Similar Threads

  1. 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
  2. Replies: 1
    Last Post: February 10th, 2012, 09:49 AM
  3. 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
  4. [SOLVED] [Method] needing some help
    By Perplexing in forum Object Oriented Programming
    Replies: 4
    Last Post: December 2nd, 2010, 05:03 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM