preface
You are a programmer of a company. The company has two new batches of goods to sell. The boss wants you to write a sales procedure for these two batches of goods
demand
boss:
First of all, the names of the two shipments are shukong And border town, the price fluctuates with the market price. For example, the price of empty books is 1233 yuan per piece, while that of border town is 2564 yuan per piece. Then, our customers are some big customers, and the order quantity they order is very large, so we should give them preferential benefits. That's the way, as long as the order quantity reaches 200 pieces, Then we'll give you a 20% discount on more than 200 items.
Of course, the boss may really lose money, but you have to write anyway. First of all, as an elder in the company, a programmer using C language told you that he had ideas. Next, he snapped on the keyboard and his program came out
code
#include<stdio.h> int main(){ int shukong_price = 1233; // The price of empty books int biancheng_price = 2564; int shukong_count; // Number of empty books int biancheng_count = 0; float shukong_perferential = 0; // Preferential price for book space float biancheng_perferential = 0; double shukong_cost = shukong_price * shukong_count - shukong_perferential; // Money to pay for an empty book double biancheng_cost = biancheng_price * biancheng_count - biancheng_perferential; double cost = biancheng_cost + shukong_cost; // Total money to be paid printf("Please enter the number of empty books to purchase:\n"); scanf("%d",&shukong_count); if (shukong_count > 200){ // Calculate whether the discount can be shukong_perferential = shukong_price * (shukong_count - 200) * 0.8; printf("Because your order quantity exceeds 200, your preferential price is:%f",shukong_perferential); printf("\n"); } shukong_cost = shukong_price * shukong_count - shukong_perferential; printf("The total price of empty books is:%lf",shukong_cost); printf("\n"); printf("Please enter the number of border towns to purchase:\n"); scanf("%d",&biancheng_count); if (biancheng_count > 200){ biancheng_perferential = biancheng_price * (biancheng_count - 200) * 0.8; printf("Because your order quantity exceeds 200, your preferential price is:%f",biancheng_perferential); printf("\n"); } biancheng_cost = biancheng_price * biancheng_count - biancheng_perferential; printf("The total price of border town is:%lf",biancheng_cost); printf("\n"); cost = biancheng_cost + shukong_cost; printf("You need to pay%lf element",cost); }
This is the operation lady next to me. When she came to see it, she said, but it's too ugly to pile it together, which is not conducive to code maintenance.
At this time, the elder said that he was right, and the backhand modified the code
#include<stdio.h> float perferential(int count,int price); int main(){ int shukong_price = 1233; // The price of empty books int biancheng_price = 2564; int shukong_count; // Number of empty books int biancheng_count = 0; float shukong_perferential; // Preferential price for book space float biancheng_perferential; double shukong_cost = shukong_price * shukong_count - shukong_perferential; // Money to pay for an empty book double biancheng_cost = biancheng_price * biancheng_count - biancheng_perferential; double cost = biancheng_cost + shukong_cost; // Total money to be paid printf("Please enter the number of empty books to purchase:\n"); scanf("%d",&shukong_count); shukong_perferential = perferential(shukong_count,shukong_price); shukong_cost = shukong_price * shukong_count - shukong_perferential; printf("The total price of empty books is:%.2lf",shukong_cost); printf("\n"); printf("Please enter the number of border towns to purchase:\n"); scanf("%d",&biancheng_count); biancheng_perferential = perferential(biancheng_count,biancheng_price); biancheng_cost = biancheng_price * biancheng_count - biancheng_perferential; printf("The total price of border town is:%.2lf",biancheng_cost); printf("\n"); cost = biancheng_cost + shukong_cost; printf("You need to pay%.2lf element",cost); } float perferential(int count,int price){ // Judge whether the preferential quantity is reached, and return the preferential price if it is reached float perferential_cost; if (count > 200){ perferential_cost = price * (count - 200) * 0.8; printf("Because your order quantity exceeds 200, your preferential price is:%.2f",perferential_cost); printf("\n"); } else { printf("If you order more than 200200 goods, you can enjoy a 20% discount!"); } return perferential_cost; }
At this time, the operation lady looked at it and it looked really good. When you think about it, no, I have to write one
import java.util.Scanner; public class test{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); goods shukong = new goods("Book space"); // Initialize empty goods shukong.price = 1233; // Set blank unit price goods biancheng = new goods("Border town"); biancheng.price = 2564; System.out.print(shukong.name + "The number of purchases is:"); shukong.set_count(scan.nextInt()); // User input purchase quantity shukong.set_perferential(perferential(shukong.get_count(),shukong.price)); // Calculate preferential price System.out.println("The price of the book is:" + String.format("%.2f",shukong.get_cost())); // Printout price System.out.print(biancheng.name + "The number of purchases is:"); biancheng.set_count(scan.nextInt()); biancheng.set_perferential(perferential(biancheng.get_count(),biancheng.price)); System.out.println("The price of border town is:" + String.format("%.2f",biancheng.get_cost())); System.out.println("The total price you need to pay is:" + String.format("%.2f",(shukong.get_cost() + biancheng.get_cost()))); scan.close(); } static float perferential(int count,int price){ // Judge whether the preferential quantity is reached, and return the preferential price if it is reached float perferential_cost = 0; if (count > 200){ perferential_cost = price * (count - 200) * 0.8f; System.out.println("Because your order quantity exceeds 200, your preferential price is:"+ perferential_cost); } else { System.out.println("If you order more than 200200 goods, you can enjoy a 20% discount!"); } return perferential_cost; } } class goods{ // Commodity category String name = new String(); // name int price; // Price private int count; // quantity private float perferential; // Favorable Price double cost = price * count - perferential; // Money to pay goods(String name){ // Constructor to initialize the trade name this.name = name; } void set_count(int count){ // Set item quantity this.count = count; } int get_count(){ // Return item quantity return count; } void set_perferential(float perferential){ // Set preferential price this.perferential = perferential; } float get_perferential(){ // Return preferential price return perferential; } double get_cost(){ // Return the money to be paid cost = this.price * get_count() - get_perferential(); return cost; } }
Your code has been written, but after seeing it, the little sister of operation said that there are too many lines of code and many fancy methods. It looks really troublesome. It's not as good as the predecessors. It looks very consistent with the logic of running the software.
First time finish
epilogue
First of all, this article, or this series of articles, is to compare the ideas between C and Java, but I may not be able to do it very well, because the learning time of C language is relatively short, and I have not experienced systematic learning. This time I wrote it by reading a lot of materials and reluctantly relying on memory, Therefore, the code level may not reflect the code level of our predecessors. Ha ha, although both of us are me, we can still see that one idea is different, but both languages can actually use each other's ideas to realize the functions they want.
Secondly, the following is for the members of the club. You can copy it once and twice according to the written code. In this way, you can speed up the progress of understanding. If you don't understand, you can actually go to Baidu
Finally, the next time the boss released new requirements, and the emergence of new requirements also made things develop in an unexpected direction
PS. how do you feel like writing a novel