Skip to content

Instantly share code, notes, and snippets.

@jaohaohsuan
Created May 8, 2019 02:28
Show Gist options
  • Save jaohaohsuan/d9f1307445f16324c7d37da0a03a4146 to your computer and use it in GitHub Desktop.
Save jaohaohsuan/d9f1307445f16324c7d37da0a03a4146 to your computer and use it in GitHub Desktop.
week 10 recursion
package comp601.week10;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
Ticket t1 = new Ticket("t1", 155);
Ticket t2 = new Ticket("t2", 245);
Ticket t3 = new Ticket("t3", 355);
List<Ticket> tickets = Arrays.asList(t1,t2,t3);
System.out.printf("the total airpoints of tickets is %d.\n", getTotalAirpoints(tickets, 0));
}
public static int getTotalAirpoints(List<Ticket> tickets, int accum) {
if(tickets == null || tickets.size() == 0) {
return accum;
}
List<Ticket> tail = tickets.subList(1, tickets.size());
accum += tickets.get(0).getAirpoints();
return getTotalAirpoints(tail, accum);
}
}
package comp601.week10;
public class Ticket {
private String tno;
private double price;
public Ticket(String tno, double price) {
this.setTno(tno);
this.setPrice(price);
}
public String getTno() {
return tno;
}
public void setTno(String tno) {
this.tno = tno;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getAirpoints() {
// 3 points for every $100
int p3 = ((int)price / 100) * 3;
// 1 points for every %50
int p1 = (((int)price % 100) / 50) * 1;
// no points for under $50
return p3 + p1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment