P168 实战练习(权限修饰符)

创建Game类,运行代码如下:

  1 package org.hanqi.pn0120;
  2 
  3 public class Game {
  4 
  5     private String name;
  6     private String category;
  7     private int totalcost = 10000;
  8     
  9     public String getName() {
 10         return name;
 11     }
 12     public void setName(String name) {
 13         this.name = name;
 14     }
 15     public String getCategory() {
 16         return category;
 17     }
 18     public void setCategory(String category) {
 19         this.category = category;
 20     }
 21     public int getTotalcost() {
 22         return totalcost;
 23     }
 24     public void setTotalcost(int totalcost) {
 25         this.totalcost = totalcost;
 26     }
 27     
 28     public Game(String name, String category) {
 29         super();
 30         this.name = name;
 31         this.category = category;        
 32     }    
 33     
 34     public void cost1(int cost)
 35     {
 36         if(cost <= 0)
 37         {
 38             System.out.println("做游戏的钱呢?!");            
 39         }
 40         else if(cost>this.totalcost)
 41         {
 42             int excess=cost-this.totalcost;
 43             System.out.println("超出预算了,超了"+excess+"金额的预算");
 44         }
 45         else
 46         {
 47             this.totalcost=cost;
 48             System.out.println("这次预算是"+this.totalcost);            
 49         }            
 50     }    
 51     
 52     private int sales;
 53     public int getSales()
 54     {
 55         return this.sales;
 56     }
 57     public void money(int number,int price)
 58     {
 59         if(number<=0)
 60         {
 61             System.out.println("一份都没卖出去,赔钱了,公司要倒闭");
 62         }
 63         else if(price<=0)
 64         {
 65             System.out.println("为什么定价是0?");
 66         }
 67         else if(price>50)
 68         {
 69             System.out.println("定价太高了");
 70         }
 71         else
 72         {
 73             this.sales=number*price;
 74             System.out.println("销售额是"+sales);
 75         
 76         if(this.sales<this.totalcost)
 77         {
 78             int debt = this.totalcost-this.sales;
 79             System.out.println("入不敷出,公司倒闭,欠了 "+debt+" 金额的债务");
 80         }
 81         else
 82         {
 83             int profit = this.sales-this.totalcost;
 84             System.out.println("挣钱了,利润是"+profit);
 85         }
 86         }
 87     }
 88     
 89     public static void main(String[]args)
 90     {
 91         Game myGame = new Game("你与我的星空","Galgame");
 92              System.out.println("游戏名称是:"+myGame.getName());
 93              System.out.println("游戏类型为:"+myGame.getCategory());
 94              myGame.cost1(-10000);             
 95              myGame.cost1(20000);
 96              myGame.cost1(2000);
 97              myGame.money(0, 25);
 98              myGame.money(14, 0);
 99              myGame.money(200, 88);
100              myGame.money(200, 25);             
101     }    
102 }

则运行代码为:

原文地址:https://www.cnblogs.com/hanazawalove/p/5246795.html