Java Shopping cart object problems using methods, and arrays no arrayLists. java

  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Arrays Cart Java
AI Thread Summary
The discussion revolves around troubleshooting a shopping cart implementation for an online ticket sales program. The main issues highlighted include difficulties in adding tickets to an array, calculating the total number of tickets, and determining the total purchase amount. The code provided outlines a ShoppingCart class that manages ticket purchases, including methods for adding tickets, calculating totals with tax, and printing the shopping cart contents. Specific problems identified include recursive calls in the addToCart method and incorrect calculations in the calculateTotal method, particularly in how individual ticket prices and taxes are aggregated. The conversation also emphasizes the importance of concise code sharing for effective troubleshooting and suggests that future posts should focus on isolated problem areas rather than extensive code segments. The user expresses gratitude for the assistance received and indicates a willingness to apply the feedback in future projects.
lypena35
Messages
18
Reaction score
0
Hello, I cannot seem to get the calculations correct for my shopping cart to print out the tickets and total how many tickets and how much they are. The first set of code is for the shopping cart the comment sections are for what is suppose to be happening in each section. I have the menu working and the ticket class working but getting the program to add a new ticket to the array and total the number of tickets with the price is where I am having problems with. Thank you.
This is my shopping cart object that goes along with the ticket class this is where my problem is
Code:
/**
*You have been given the task of creating a program that creates a shopping cart for an online company that sells tickets for events. Your program will have a menu that allows a user to create a shopping cart, add tickets and checkout. For simplicity, the user will input the information of the ticket.
**/

import java.util.Scanner;

public class ShoppingCart2{
private String clientName;
private Ticket [] ticketsPurchased = new Ticket [20];
private int ticketNumber = 0;
private double totalPurchase;
private double individualTickets;

    //Default constructor
public ShoppingCart2(){

}

    //second constructor for the class that receives as parameter the clientName.
    
public ShoppingCart2(String clientNameIn){
this.clientName = clientNameIn;
}

    //Setter method for the attribute name
public void setClientName(String clientNameIn){
this.clientName = clientNameIn;
}
//Setter method for the arrtibute totalPurchase
public void setTotalPurchase(double totalPurchaseIn){
this.totalPurchase = totalPurchaseIn;
}

//Getter Method for the attribute name
public String getClientName(){
return this.clientName;
}

public double getTotalPurchase(){
return this.totalPurchase;
}

    
    //other methods-Actuator methods
    
    //the method addToCart that adds a new ticket into the array of purchased tickets.
    //This method receives newTicket, an instance of the class Ticket, adds it to the list
    //of ticketsPurchased and makes the newTicket unavailable. 
    
public void addToCart(Ticket newTicket){
	addToCart(newTicket);
	newTicket.setAvailable(false);
	ticketNumber ++;
	
}
//The method calculateTotal that calculates the total purchase by:
    //1. Adding the price of all the tickets in the list
    //2. Adding 15% of taxes.
    //3. Set the totalPurchase to the total purchase calculated.
 [B]This part[/B]   
[CODE]public void calculateTotal(){
    double total=0;
    
    //This statement adds the price of individual tickets.
    individualTickets+=ticketNumber;
   
    
    //This statement assigned the value of the total purchase to the total +15%.
     totalPurchase=individualTickets +.15;
}
    
  //The method printShoppingCart prints this information of the shopping cart (each ticket).
[B]This part[/B]
    public void printShoppingCart(){
    for (int j=0;j<totalPurchase;j++) {}   
    }

//Main method of the class ShoppingCart which creates an instance of the class
//shopping cart and enables input user with a menu.

public static void main(String[] args){
ShoppingCart myShoppingCart=new ShoppingCart();
Scanner input= new Scanner(System.in);
String answ="";
int option=0;
String name;

//While method for the menu options

while(option<=2){
System.out.println("1. Set client name");
System.out.println("2. Add tickets to the shopping cart");
System.out.println("3. Checkout");
System.out.print("Input your option: ");
option=input.nextInt();
System.out.println("Option selected-"+option);
if(option==1){
//Option 1 will request input for the client name.
System.out.print("\n Enter client name:");
name=input.next();
}
Code:
   //Option 3 will create a new ticket, request the input of ticket information
                //and add the new ticket to the shopping cart with the method addToCart.
[B]This is where the new tickets are suppose to be entered[/B]
           if(option==2){
               System.out.print("\n Enter the event of the ticket:");
                Ticket ticket=new Ticket();
                myShoppingCart.printShoppingCart();
           }
[B]This is where we are suppose to get the total[/B]
            //Option 3 will do the checkout by:
                //Calculating the total of the shopping cart,
                //printing the contents of the shopping cart and exiting the program.
            if(option==3){
               System.out.println("Your total is: "); myShoppingCart.calculateTotal();
            }
else{
System.out.println("Unrecognized input, try again");
}
}
}
}
[/CODE]

This is my Ticket class and it works perfectly
Code:
/***
 **Class Ticket
 **
 ***/
public class Ticket {

 //Attributes
 private String event;
 private String seat;
 private float price;
 private boolean available;

 //Default Constructor
 public Ticket() {}

 //Second Constructor

 public Ticket(String eventIn, String seatIn, float priceIn, boolean availableIn) {
  this.event = eventIn;
  this.seat = seatIn;
  this.price = priceIn;
  this.available = availableIn;
 }

 //Third Constructor

 public Ticket(String eventIn, String seatIn, float priceIn) {
  this.event = eventIn;
  this.seat = seatIn;
  this.price = priceIn;
  this.available = true;
 }

 //Setters

 public void setEvent(String eventIn) {
  this.event = eventIn;
 }

 public void setSeat(String seatIn) {
  this.seat = seatIn;
 }

 public void setPrice(float priceIn) {
  this.price = priceIn;
 }

 public void setAvailable(boolean availableIn) {
  this.available = availableIn;
 }

 //Getters

 public String getEvent() {
  return this.event;
 }

 public String getSeat() {
  return this.seat;
 }

 public float getPrice() {
  return this.price;
 }

 public boolean getAvailable() {
  return this.available;
 }

 //Actuators/Method

 public void printTicket() {
  System.out.print("Event: " + this.event + "," + " Seat: " + seat+",");
  System.out.printf(" Price: $%.2f", this.price);
  if (this.available)
   System.out.println(", AVAILABLE.");
  else
   System.out.println(", UNAVAILABLE.");
 }

 //Define main method

 public static void main(String[] args) {

  Ticket newTicket = new Ticket("The Nutcracker", "2A", 75);
  newTicket.printTicket();
  newTicket.setAvailable(false);
     
 }

}
 
Technology news on Phys.org
Please describe what each of the following variables is supposed to store.

Code:
private Ticket [] ticketsPurchased = new Ticket [20];
private int ticketNumber = 0;
private double totalPurchase;
private double individualTickets;

Please describe in words what the method [m]addToCart[/m] should do and how it should do it. Be specific.

Please describe how to change $x$ in order to increase it by 15%.

In the future, please try to avoid posting long segments of code. Don't post parts that work; instead try to isolate the problematic part as much as possible. Provide the description that is often required for bug reports: a short description of the problem, expected behavior, actual behavior. If your problem is with math (such as adding 15% of taxes), there is no reason to post code.
 
Thank you very much for your help and tip on just posting the problem code. I was able to finally fix all the issues. However, I have another assignment to make a fake credit card encryption type project. If I run into any problems with it, I will be sure to post only the problem code. Thanks again!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top